1.10 Lesson Homework
Lesson 1.10 Homework
Popcorn Hack 1
import java.util.ArrayList;
public class PopcornHack1 {
public static void main(String[] args) {
// Use Math.pow() to calculate 3^4
double power = Math.pow(3, 4);
System.out.println("3^4 = " + power);
// Use Math.sqrt() to find square root of 64
double sqrt = Math.sqrt(64);
System.out.println("Square root of 64 = " + sqrt);
// Create ArrayList of Strings
ArrayList<String> colors = new ArrayList<>();
// Add 3 colors
colors.add("red");
colors.add("blue");
colors.add("green");
// Print the size
System.out.println("Number of colors in the list = " + colors.size());
}
}
PopcornHack1.main(null);
3^4 = 81.0
Square root of 64 = 8.0
Number of colors in the list = 3
Popcorn Hack 2
public class Book {
private String title;
private String author;
private int pages;
// Constructor to set all attributes
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// Method to display all book info
public void displayInfo() {
System.out.println("Book Title: " + title);
System.out.println("Author: " + author);
System.out.println("Pages: " + pages);
}
// Method to check if the book is long
public boolean isLong() {
return pages > 300;
}
}
// --- Test the Book class ---
Book myBook = new Book("Java Basics", "Ahaan Vaidyanathan", 350);
// Call displayInfo()
myBook.displayInfo();
System.out.println("Is the book long? " + myBook.isLong());
Book Title: Java Basics
Author: Ahaan Vaidyanathan
Pages: 350
Is the book long? true
Homework Hack
import java.util.ArrayList;
// --- Phone class ---
public class Phone {
// Attributes
private String brand;
private String model;
private int batteryLevel;
private ArrayList<String> contacts;
// Constructor
public Phone(String brand, String model) {
this.brand = brand;
this.model = model;
this.batteryLevel = 100; // starts at 100
this.contacts = new ArrayList<>();
}
// Display brand, model, and battery
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Battery Level: " + batteryLevel + "%");
}
// Add a contact
public void addContact(String name) {
contacts.add(name);
}
// Show all contacts
public void showContacts() {
System.out.println("Contacts: " + contacts);
}
// Use phone for some minutes
public void usePhone(int minutes) {
batteryLevel -= minutes;
if (batteryLevel < 0) batteryLevel = 0;
}
}
// --- Test Phone class ---
public class PhoneTest {
public static void main(String[] args) {
// Create 2 Phone objects
Phone phone1 = new Phone("Apple", "iPhone 15");
Phone phone2 = new Phone("Samsung", "Galaxy S23");
// Add 3 contacts to each phone
phone1.addContact("Ahaan");
phone1.addContact("Bella");
phone1.addContact("Rudransh");
phone2.addContact("Arnav");
phone2.addContact("Nikhil");
phone2.addContact("Xavier");
// Use phones for some minutes
phone1.usePhone(20);
phone2.usePhone(45);
// Display all information
System.out.println("--- Phone 1 Info ---");
phone1.displayInfo();
phone1.showContacts();
System.out.println("\n--- Phone 2 Info ---");
phone2.displayInfo();
phone2.showContacts();
}
}
PhoneTest.main(null);
--- Phone 1 Info ---
Brand: Apple
Model: iPhone 15
Battery Level: 80%
Contacts: [Ahaan, Bella, Rudransh]
--- Phone 2 Info ---
Brand: Samsung
Model: Galaxy S23
Battery Level: 55%
Contacts: [Arnav, Nikhil, Xavier]