Unit 2 Hacks


Hack 1


import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListHack1 {
    private ArrayList<Integer> list = new ArrayList<>();

    // void method to append int value
    public void addToArrayList(int value) {
        list.add(value);
    }

    // non-void method to call index from arraylist
    public int indexCall(int index) {
        if (index >= 0 && index < list.size()) {
            return list.get(index);
        } else {
            return -1; // Return -1 if the index is not in the valid range
        }
    }

    public static void main(String[] args) {
        ArrayListHack1 example = new ArrayListHack1();
        Scanner scanner = new Scanner(System.in);

        // putting in some values to the arraylist :))

        example.addToArrayList(419);
        example.addToArrayList(420);
        example.addToArrayList(421);


        System.out.println("Enter an index to get the corresponding ArrayList value: ");
        int userIndex = scanner.nextInt();

        int element = example.indexCall(userIndex);

        // using some nice ! operators (NOT) equal to -1 --> return value of -1 signifies that index was anyways out of bound, this is from the non-void method
        if (element != -1) {
            System.out.println("Element at index " + userIndex + ": " + element + " - " + " " + "very good beta..");
        } else {
            System.out.println("INVALID INDEX... TRY AGAIN BETA");
        }

        scanner.close();
    }
}

ArrayListHack1.main(null);
Enter an index to get the corresponding ArrayList value: 


Element at index 1: 420 -  very good beta..


Hack 2

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

public class RootsGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int min = 1;
        int max = 10;
        int baseValue = random.nextInt(max - min + 1) + min; // generates random base
        int exponent = random.nextInt(5) + 1; // generates random exponent

        System.out.println("Guess the base value and exponent of a number between " + min + " and " + max + " (Hints will be provided):");
         
        int attempts = 0;
        while (true) {
            System.out.println("Enter your guess for the BASE value: ");
            int baseGuess = scanner.nextInt();

            System.out.println("Enter your guess for the EXPONENT value: ");
            int exponentGuess = scanner.nextInt();

            if (baseGuess == baseValue && exponentGuess == exponent) {
                System.out.println("Congratulations! You guessed the base value and exponent correctly.");
                break;
            } else {
                attempts++;
                System.out.println("Incorrect guess. Hints: ");
                // hint 1 is kinda messed, just gives the same hint everytime, not sure how this can be enhanced though.
                System.out.println("Hint 1: The base value is between " + min + " and " + max);
                System.out.println("Hint 2: The exponent is " + exponent);
                // updates attempts integer variable which was initialized to be = 0
                System.out.println("Attempts: " + attempts);
            }
        }

        scanner.close();
    }
}

RootsGame.main(null);
Guess the base value and exponent of a number between 1 and 10 (Hints will be provided):
Enter your guess for the base value: 
Enter your guess for the exponent: 
Incorrect guess. Hints: 
Hint 1: The base value is between 1 and 10
Hint 2: The exponent is 4
Attempts: 1
Enter your guess for the base value: 
Enter your guess for the exponent: 
Incorrect guess. Hints: 
Hint 1: The base value is between 1 and 10
Hint 2: The exponent is 4
Attempts: 2
Enter your guess for the base value: 
Enter your guess for the exponent: 
Congratulations! You guessed the base value and exponent correctly.

There are some things I could possibly improve on for this hack:

  1. Make it so that the hints change, not the same hint every time (at least for hint 1)

  2. Make it so that the user’s inputs are being outputted so that anyone else can also see what someone guessed.


Hack 3

import java.util.Scanner;

public class AccessClass {
    private int intValue;
    private boolean boolValue;
    private String stringValue;
    private double doubleValue;

    public AccessClass(int intValue, boolean boolValue, String stringValue, double doubleValue) {
        this.intValue = intValue;
        this.boolValue = boolValue;
        this.stringValue = stringValue;
        this.doubleValue = doubleValue;
    }


    public static void main(String[] args) {
        // initializes a sample object as asked for by the hack 
        AccessClass myObject = new AccessClass(420, false, "Theo <3", 3.141592653589);
        Scanner scanner = new Scanner(System.in);


        // allows the user to select a certain data type and output the corresponding object from the different types of data
        System.out.println("Enter the data type (int, boolean, string, double): ");
        String dataType = scanner.nextLine();

        // all the switch cases defined here for each data type, simply printing
        
        switch (dataType.toLowerCase()) {
            case "int":
                System.out.println("Int Value: " + myObject.intValue);
                break;
            case "boolean":
                System.out.println("Boolean Value: " + myObject.boolValue);
                break;
            case "string":
                System.out.println("String Value: " + myObject.stringValue);
                break;
            case "double":
                System.out.println("Double Value: " + myObject.doubleValue);
                break;
            default:
                System.out.println("Unsupported data type.");
        }

        scanner.close();
    }
}

AccessClass.main(null);

Enter the data type (int, boolean, string, double): 
String Value: Theo <3

NOTE: For this hack, there is probably a way for me to shorten the entire switch case code into 1-2 lines that adapts itself based on the user input, but I just chose not to for now since this way was easier to think of.


Hack 4

public class PersonName {
    private String fullName;

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

    public String getFirstName() {
        StringBuilder firstName = new StringBuilder();
        // for loop, i++ is jus starting at index 0 and increasing the index until it reaches a space
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);
            if (c == ' ') {
                // this makes it so that the for loop iteration stops when it reaches a space, which would logically be the space between first and last name
                break; 
            }
            firstName.append(c);
        }
        return firstName.toString();
    }
     
    // this is just a cut paste of the first name code, does the exact same thing
    public String getLastName() {
        StringBuilder lastName = new StringBuilder();
        boolean foundSpace = false;
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);
            if (foundSpace) {
                lastName.append(c);
            }
            if (c == ' ') {
                foundSpace = true;
            }
        }
        return lastName.toString();
    }

    public static void main(String[] args) {
        // sample bf name to show working capabilities
        PersonName person = new PersonName("Theo Huntalas");

        String firstName = person.getFirstName();
        String lastName = person.getLastName();

        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
    }
}

PersonName.main(null);

First Name: Theo
Last Name: Huntalas


Hack Reflection

Overall, I thought the hacks were pretty good for helping solidify the Unit 2 content from the lesson. I felt that the second hack was easily the hardest one, simply because it was difficult to get the random part of it working properly.