Hack 1

Create a void method that takes an integer input and adds it to an ArrayList. Then, add a non-void method that is able to call a certain index from the ArrayList.

import java.util.ArrayList;

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

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

    // Non-void method to retrieve an element from the ArrayList at a specific index
    public int getElementAtIndex(int index) {
        // Check if the index is valid
        if (index >= 0 && index < integerList.size()) {
            return integerList.get(index);
        } else {
            // If the index is out of bounds, return a default value (you can change this as needed)
            System.err.println("Index is out of bounds.");
            return -1; // Default value for an invalid index
        }
    }
}

ArrayListExample example = new ArrayListExample();

// Add integers to the ArrayList
example.addIntegerToList(10);
example.addIntegerToList(20);
example.addIntegerToList(30);

// Retrieve and print elements from the ArrayList
System.out.println("Element at index 0: " + example.getElementAtIndex(0));
System.out.println("Element at index 1: " + example.getElementAtIndex(1));
System.out.println("Element at index 2: " + example.getElementAtIndex(2));

// Attempt to retrieve an element at an out-of-bounds index
System.out.println("Element at index 3: " + example.getElementAtIndex(3));
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30


Index is out of bounds.


Element at index 3: -1

Hack 2

Create a simple guessing game with random numbers in math, except the random number is taken to a random exponent (also includes roots), and the person has to find out what the root and exponent is (with hints!). Use at least one static and one non-static method in your class.

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

public class GuessingGame {
    private static Random random = new Random();
    private double number;

    public GuessingGame() {
        generateRandomNumber();
    }

    private void generateRandomNumber() {
        // Generate a random number between 2 and 10
        double base = random.nextDouble() * 9 + 2;
        
        // Generate a random exponent between 2 and 5 (you can adjust the range)
        int exponent = random.nextInt(4) + 2;
        
        // Calculate the number as base raised to the power of the exponent
        number = Math.pow(base, exponent);
    }

    public void play() {
        Scanner scanner = new Scanner(System.in);
        int attempts = 3;
        
        System.out.println("Welcome to the Guessing Game!");
        System.out.println("I have a number in the form of (base^exponent). Your task is to guess both the base and the exponent.");
        System.out.println("You have " + attempts + " attempts.");

        while (attempts > 0) {
            System.out.print("Enter your guess for the base: ");
            double guessBase = scanner.nextDouble();
            
            System.out.print("Enter your guess for the exponent: ");
            int guessExponent = scanner.nextInt();
            
            if (guessBase == Math.floor(guessBase) && guessExponent > 1) {
                if (guessBase == Math.floor(number) && guessExponent == Math.log(number) / Math.log(guessBase)) {
                    System.out.println("Congratulations! You guessed it correctly.");
                    break;
                } else {
                    System.out.println("Incorrect! Try again.");
                    attempts--;
                    System.out.println("You have " + attempts + " attempts left.");
                }
            } else {
                System.out.println("Please enter valid guesses (integer for base and exponent > 1).");
            }
        }

        if (attempts == 0) {
            System.out.println("Sorry, you've run out of attempts. The correct answer was: " + number);
        }

        scanner.close();
    }
}

GuessingGame game = new GuessingGame();
game.play();
Welcome to the Guessing Game!
I have a number in the form of (base^exponent). Your task is to guess both the base and the exponent.
You have 3 attempts.
Enter your guess for the base: Enter your guess for the exponent: Incorrect! Try again.
You have 2 attempts left.
Enter your guess for the base: Enter your guess for the exponent: Incorrect! Try again.
You have 1 attempts left.
Enter your guess for the base: Enter your guess for the exponent: Incorrect! Try again.
You have 0 attempts left.
Sorry, you've run out of attempts. The correct answer was: 554.2704042432692

Hack 3

Create a class of your choosing that has multiple parameters of different types (int, boolean, String, double) and put 5 data values in that list. Show that you can access the information by givng some samples.

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

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

    public int getAge() {
        return age;
    }

    public boolean isStudent() {
        return isStudent;
    }

    public String getName() {
        return name;
    }

    public double getHeight() {
        return height;
    }
}


// Create instances of the Person class
Person person1 = new Person(25, true, "Alice", 165.5);
Person person2 = new Person(30, false, "Bob", 180.0);
Person person3 = new Person(22, true, "Charlie", 175.2);
Person person4 = new Person(28, false, "David", 170.7);
Person person5 = new Person(35, true, "Eva", 160.3);

// Access and print information about the persons
System.out.println("Person 1:");
System.out.println("Name: " + person1.getName());
System.out.println("Age: " + person1.getAge() + " years");
System.out.println("Is a student: " + (person1.isStudent() ? "Yes" : "No"));
System.out.println("Height: " + person1.getHeight() + " cm");
System.out.println();
Person 1:
Name: Alice
Age: 25 years
Is a student: Yes
Height: 165.5 cm

Hack 4

Using your preliminary knowlege of loops, use a for loop to iterate through a person’s first and last name, seperated by a space, and create methods to call a person’s first name and a person’s last name by iterating through the string.

public class PersonName {
    private String fullName;

    public PersonName(String fullName) {
        this.fullName = fullName;
    }

    // Method to get the first name
    public String getFirstName() {
        // Split the full name using a space as the delimiter
        String[] nameParts = fullName.split(" ");
        
        // Check if there are at least two parts (first name and last name)
        if (nameParts.length >= 2) {
            return nameParts[0]; // The first part is the first name
        } else {
            return "Invalid Name"; // Handle the case of an invalid name format
        }
    }

    // Method to get the last name
    public String getLastName() {
        // Split the full name using a space as the delimiter
        String[] nameParts = fullName.split(" ");
        
        // Check if there are at least two parts (first name and last name)
        if (nameParts.length >= 2) {
            return nameParts[1]; // The second part is the last name
        } else {
            return "Invalid Name"; // Handle the case of an invalid name format
        }
    }
}

PersonName person = new PersonName("John Doe");

// Access and print the first name and last name
System.out.println("First Name: " + person.getFirstName());
System.out.println("Last Name: " + person.getLastName());
First Name: John
Last Name: Doe