import java.util.ArrayList;
public class ArrayListExample {
private ArrayList<Integer> numbers;
public ArrayListExample() {
numbers = new ArrayList<>();
}
public void addToArrayList(int number) {
numbers.add(number);
}
public int getFromArrayList(int index) {
return numbers.get(index);
}
public static void main(String[] args) {
ArrayListExample example = new ArrayListExample();
example.addToArrayList(10);
example.addToArrayList(20);
example.addToArrayList(30);
int value = example.getFromArrayList(1);
System.out.println("Value at index 1: " + value);
}
}
The ArrayListExample class has an instance variable numbers of type ArrayList
In the main method, we create an instance of ArrayListExample and add three integers to the ArrayList using the addToArrayList method. Then, we call the getFromArrayList method to retrieve the value at index 1 and print it. The output of this code will be: “Value at index 1: 20”.
2.4-2.5
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 GuessingGame {
private static Random random = new Random();
private double number;
private double result;
public GuessingGame() {
number = random.nextDouble() * 10; // Generates a random number between 0 and 10
int operation = random.nextInt(2); // Randomly selects either 0 (exponent) or 1 (root)
if (operation == 0) {
int exponent = random.nextInt(4) + 2; // Generates a random exponent between 2 and 5
result = Math.pow(number, exponent);
System.out.println("The number is raised to the power of " + exponent);
} else {
int root = random.nextInt(4) + 2; // Generates a random root between 2 and 5
result = Math.pow(number, 1.0 / root);
System.out.println("The number is the " + root + "th root");
}
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter your guess for the number: ");
double guess = scanner.nextDouble();
if (guess == result) {
System.out.println("You found the correct root/exponent.");
break;
} else {
if (guess < result) {
System.out.println("too low");
} else {
System.out.println("too high");
}
}
}
}
public static void main(String[] args) {
GuessingGame game = new GuessingGame();
game.playGame();
}
}
In this example, the GuessingGame class uses a static random object from the java.util.Random class to generate random numbers. It has a number variable to store the randomly generated number and a result variable to store the number after applying a random exponent or root.
The constructor initializes the number and result variables by randomly selecting an operation (exponent or root) and generating a corresponding exponent or root. The result is calculated based on the operation and a hint is displayed to the user.
The playGame method takes user input using a Scanner object and compares it with the result. If the guess is correct, the game ends. Otherwise, it provides hints on whether the guess is too low or too high and prompts the user to guess again.
In the main method, an instance of GuessingGame is created and the playGame method is called to start the guessing game.
2.6-2.7
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 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) {
Person person1 = new Person(16, true, "Orlando", 5.4);
Person person2 = new Person(16, false, "Soham", 5.6);
Person person3 = new Person(16, true, "Aniket", 5.8);
Person person4 = new Person(16, false, "Kevin", 5.5);
Person person5 = new Person(16, true, "Colin", 5.10);
// Accessing information for person1
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());
// Accessing information for person3
System.out.println("\nPerson 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());
}
}
In this example, the Person class represents a person with attributes such as age, isStudent, name, and height. The constructor initializes these attributes based on the provided parameters.
The class also includes getter methods (getAge(), isStudent(), getName(), getHeight()) to access the information stored in the object.
In the main method, we create five instances of the Person class (person1, person2, person3, person4, and person5) with different data values.
To access the information, we demonstrate how to retrieve and display the attributes for person1 and person3 using the getter methods. We print out their names, ages, student status, and heights.
2.8-2.9
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 PersonName {
private String fullName;
public PersonName(String fullName) {
this.fullName = fullName;
}
public String getFirstName() {
StringBuilder firstName = new StringBuilder();
for (int i = 0; i < fullName.length(); i++) {
if (fullName.charAt(i) == ' ') {
break;
}
firstName.append(fullName.charAt(i));
}
return firstName.toString();
}
public String getLastName() {
StringBuilder lastName = new StringBuilder();
boolean foundSpace = false;
for (int i = 0; i < fullName.length(); i++) {
if (foundSpace) {
lastName.append(fullName.charAt(i));
}
if (fullName.charAt(i) == ' ') {
foundSpace = true;
}
}
return lastName.toString();
}
public static void main(String[] args) {
PersonName person = new PersonName("Billy Goat");
System.out.println("First Name: " + person.getFirstName());
System.out.println("Last Name: " + person.getLastName());
}
}
In this example, the PersonName class represents a person’s full name. The constructor initializes the fullName attribute with the provided name.
The getFirstName method iterates through the characters of the fullName string using a for loop. It appends each character to a StringBuilder until it encounters a space character. It then returns the first name as a string.
The getLastName method iterates through the characters of the fullName string using a for loop. It appends each character to a StringBuilder only after encountering a space character. This ensures that it appends characters after the space, representing the last name. It then returns the last name as a string.
In the main method, we create an instance of the PersonName class with the full name “Billy Goat”. We then call the getFirstName and getLastName methods to retrieve and print the person’s first and last names respectively.