Home Unit 2 Hacks
Post
Cancel

Unit 2 Hacks

Hacks (no chat gpt!!)

Hack 1: (0.25) 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.ArrayList;
import java.util.Scanner;

public class MyArrayList {
    private ArrayList<Integer> intList = new ArrayList<>();  
    private Scanner scanner = new Scanner(System.in); 

    public void addToIntArray() {
        int myInt = scanner.nextInt(); 
        intList.add(myInt);
    }

    public int callInteger() {
        System.out.println("Enter an index to retrieve an integer from the list:");
        int index = scanner.nextInt();
        System.out.println("the selected index was " + index);

        if (index >= 0 && index < intList.size()) {
            return intList.get(index);
        }

        return -1;
    }

    public static void main(String[] args){
        MyArrayList myList = new MyArrayList();
        System.out.println("Enter an integer to add to the list:");
        myList.addToIntArray();

        int retrievedValue = myList.callInteger();

        if (retrievedValue != -1) {
            System.out.println("Value at the requested index: " + retrievedValue);
        } else {
            System.out.println("Index is out of bounds.");
        }
    }
}

MyArrayList.main(null);

1
2
3
4
Enter an integer to add to the list:
Enter an index to retrieve an integer from the list:
the selected index was 0
Value at the requested index: 5

Hack 2: (0.25) 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Random;
import java.util.Scanner;

public class GuessingGame {
   private double randomNumber;
   private double randomExponent;
   private double result;

   public double createRandomNumber(){
        double min = 0;
        double max = 10;

        randomNumber = Math.floor(Math.random() *(max - min + 1) + min);
        randomExponent = Math.floor(Math.random() *(max - min + 1) + min);
        result = Math.pow(randomNumber, randomExponent); 
        
        //print statements i was using to make sure this thing worked
        //System.out.println(randomNumber);
        //System.out.println(randomExponent);
        //System.out.println(randomNumber + " to the power of " + randomExponent);
        //System.out.println(result);
      
        return randomExponent;
    }

    public void guessingGame() {

        System.out.println("Welcome to the exponent/root guessing game");
        System.out.println("You need to guess the exponent/root, but don't worry there are hints!");

        Scanner guess = new Scanner(System.in);
        boolean correct = false;


        while(!correct) {
            System.out.print("Enter your guess: ");
            double myGuess = guess.nextDouble();

            if (myGuess == randomExponent) {
                System.out.println("That is correct, good job!");
                correct = true;
            }

            else if (myGuess > randomExponent){
                System.out.println("Your guess is too high. Would you like a hint? (say yes or no)");
                Scanner hint = new Scanner(System.in);
                String myHint = hint.next();
                if (myHint.equalsIgnoreCase("yes")){
                    System.out.println(randomNumber + " to the power of this random exponent is " + result);
                }
            }
            else {
                System.out.println("Your guess is too low. Would you like a hint? (say yes or no)");
                Scanner hint = new Scanner(System.in);
                String myHint = hint.next();
    
                if (myHint.equalsIgnoreCase("yes")){
                    System.out.println(randomNumber + " to the power of this random exponent is " + result);
                }
            }
        }
        
    }

    public static void main(String[] args) {
        GuessingGame game = new GuessingGame();
        game.createRandomNumber();
        game.guessingGame();
       
    }
}

GuessingGame.main(null);
1
2
3
4
5
6
7
Welcome to the exponent/root guessing game
You need to guess the exponent/root, but don't worry there are hints!
Enter your guess: Your guess is too low. Would you like a hint? (say yes or no)
2.0 to the power of this random exponent is 512.0
Enter your guess: Your guess is too low. Would you like a hint? (say yes or no)
Enter your guess: Your guess is too low. Would you like a hint? (say yes or no)
Enter your guess: That is correct, good job!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class test {
    
    public static void main(String[] args) {
        double min = 0;
        double max = 10;

        double randomNumber = Math.floor(Math.random() *(max - min + 1) + min);
        System.out.println(randomNumber);

        double randomExponent = Math.floor(Math.random() *(max - min + 1) + min);
        System.out.println(randomExponent);

        double result = Math.pow(randomNumber, randomExponent); 
        System.out.println(randomNumber + " to the power of " + randomExponent);

        System.out.println(result);
}

}

test.main(null);

1
2
3
4
3.0
6.0
3.0 to the power of 6.0
729.0

Hack 3: (0.25) 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 giving some samples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Car {

    private int year;
    private double price;
    private String brand;

    public Car(int year, double price, String brand) {
        this.year = year;
        this.price = price;
        this.brand = brand;
    }

    public static void main(String[] args) {
        Car car1 = new Car(1969, 50000, "Mustang");
        Car car2 = new Car(2000, 85000, "Tesla");
        Car car3 = new Car(1935, 65000, "Ford");
        Car car4 = new Car(2011, 100000, "Dodge");
        Car car5 = new Car(1988, 20000, "Honda");

        System.out.println(car1.year); 
        System.out.println(car2.brand); 
        System.out.println(car5.price); 
    }
}

Car.main(null);

1
2
3
1969
Tesla
20000.0

Hack 4: (0.25) Using your preliminary knowledge of loops, use a for loop to iterate through a person’s first and last name, separated by a space, and create methods to call a person’s first name and a person’s last name by iterating through the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;

public class Name {

    public String getFullName() {
        Scanner fullNameScanner = new Scanner(System.in);
        String name = fullNameScanner.nextLine();
        return name;
    }

    public void firstName(String name) {
        System.out.println("Your first name is: ");
        int i = 0;
        while (i < name.length() && name.charAt(i) != ' ') {  
            System.out.print(name.charAt(i));
            i++;
        }
        System.out.println();
    }

    public void lastName(String name) {
        System.out.println("Your last name is: ");

        //stores the index of the ' ' in nameSeparator
        int nameSeparator = name.lastIndexOf(' ');

        // -1 means no space is found
        if (nameSeparator != -1) {
            // nameSeparator + 1 because I want it to print everything after the space
            for (int i = nameSeparator + 1; i < name.length(); i++){
                System.out.print(name.charAt(i));
            }
        }
        System.out.println();
        
    }

    public static void main(String[] args) {
        Name newName = new Name();
        System.out.println("Enter your full name");
        String fullName = newName.getFullName();
        newName.firstName(fullName);
        newName.lastName(fullName);
    }
}

Name.main(null);
1
2
3
4
5
Enter your full name
Your first name is: 
Vivian
Your last name is: 
Ni
This post is licensed under CC BY 4.0 by the author.

Unit 1 Lesson

Unit 2 Lesson