🍿 Popcorn Hack #1: Using Documentation (5 minutes)

Task: Use the Math class and ArrayList class based on their documentation.

Complete the following:

  1. Use Math.pow() to calculate 3^4
  2. Use Math.sqrt() to find square root of 64
  3. Create an ArrayList of Strings
  4. Add 3 colors to the ArrayList
  5. Print the ArrayList size
// Popcorn Hack #1: Complete this code

import java.util.ArrayList;

public class PopcornHack1 {
    public static void main(String[] args) {
        // TODO: Use Math.pow() to calculate 3^4
        double threeToTheFourth = Math.pow(3, 4);
        System.out.println("3^4 = " + threeToTheFourth);

        // TODO: Use Math.sqrt() to find square root of 64
        double sqrt64 = Math.sqrt(64);
        System.out.println("sqrt(64) = " + sqrt64);

        // TODO: Create ArrayList of Strings
        ArrayList<String> colors = new ArrayList<>();

        // TODO: Add 3 colors ("red", "blue", "green")
        colors.add("red");
        colors.add("blue");
        colors.add("green");

        // TODO: Print the size
        System.out.println("Number of colors: " + colors.size());        
    }
}

PopcornHack1.main(null);
3^4 = 81.0
sqrt(64) = 8.0
Number of colors: 3


---

## 🍿 Popcorn Hack #2: Attributes and Behaviors (5 minutes)

**Task:** Create a `Book` class with attributes and behaviors.

**Requirements:**

**Attributes (3):**
- `title` (String)
- `author` (String)  
- `pages` (int)

**Behaviors (3 methods):**
1. Constructor to set all attributes
2. `displayInfo()` - print all book info
3. `isLong()` - return true if pages > 300

**Test:** Create a Book object and call all methods.
// Popcorn Hack #2: Complete the Book class

public class Book {
    // Attributes
    private String title;
    private String author;
    private int pages;

    // 1) Constructor to set all attributes
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    // 2) displayInfo() - print all book info
    public void displayInfo() {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Pages: " + pages);
    }

    // 3) isLong() - return true if pages > 300
    public boolean isLong() {
        return pages > 300;
    }

    // Test: create a Book and call all methods
    public static void main(String[] args) {
        Book myBook = new Book("Java Basics", "John Doe", 350);
        myBook.displayInfo();
        System.out.println("Is long? " + myBook.isLong());

        System.out.println();

        Book popcornHackBook = new Book("Popcorn Hack", "CSA", 20);
        popcornHackBook.displayInfo();
        System.out.println("Is long? " + popcornHackBook.isLong());
    }
}

Book.main(null)

Title: Java Basics
Author: John Doe
Pages: 350
Is long? true

Title: Popcorn Hack
Author: CSA
Pages: 20
Is long? false

📝 Homework Hack: Phone Class

Create a Phone class that demonstrates all concepts from this lesson:

  • Libraries/Classes
  • Packages (use ArrayList from java.util)
  • Attributes
  • Behaviors

Requirements:

Attributes (4):

  • brand (String)
  • model (String)
  • batteryLevel (int) - starts at 100
  • contacts (ArrayList)

Behaviors (5 methods):

  1. Constructor - sets brand and model, initializes empty contacts list
  2. displayInfo() - prints brand, model, and battery level
  3. addContact(String name) - adds name to contacts list
  4. showContacts() - prints all contacts
  5. usePhone(int minutes) - decreases battery by minutes used

Testing:

  • Create 2 Phone objects
  • Add 3 contacts to each phone
  • Use phone for some minutes
  • Display all information

Grading:

  • Correct attributes (4 points)
  • Correct methods (5 points)
  • Proper use of ArrayList from java.util (3 points)
  • Complete testing (3 points)
  • Total: 15 points
// Homework Hack: Complete the Phone class

import java.util.ArrayList;


public class Phone {
    // Attributes
    private String brand;
    private String model;
    private int batteryLevel; // starts at 100
    private ArrayList<String> contacts;

    // 1) Constructor - sets brand/model, initializes battery & contacts
    public Phone(String brand, String model) {
        this.brand = brand;
        this.model = model;
        this.batteryLevel = 100;
        this.contacts = new ArrayList<>();
    }

    // 2) displayInfo() - prints brand, model, battery
    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Battery: " + batteryLevel + "%");
    }

    // 3) addContact(String name) - adds to contacts list
    public void addContact(String name) {
        if (name != null && !name.isBlank()) {
            contacts.add(name.trim());
        }
    }

    // 4) showContacts() - prints all contacts
    public void showContacts() {
        if (contacts.isEmpty()) {
            System.out.println("Contacts: (none)");
            return;
        }
        System.out.println("Contacts:");
        for (int i = 0; i < contacts.size(); i++) {
            System.out.println("  " + (i + 1) + ". " + contacts.get(i));
        }
    }

    // 5) usePhone(int minutes) - decreases battery by minutes used (clamped 0–100)
    public void usePhone(int minutes) {
        if (minutes <= 0) return;
        batteryLevel -= minutes;
        if (batteryLevel < 0) batteryLevel = 0;
    }
}


public class PhoneTest {
    public static void main(String[] args) {
        // Create 2 Phone objects
        Phone phone1 = new Phone("Apple", "iPhone 16e");
        Phone phone2 = new Phone("Samsung", "Galaxy S24");

        // Add 3 contacts to each
        phone1.addContact("Risha");
        phone1.addContact("Ruta");
        phone1.addContact("Vibha");

        phone2.addContact("Anvay");
        phone2.addContact("Aadi");
        phone2.addContact("Neil");

        // Use phones for some minutes
        phone1.usePhone(34);
        phone2.usePhone(22);

        // Display all information
        System.out.println("Phone 1");
        phone1.displayInfo();
        phone1.showContacts();

        System.out.println();
        System.out.println("Phone 2");
        phone2.displayInfo();
        phone2.showContacts();
    }
}

PhoneTest.main(null);
Phone 1
Brand: Apple
Model: iPhone 16e
Battery: 66%
Contacts:
  1. Risha
  2. Ruta
  3. Vibha

Phone 2
Brand: Samsung
Model: Galaxy S24
Battery: 78%
Contacts:
  1. Anvay
  2. Aadi
  3. Neil