Popcorn Hack 1
import java.util.ArrayList;
public class PopcornHack1 {
public static void main(String[] args) {
// Use Math.pow() to calculate 3^4
double powerResult = Math.pow(3, 4);
System.out.println("3^4 = " + powerResult);
// Use Math.sqrt() to find the square root of 64
double squareRootResult = Math.sqrt(64);
System.out.println("√64 = " + squareRootResult);
// Create an ArrayList of Strings
ArrayList<String> colors = new ArrayList<>();
// Add 3 colors ("red", "blue", "green")
colors.add("red");
colors.add("blue");
colors.add("green");
// Print the size of the ArrayList
System.out.println("Number of colors in the list: " + colors.size());
}
}
PopcornHack1.main(null);
3^4 = 81.0
√64 = 8.0
Number of colors in the list: 3
Popcorn Hack 2
public class Book {
// Attributes
String title;
String author;
int pages;
// Constructor to set all attributes
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// displayInfo() method - prints all book info
public void displayInfo() {
System.out.println("Book Information:");
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Pages: " + pages);
}
// isLong() method - returns true if pages > 300
public boolean isLong() {
return pages > 300;
}
// Main method to test Book class
public static void main(String[] args) {
// Create a Book object
Book myBook = new Book("Java Basics", "John Doe", 350);
// Call displayInfo()
myBook.displayInfo();
// Call isLong() and print the result
System.out.println("Is this book long? " + myBook.isLong());
}
}
Book.main(null);
Book Information:
Title: Java Basics
Author: John Doe
Pages: 350
Is this book long? true
Homework Hack
import java.util.ArrayList;
// Phone Class
public class Phone {
// Attributes
String brand;
String model;
int batteryLevel;
ArrayList<String> contacts;
// Constructor - sets brand, model, and initializes battery and contacts list
public Phone(String brand, String model) {
this.brand = brand;
this.model = model;
this.batteryLevel = 100; // starts at 100%
this.contacts = new ArrayList<>(); // empty contact list
}
// displayInfo() - print phone info
public void displayInfo() {
System.out.println("Phone Info:");
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Battery Level: " + batteryLevel + "%");
}
// addContact(String name) - adds name to contacts
public void addContact(String name) {
contacts.add(name);
System.out.println("👤 Contact added: " + name);
}
// showContacts() - print all contacts
public void showContacts() {
System.out.println("Contacts for " + brand + " " + model + ":");
if (contacts.isEmpty()) {
System.out.println("(No contacts saved)");
} else {
for (String contact : contacts) {
System.out.println("- " + contact);
}
}
}
// usePhone(int minutes) - decrease battery
public void usePhone(int minutes) {
batteryLevel -= minutes;
if (batteryLevel < 0) {
batteryLevel = 0; // prevent negative battery
}
System.out.println("Used phone for " + minutes + " minutes. Battery now: " + batteryLevel + "%");
}
}
// Phone Test Class
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 S24");
// Add 3 contacts to each phone
phone1.addContact("Nolan");
phone1.addContact("Yash");
phone1.addContact("Yuva");
phone2.addContact("Nora");
phone2.addContact("Soni");
phone2.addContact("Avika");
System.out.println();
// Use phones for some minutes
phone1.usePhone(25);
phone2.usePhone(40);
System.out.println();
// Display all information
phone1.displayInfo();
phone1.showContacts();
System.out.println();
phone2.displayInfo();
phone2.showContacts();
}
}
PhoneTest.main(null);
👤 Contact added: Nolan
👤 Contact added: Yash
👤 Contact added: Yuva
👤 Contact added: Nora
👤 Contact added: Soni
👤 Contact added: Avika
Used phone for 25 minutes. Battery now: 75%
Used phone for 40 minutes. Battery now: 60%
Phone Info:
Brand: Apple
Model: iPhone 15
Battery Level: 75%
Contacts for Apple iPhone 15:
- Nolan
- Yash
- Yuva
Phone Info:
Brand: Samsung
Model: Galaxy S24
Battery Level: 60%
Contacts for Samsung Galaxy S24:
- Nora
- Soni
- Avika