Hack 1
import java.util.ArrayList;
public class ArrayListExample {
private ArrayList<Integer> myArrayList = new ArrayList<>();
public void addToArrayList(int value) {
myArrayList.add(value);
}
public int getElementAtIndex(int index) {
if (index >= 0 && index < myArrayList.size()) {
return myArrayList.get(index);
} else {
throw new IndexOutOfBoundsException("Index is out of bounds.");
}
}
public static void main(String[] args) {
ArrayListExample example = new ArrayListExample();
example.addToArrayList(10);
example.addToArrayList(20);
example.addToArrayList(30);
int elementAtIndex = example.getElementAtIndex(1);
System.out.println("Element at index 1: " + elementAtIndex);
}
}
ArrayListExample.main(null)
Element at index 1: 20
Hack 2
import java.util.Random;
import java.util.Scanner;
public class SimpleMathGuessingGame {
public static void main(String[] args) {
Random random = new Random();
int base = random.nextInt(9) + 2; // random base between 2 and 10 (inclusive)
int exponent = random.nextInt(4) + 2; // random exponent between 2 and 5 (inclusive)
double result = Math.pow(base, exponent);
Scanner scanner = new Scanner(System.in);
int attempts = 0;
boolean guessed = false;
System.out.println("Welcome to the Simple Math Guessing Game!");
System.out.println("I have a number in mind, and you need to guess both the base and exponent.");
while (!guessed) {
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();
attempts++;
if (guessedBase == base && guessedExponent == exponent) {
guessed = true;
System.out.println("Congratulations! You guessed it!");
System.out.println("It took you " + attempts + " attempts.");
} else {
System.out.println("Sorry, that's not correct. Try again!");
}
}
scanner.close();
}
}
SimpleMathGuessingGame.main(null)
Welcome to the Simple Math Guessing Game!
I have a number in mind, and you need to guess both the base and exponent.
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base: Enter your guess for the exponent: Sorry, that's not correct. Try again!
Enter your guess for the base:
Hack 3
public class MyClass {
private int intValue;
private boolean boolValue;
private String stringValue;
private double doubleValue;
public MyClass(int intValue, boolean boolValue, String stringValue, double doubleValue) {
this.intValue = intValue;
this.boolValue = boolValue;
this.stringValue = stringValue;
this.doubleValue = doubleValue;
}
public int getIntValue() {
return intValue;
}
public boolean getBoolValue() {
return boolValue;
}
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.getBoolValue());
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 PersonName {
private String fullName;
public PersonName(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) {
return fullName.substring(spaceIndex + 1);
} else {
return "";
}
}
public static void main(String[] args) {
PersonName person = new PersonName("Parav Salaniwal");
System.out.println("First Name: " + person.getFirstName());
System.out.println("Last Name: " + person.getLastName());
}
}
PersonName.main(null)
First Name: Parav
Last Name: Salaniwal