Here is the list of the unit 2 hacks.
Note: This is due Monday! Write your hacks in a Jupyter Notebook and then DM them to us over slack by Monday Morning at 8:00 AM. For full credit, all criteria in the hacks must be completed and show some originality.
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.
import java.util.ArrayList;
public class ArrayListExample {
private ArrayList<Integer> integerList = new ArrayList<>();
// Void method to add an integer to the ArrayList
public void addToArrayList(int number) {
integerList.add(number);
}
// Non-void method to get an element at a specific index
public int getElementAtIndex(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();
// Adding integers to the ArrayList
example.addToArrayList(10);
example.addToArrayList(20);
example.addToArrayList(30);
// Retrieving an element at a specific index
int elementAtIndex = example.getElementAtIndex(1);
System.out.println("Element at index 1: " + elementAtIndex); // This will print "Element at index 1: 20"
}
}
ArrayListExample.main(null)
Element at index 1: 20
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.
import java.util.Random;
import java.util.Scanner;
public class MathGuessingGame {
private static Random random = new Random();
private int base;
private int exponent;
private double result;
// Non-static method to generate a random number and its exponent
private void generateRandomNumberAndExponent() {
base = random.nextInt(10) + 1; // Random base between 1 and 10
exponent = random.nextInt(5) + 2; // Random exponent between 2 and 6
result = Math.pow(base, exponent);
}
// Static method to provide a hint about the random number and exponent
private static void provideHint(int base, int exponent) {
System.out.println("Hint: The number is " + base + " raised to the power of " + exponent);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MathGuessingGame game = new MathGuessingGame();
System.out.println("Welcome to the Math Guessing Game!");
System.out.println("Try to guess the number!");
game.generateRandomNumberAndExponent();
System.out.print("Enter your guess for the base: ");
int guessedBase = scanner.nextInt();
System.out.print("Enter your guess for the exponent: ");
int guessedExponent = scanner.nextInt();
if (guessedBase == game.base && guessedExponent == game.exponent) {
System.out.println("Congratulations! You guessed it right. The number is " + game.result);
} else {
System.out.println("Sorry, your guess is incorrect.");
provideHint(game.base, game.exponent);
}
scanner.close();
}
}
MathGuessingGame.main(null)
Welcome to the Math Guessing Game!
Try to guess the number!
Enter your guess for the base: Enter your guess for the exponent: Sorry, your guess is incorrect.
Hint: The number is 7 raised to the power of 5
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 givng some samples.
public class Person {
private int age;
private boolean isStudent;
private String name;
private double heightInCm;
public Person(int age, boolean isStudent, String name, double heightInCm) {
this.age = age;
this.isStudent = isStudent;
this.name = name;
this.heightInCm = heightInCm;
}
public int getAge() {
return age;
}
public boolean isStudent() {
return isStudent;
}
public String getName() {
return name;
}
public double getHeightInCm() {
return heightInCm;
}
public static void main(String[] args) {
// Creating an instance of the Person class with sample data
Person person1 = new Person(25, true, "Alice", 165.5);
// Accessing and printing the information
System.out.println("Name: " + person1.getName());
System.out.println("Age: " + person1.getAge());
System.out.println("Is a student: " + person1.isStudent());
System.out.println("Height (in cm): " + person1.getHeightInCm());
}
}
Person.main(null)
Name: Alice
Age: 25
Is a student: true
Height (in cm): 165.5
Hack 4: (0.25)
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 PersonNameParser {
private String fullName;
public PersonNameParser(String fullName) {
this.fullName = fullName;
}
public String getFirstName() {
// Initialize an empty string to store the first name
String firstName = "";
// Iterate through the full name character by character
for (int i = 0; i < fullName.length(); i++) {
char currentChar = fullName.charAt(i);
// Break the loop when a space is encountered, indicating the end of the first name
if (currentChar == ' ') {
break;
}
// Append the current character to the first name
firstName += currentChar;
}
return firstName;
}
public String getLastName() {
// Initialize an empty string to store the last name
String lastName = "";
// Find the index of the last space in the full name
int lastSpaceIndex = fullName.lastIndexOf(' ');
// Iterate through the full name character by character, starting from the character after the last space
for (int i = lastSpaceIndex + 1; i < fullName.length(); i++) {
char currentChar = fullName.charAt(i);
// Append the current character to the last name
lastName += currentChar;
}
return lastName;
}
public static void main(String[] args) {
// Create an instance of PersonNameParser with a full name
PersonNameParser person = new PersonNameParser("John Doe");
// Call the methods to get the first name and last name
String firstName = person.getFirstName();
String lastName = person.getLastName();
// Print the extracted first name and last name
System.out.println("First Name: " + firstName); // Output: "First Name: John"
System.out.println("Last Name: " + lastName); // Output: "Last Name: Doe"
}
}
PersonNameParser.main(null)
First Name: John
Last Name: Doe