Hack 1
import java.util.ArrayList;
import java.util.Scanner;
public class NumberListDemo {
private ArrayList<Integer> numbers = new ArrayList<>();
// Method to add a number to the list
public void addNumber(int num) {
numbers.add(num);
}
// Method to retrieve a number from the list by its index
public int getNumberAtIndex(int idx) {
if (idx >= 0 && idx < numbers.size()) {
return numbers.get(idx);
} else {
return -1; // Return -1 if the index is out of bounds
}
}
public static void main(String[] args) {
NumberListDemo demo = new NumberListDemo();
Scanner inputScanner = new Scanner(System.in);
// Adding some sample numbers to the list
demo.addNumber(101);
demo.addNumber(102);
demo.addNumber(103);
System.out.println("Please provide an index to retrieve the number: ");
int userInput = inputScanner.nextInt();
int retrievedNumber = demo.getNumberAtIndex(userInput);
if (retrievedNumber != -1) {
System.out.println("Number at index " + userInput + ": " + retrievedNumber + ". Well done!");
} else {
System.out.println("Oops! That's an invalid index. Please try again.");
}
inputScanner.close();
}
}
NumberListDemo.main(null);
Hack 2
import java.util.Random;
import java.util.Scanner;
public class ExponentChallenge {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
Random randGenerator = new Random();
int lowerLimit = 1;
int upperLimit = 10;
int randomBase = randGenerator.nextInt(upperLimit - lowerLimit + 1) + lowerLimit; // generates random base number
int power = randGenerator.nextInt(5) + 1; // generates random power
System.out.println("Try to guess the base number and its power for a number between " + lowerLimit + " and " + upperLimit + " (Clues will be given):");
int tries = 0;
while (true) {
System.out.println("Input your guess for the BASE number: ");
int basePrediction = userInput.nextInt();
System.out.println("Input your guess for the POWER: ");
int powerPrediction = userInput.nextInt();
if (basePrediction == randomBase && powerPrediction == power) {
System.out.println("Well done! You've correctly identified the base number and its power.");
break;
} else {
tries++;
System.out.println("That's not right. Here are some clues: ");
System.out.println("Clue 1: The base number lies between " + lowerLimit + " and " + upperLimit);
System.out.println("Clue 2: The power is " + power);
System.out.println("Number of tries: " + tries);
}
}
userInput.close();
}
}
ExponentChallenge.main(null);
Input your guess for the POWER:
Hack 3
import java.util.Scanner;
public class DataRetriever {
private int numValue;
private boolean flagValue;
private String textValue;
private double floatNumValue;
public DataRetriever(int numValue, boolean flagValue, String textValue, double floatNumValue) {
this.numValue = numValue;
this.flagValue = flagValue;
this.textValue = textValue;
this.floatNumValue = floatNumValue;
}
public static void main(String[] args) {
// Create a sample instance for demonstration
DataRetriever instance = new DataRetriever(420, false, "Hello, World!", 3.141592653589);
Scanner inputScanner = new Scanner(System.in);
// Prompt the user to choose a data type and display the corresponding value
System.out.println("Choose the data type (int, boolean, string, double): ");
String chosenType = inputScanner.nextLine();
// Display the value based on the chosen data type
switch (chosenType.toLowerCase()) {
case "int":
System.out.println("Integer Value: " + instance.numValue);
break;
case "boolean":
System.out.println("Flag Value: " + instance.flagValue);
break;
case "string":
System.out.println("Text Value: " + instance.textValue);
break;
case "double":
System.out.println("Floating Number Value: " + instance.floatNumValue);
break;
default:
System.out.println("Data type not recognized.");
}
inputScanner.close();
}
}
DataRetriever.main(null);
Choose the data type (int, boolean, string, double):
Data type not recognized.
Hack 4
public class Individual {
private String completeName;
public Individual(String completeName) {
this.completeName = completeName;
}
public String extractFirstName() {
StringBuilder fName = new StringBuilder();
// Loop through each character until a space is found, which indicates the end of the first name
for (int index = 0; index < completeName.length(); index++) {
char character = completeName.charAt(index);
if (character == ' ') {
break;
}
fName.append(character);
}
return fName.toString();
}
public String extractLastName() {
StringBuilder lName = new StringBuilder();
boolean spaceDetected = false;
for (int index = 0; index < completeName.length(); index++) {
char character = completeName.charAt(index);
if (spaceDetected) {
lName.append(character);
}
if (character == ' ') {
spaceDetected = true;
}
}
return lName.toString();
}
public static void main(String[] args) {
// Sample name for demonstration
Individual individual = new Individual("Kaiden Do");
String fName = individual.extractFirstName();
String lName = individual.extractLastName();
System.out.println("First Name: " + fName);
System.out.println("Last Name: " + lName);
}
}
Individual.main(null);
First Name: Kaiden
Last Name: Do
Overall, I thought the hacks were pretty good for helping solidify the Unit 2 content from the lesson. I felt that the second hack was easily the hardest one, simply because it was difficult to get the random part of it working properly.