Hack 1

In this example, the addToArrayList method is a void method that adds an integer to the ArrayList. The getValueAtIndex method is a non-void method that retrieves the value at a specified index from the ArrayList. The main method demonstrates how to use these methods by creating an instance of the ArrayListExample class, adding values to the ArrayList, and retrieving values at specific indices.

import java.util.ArrayList;

public class ArrayListExample {
    // ArrayList to store integers
    private ArrayList<Integer> integerList = new ArrayList<>();

    // Void method to add an integer to the ArrayList
    public void addToArrayList(int value) {
        integerList.add(value);
    }

    // Non-void method to retrieve the value at a specific index
    public int getValueAtIndex(int index) {
        // Check if the index is within bounds
        if (index >= 0 && index < integerList.size()) {
            return integerList.get(index);
        } else {
            // Handle index out of bounds
            System.out.println("Index out of bounds");
            return -1; // or throw an exception
        }
    }

    public static void main(String[] args) {
        ArrayListExample example = new ArrayListExample();

        // Add values to the ArrayList
        example.addToArrayList(10);
        example.addToArrayList(20);
        example.addToArrayList(30);

        // Retrieve and print value at index 1
        int valueAtIndex1 = example.getValueAtIndex(1);
        System.out.println("Value at index 1: " + valueAtIndex1);

        // Try to retrieve a value at an invalid index
        int valueAtInvalidIndex = example.getValueAtIndex(5);
        System.out.println("Value at index 5: " + valueAtInvalidIndex); // This will print "Index out of bounds"
    }
}
ArrayListExample.main(null)

Hack 2

This program generates a random number raised to a random exponent or its square root and asks the user to guess both the base and the exponent or root. The user receives hints about the number’s range before making a guess. The generateRandomNumber method is static, and the playGame method is non-static.

import java.util.Random;
import java.util.Scanner;

public class MathGuessingGame {
    // Static method to generate a random number to a random exponent (including roots)
    private static double generateRandomNumber() {
        Random random = new Random();
        double base = random.nextDouble() * 10 + 1; // Random base between 1 and 10
        int exponent = random.nextInt(5) + 2; // Random exponent between 2 and 6
        boolean isRoot = random.nextBoolean();

        if (isRoot) {
            // If it's a root, generate a square root
            return Math.sqrt(base);
        } else {
            // Otherwise, raise the base to the random exponent
            return Math.pow(base, exponent);
        }
    }

    // Non-static method to play the guessing game
    private void playGame() {
        double randomNumber = generateRandomNumber();
        Scanner scanner = new Scanner(System.in);  // Scanner is used to receive user input and parse them into primitive data types such as int, double or default String.

        System.out.println("Welcome to the Math Guessing Game!");
        System.out.println("Can you guess the base, exponent, and root of the number?");

        // Provide a hint
        System.out.println("Hint: The number is " + (randomNumber > 1 ? "greater than 1" : "between 0 and 1"));

        // Get user guesses
        System.out.print("Enter your guess for the base: ");
        double userBaseGuess = scanner.nextDouble();

        System.out.print("Enter your guess for the exponent: ");
        int userExponentGuess = scanner.nextInt();

        // Check if it's a root
        boolean isRootGuess = userExponentGuess == 0;

        // Check user guesses
        if (userBaseGuess == (isRootGuess ? Math.sqrt(randomNumber) : randomNumber) && userExponentGuess == (isRootGuess ? 0 : Math.log(randomNumber) / Math.log(userBaseGuess))) {
            System.out.println("Congratulations! You guessed correctly!");
        } else {
            System.out.println("Sorry, that's incorrect. The correct answers are:");
            System.out.println("Base: " + randomNumber);
            System.out.println("Exponent: " + (isRootGuess ? "Root" : userExponentGuess));
            System.out.println("Root: " + (isRootGuess ? userExponentGuess : "Not applicable"));
        }
    }

    public static void main(String[] args) {
        MathGuessingGame game = new MathGuessingGame();
        game.playGame();
    }
}
MathGuessingGame.main(null)

Hack 3

In this example, the Person class has parameters for age (int), student status (boolean), name (String), and height (double). The main method demonstrates creating five instances of the Person class and accessing their information using getter methods.

public class Person {
    private int age;
    private boolean isStudent;
    private String name;
    private double height;

    // Constructor
    public Person(int age, boolean isStudent, String name, double height) {
        this.age = age;
        this.isStudent = isStudent;
        this.name = name;
        this.height = height;
    }

    // Getter methods
    public int getAge() {
        return age;
    }

    public boolean isStudent() {
        return isStudent;
    }

    public String getName() {
        return name;
    }

    public double getHeight() {
        return height;
    }

    public static void main(String[] args) {
        // Creating instances of the Person class
        Person person1 = new Person(25, true, "Alice", 1.75);
        Person person2 = new Person(30, false, "Bob", 1.80);
        Person person3 = new Person(22, true, "Charlie", 1.65);
        Person person4 = new Person(35, false, "David", 1.70);
        Person person5 = new Person(28, true, "Eva", 1.68);

        // Accessing information
        System.out.println("Person 1: " + person1.getName() + ", Age: " + person1.getAge() + ", Student: " + person1.isStudent() + ", Height: " + person1.getHeight());
        System.out.println("Person 2: " + person2.getName() + ", Age: " + person2.getAge() + ", Student: " + person2.isStudent() + ", Height: " + person2.getHeight());
        System.out.println("Person 3: " + person3.getName() + ", Age: " + person3.getAge() + ", Student: " + person3.isStudent() + ", Height: " + person3.getHeight());
        System.out.println("Person 4: " + person4.getName() + ", Age: " + person4.getAge() + ", Student: " + person4.isStudent() + ", Height: " + person4.getHeight());
        System.out.println("Person 5: " + person5.getName() + ", Age: " + person5.getAge() + ", Student: " + person5.isStudent() + ", Height: " + person5.getHeight());
    }
}
Person.main(null)

Hack 4

In this example, the extractFirstName method uses the indexOf method to find the position of the space in the full name string. It then uses the substring method to extract the characters from the beginning of the string up to the space (exclusive). The extractLastName method extracts the characters after the space to the end of the string.

public class PersonNameProcessor {

    // Method to extract and return the first name
    public static String extractFirstName(String fullName) {
        // Assuming the first name is the part before the space
        int spaceIndex = fullName.indexOf(' ');
        if (spaceIndex != -1) {
            return fullName.substring(0, spaceIndex);
        } else {
            return fullName; // If no space is found, consider the whole string as the first name
        }
    }

    // Method to extract and return the last name
    public static String extractLastName(String fullName) {
        // Assuming the last name is the part after the space
        int spaceIndex = fullName.indexOf(' ');
        if (spaceIndex != -1) {
            return fullName.substring(spaceIndex + 1);
        } else {
            return ""; // If no space is found, consider an empty string as the last name
        }
    }

    public static void main(String[] args) {
        // Example full name
        String fullName = "John Doe";

        // Call the methods to extract first and last names
        String firstName = extractFirstName(fullName);
        String lastName = extractLastName(fullName);

        // Output the results
        System.out.println("Full Name: " + fullName);
        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
    }
}
PersonNameProcessor.main(null)