Skip to the content.

Big Idea 1 Lesson 1.7

Big Idea 1 Lesson 1.7

Popcorn Hacks

Popcorn Hack 1

import java.util.ArrayList;

public class PopcornHack1 {
    public static void main(String[] args) {
        // TODO: Use Math.pow() to calculate 3^4
        double power = Math.pow(3, 4);
        System.out.println("3^4 = " + power);
        
        // TODO: Use Math.sqrt() to find square root of 64
        double squareRoot = Math.sqrt(64);
        System.out.println("Square root of 64 = " + squareRoot);
        
        // TODO: Create ArrayList of Strings
        ArrayList<String> colors = new ArrayList<String>();
        
        // TODO: Add 3 colors ("red", "blue", "green")
        colors.add("red");
        colors.add("blue");
        colors.add("green");
        
        // TODO: Print the size
        System.out.println("ArrayList size: " + colors.size());
    }
}

PopcornHack1.main(null);
3^4 = 81.0
Square root of 64 = 8.0
ArrayList size: 3

Popcorn Hack 2

public class Book {
    // TODO: Add 3 attributes (title, author, pages)
    private String title;
    private String author;
    private int pages;
    
    // TODO: Add constructor
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    // TODO: Add displayInfo() method
    public void displayInfo() {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Pages: " + pages);
    }
    
    // TODO: Add isLong() method (returns true if pages > 300)
    public boolean isLong() {
        return pages > 300;
    }
    
    // Main method for testing
    public static void main(String[] args) {
        // TODO: Create a Book object and test all methods
        Book myBook = new Book("Java Basics", "John Doe", 350);
        
        System.out.println("Book Information:");
        myBook.displayInfo();
        
        System.out.println("\nIs this book long? " + myBook.isLong());
        
        System.out.println("\n--- Testing another book ---");
        Book shortBook = new Book("Quick Guide", "Jane Smith", 150);
        shortBook.displayInfo();
        System.out.println("\nIs this book long? " + shortBook.isLong());
    }
}

Book.main(null);
Book Information:
Title: Java Basics
Author: John Doe
Pages: 350

Is this book long? true

--- Testing another book ---
Title: Quick Guide
Author: Jane Smith
Pages: 150

Is this book long? false

Homework Hacks

import java.util.ArrayList;

public class Phone {
    // TODO: Add 4 attributes
    private String brand;
    private String model;
    private int batteryLevel;
    private ArrayList<String> contacts;
    
    // TODO: Add constructor
    public Phone(String brand, String model) {
        this.brand = brand;
        this.model = model;
        this.batteryLevel = 100;
        this.contacts = new ArrayList<String>();
    }
    
    // TODO: Add displayInfo() method
    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Battery Level: " + batteryLevel + "%");
    }
    
    // TODO: Add addContact(String name) method
    public void addContact(String name) {
        contacts.add(name);
        System.out.println("Added contact: " + name);
    }
    
    // TODO: Add showContacts() method
    public void showContacts() {
        System.out.println("Contacts (" + contacts.size() + "):");
        for (String contact : contacts) {
            System.out.println("  - " + contact);
        }
    }
    
    // TODO: Add usePhone(int minutes) method
    public void usePhone(int minutes) {
        batteryLevel -= minutes;
        if (batteryLevel < 0) {
            batteryLevel = 0;
        }
        System.out.println("Used phone for " + minutes + " minutes. Battery now at " + batteryLevel + "%");
    }
}

class PhoneTest {
    public static void main(String[] args) {
        // TODO: Create 2 Phone objects
        System.out.println("=== Creating Phone 1 ===");
        Phone phone1 = new Phone("Apple", "iPhone 15");
        
        System.out.println("\n=== Creating Phone 2 ===");
        Phone phone2 = new Phone("Samsung", "Galaxy S24");
        
        // TODO: Add 3 contacts to each
        System.out.println("\n=== Adding Contacts to Phone 1 ===");
        phone1.addContact("Alice");
        phone1.addContact("Bob");
        phone1.addContact("Charlie");
        
        System.out.println("\n=== Adding Contacts to Phone 2 ===");
        phone2.addContact("David");
        phone2.addContact("Emma");
        phone2.addContact("Frank");
        
        // TODO: Use phones for some minutes
        System.out.println("\n=== Using Phones ===");
        phone1.usePhone(25);
        phone2.usePhone(40);
        
        // TODO: Display all information
        System.out.println("\n=== Phone 1 Information ===");
        phone1.displayInfo();
        phone1.showContacts();
        
        System.out.println("\n=== Phone 2 Information ===");
        phone2.displayInfo();
        phone2.showContacts();
    }
}

PhoneTest.main(null);
=== Creating Phone 1 ===

=== Creating Phone 2 ===

=== Adding Contacts to Phone 1 ===
Added contact: Alice
Added contact: Bob
Added contact: Charlie

=== Adding Contacts to Phone 2 ===
Added contact: David
Added contact: Emma
Added contact: Frank

=== Using Phones ===
Used phone for 25 minutes. Battery now at 75%
Used phone for 40 minutes. Battery now at 60%

=== Phone 1 Information ===
Brand: Apple
Model: iPhone 15
Battery Level: 75%
Contacts (3):
  - Alice
  - Bob
  - Charlie

=== Phone 2 Information ===
Brand: Samsung
Model: Galaxy S24
Battery Level: 60%
Contacts (3):
  - David
  - Emma
  - Frank