Skip to main content Link Menu Expand (external link) Document Search Copy Copied

lesson 2 hacks • 18 min read

Description

the hacks for all of the secction 2

hack numero uno (chatgpt)

import java.util.ArrayList;

public class ArrayListExample {
    private ArrayList<Integer> arrayList = new ArrayList<>();

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

    // Non-void method to retrieve a value from the ArrayList at a specific index
    public int getValueAtIndex(int index) {
        if (index >= 0 && index < arrayList.size()) {
            return arrayList.get(index);
        } else {
            // Handle index out of bounds error
            System.out.println("Index is out of bounds.");
            return -1; // You can choose an appropriate default value or throw an exception
        }
    }

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

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

        // Retrieving a value from the ArrayList at a specific index
        int valueAtIndex = example.getValueAtIndex(1);
        System.out.println("Value at index 1: " + valueAtIndex);

        // Trying to access an index that is out of bounds
        int outOfBoundsValue = example.getValueAtIndex(5);
        System.out.println("Value at index 5: " + outOfBoundsValue);
    }
}
ArrayListExample.main(null)
Value at index 1: 20
Index is out of bounds.
Value at index 5: -1

hack numero dos (chatgpt)

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

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

    public GuessingGame() {
        // Generate a random base value between 2 and 10
        base = random.nextDouble() * 8 + 2;

        // Generate a random exponent between 2 and 5
        exponent = random.nextInt(4) + 2;

        // Calculate the result by raising the base to the exponent
        result = Math.pow(base, exponent);
    }

    public void play() {
        Scanner scanner = new Scanner(System.in);
        int attempts = 0;
        double guessBase;
        int guessExponent;

        System.out.println("Welcome to the Guessing Game!");
        System.out.println("I've raised a number to an exponent (including roots), can you guess them?");
        System.out.println("Here's a hint: The result is " + result);

        do {
            System.out.print("Guess the base: ");
            guessBase = scanner.nextDouble();

            System.out.print("Guess the exponent: ");
            guessExponent = scanner.nextInt();

            attempts++;

            if (guessBase == base && guessExponent == exponent) {
                System.out.println("Congratulations! You guessed it!");
                System.out.println("It took you " + attempts + " attempts.");
                break;
            } else {
                System.out.println("Incorrect! Try again.");

                // Provide hints based on the player's guesses
                if (guessBase < base) {
                    System.out.println("Hint: The base is higher.");
                } else {
                    System.out.println("Hint: The base is lower.");
                }

                if (guessExponent < exponent) {
                    System.out.println("Hint: The exponent is higher.");
                } else {
                    System.out.println("Hint: The exponent is lower.");
                }
            }
        } while (true);
    }

    public static void main(String[] args) {
        GuessingGame game = new GuessingGame();
        game.play();
    }
}
GuessingGame.main(null)
Welcome to the Guessing Game!
I've raised a number to an exponent (including roots), can you guess them?
Here's a hint: The result is 503.5925459322878
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is higher.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is higher.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is higher.
Guess the base: Guess the exponent: Incorrect! Try again.
Hint: The base is lower.
Hint: The exponent is lower.
Guess the base: Guess the exponent: 

hack numero tres (chatgpt)

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;
    }

    public static void main(String[] args) {
        // Create instances of the Person class with sample data
        Person person1 = new Person(25, true, "Alice", 5.6);
        Person person2 = new Person(30, false, "Bob", 6.0);
        Person person3 = new Person(18, true, "Charlie", 5.8);
        Person person4 = new Person(40, false, "David", 5.9);
        Person person5 = new Person(22, true, "Eve", 5.7);

        // Access and display information about the persons
        System.out.println("Person 1:");
        System.out.println("Name: " + person1.getName());
        System.out.println("Age: " + person1.getAge());
        System.out.println("Is a student: " + person1.isStudent());
        System.out.println("Height: " + person1.getHeight() + " feet\n");

        System.out.println("Person 3:");
        System.out.println("Name: " + person3.getName());
        System.out.println("Age: " + person3.getAge());
        System.out.println("Is a student: " + person3.isStudent());
        System.out.println("Height: " + person3.getHeight() + " feet");
    }
}
Person.main(null)
Person 1:
Name: Alice
Age: 25
Is a student: true
Height: 5.6 feet

Person 3:
Name: Charlie
Age: 18
Is a student: true
Height: 5.8 feet

hack numero cuarto (chatgpt)

public class PersonName {
    private String fullName;

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

    // Method to get the person's first name
    public String getFirstName() {
        StringBuilder firstName = new StringBuilder();

        // Iterate through the characters in the full name
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);

            // Stop when we encounter a space
            if (c == ' ') {
                break;
            }

            // Append the character to the first name
            firstName.append(c);
        }

        return firstName.toString();
    }

    // Method to get the person's last name
    public String getLastName() {
        StringBuilder lastName = new StringBuilder();
        boolean foundSpace = false;

        // Iterate through the characters in the full name
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);

            // Start appending characters when we encounter a space
            if (c == ' ') {
                foundSpace = true;
            } else if (foundSpace) {
                lastName.append(c);
            }
        }

        return lastName.toString();
    }

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

        // Get and print the first name
        String firstName = person.getFirstName();
        System.out.println("First Name: " + firstName);

        // Get and print the last name
        String lastName = person.getLastName();
        System.out.println("Last Name: " + lastName);
    }
}
PersonName.main(null)
First Name: John
Last Name: Doe