Skip to the content.

Hacks for the Unit 2 Student Lesson • 8 min read

Description

Unit 2 - Using Objects Hacks

Hack 1

import java.util.ArrayList;

public class ArrayListExample {
    private ArrayList<Integer> integerList = new ArrayList<>();
    public void addIntegerToList(int number) {
        integerList.add(number);
    }

    public int getIntegerAtIndex(int index) {
        if (index >= 0 && index < integerList.size()) {
            return integerList.get(index);
        } else {
            throw new IndexOutOfBoundsException("Index is out of bounds.");
        }
    }

    public static void main(String[] args) {
        ArrayListExample example = new ArrayListExample();
        example.addIntegerToList(10);
        example.addIntegerToList(20);
        example.addIntegerToList(30);
        int retrievedNumber = example.getIntegerAtIndex(1);
        System.out.println("Number at index 1: " + retrievedNumber);
    }
}

ArrayListExample.main(null);
Number at index 1: 20

Hack 2

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

public class NumberGuessingGame {
    private static Random random = new Random();
    private double base;
    private int exponent;

    public NumberGuessingGame() {
        base = random.nextDouble() * 8 + 2;
        exponent = random.nextInt(5) + 1;
    }

    public void play() {
        System.out.println("Welcome to the Number Guessing Game!");
        System.out.println("You need to guess the root and exponent of a number.");
        System.out.println("Here's a hint: The number is a root of " + base + " raised to the power of " + exponent + ".");
        
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your guess for the root: ");
        double guessedRoot = scanner.nextDouble();
        System.out.print("Enter your guess for the exponent: ");
        int guessedExponent = scanner.nextInt();

        if (guessedRoot == Math.pow(base, 1.0 / exponent) && guessedExponent == exponent) {
            System.out.println("Congratulations! You guessed correctly!");
        } else {
            System.out.println("Sorry, your guess is incorrect.");
            System.out.println("The correct answer is: " + Math.pow(base, 1.0 / exponent) + " raised to the power of " + exponent);
        }
    }

    public static void main(String[] args) {
        NumberGuessingGame game = new NumberGuessingGame();
        game.play();
    }
}

NumberGuessingGame.main(null);
Welcome to the Number Guessing Game!
You need to guess the root and exponent of a number.
Here's a hint: The number is a root of 2.282116664503402 raised to the power of 3.
Enter your guess for the root: Enter your guess for the exponent: Sorry, your guess is incorrect.
The correct answer is: 1.316576040632286 raised to the power of 3

Hack 3

public class MyClass {
    private int intValue;
    private boolean booleanValue;
    private String stringValue;
    private double doubleValue;

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

    public int getIntValue() {
        return intValue;
    }

    public boolean isBooleanValue() {
        return booleanValue;
    }

    public String getStringValue() {
        return stringValue;
    }

    public double getDoubleValue() {
        return doubleValue;
    }

    public static void main(String[] args) {
        MyClass myObject = new MyClass(42, true, "Hello, World!", 3.14);

        System.out.println("Int Value: " + myObject.getIntValue());
        System.out.println("Boolean Value: " + myObject.isBooleanValue());
        System.out.println("String Value: " + myObject.getStringValue());
        System.out.println("Double Value: " + myObject.getDoubleValue());
    }
}

MyClass.main(null);
Int Value: 42
Boolean Value: true
String Value: Hello, World!
Double Value: 3.14

Hack 4

public class PersonNameParser {
    private String fullName;

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

    public String getFirstName() {
        int spaceIndex = fullName.indexOf(' ');
        if (spaceIndex != -1) {
            return fullName.substring(0, spaceIndex);
        } else {
            return fullName;
        }
    }

    public String getLastName() {
        int spaceIndex = fullName.indexOf(' ');
        if (spaceIndex != -1 && spaceIndex < fullName.length() - 1) {
            return fullName.substring(spaceIndex + 1);
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        PersonNameParser person = new PersonNameParser("John Mortensen");

        System.out.println("First Name: " + person.getFirstName());
        System.out.println("Last Name: " + person.getLastName());
    }
}

PersonNameParser.main(null);
First Name: John
Last Name: Mortensen