Skip to the content.

API

Popcorn Hack 1

import java.util.ArrayList;

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

        // 2. Use Math.sqrt() to find square root of 64
        double sqrt = Math.sqrt(64);    // √64 = 8
        System.out.println("√64 = " + sqrt);

        // 3. Create ArrayList of Strings
        ArrayList<String> colors = new ArrayList<>();

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

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

PopcornHack1.main(null);
3^4 = 81.0
√64 = 8.0
Number of colors: 3
Colors: [red, blue, green]

Popcorn Hack 2

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

    // 2. Constructor
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

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

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

    // Testing the Book class
    public static void main(String[] args) {
        Book myBook = new Book("Java Basics", "John Doe", 350);

        // Call all methods
        myBook.displayInfo();
        System.out.println("Is this book long? " + myBook.isLong());
    }
}

Book.main(null);
Title: Java Basics
Author: John Doe
Pages: 350
Is this book long? true

Homework Hack

import java.util.ArrayList;

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

    // Constructor - sets brand, model, initializes battery and contacts
    public Phone(String brand, String model) {
        this.brand = brand;
        this.model = model;
        this.batteryLevel = 100;            // starts at 100
        this.contacts = new ArrayList<>();   // empty contacts list
    }

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

    // addContact - adds a name to contacts list
    public void addContact(String name) {
        contacts.add(name);
    }

    // showContacts - prints all contacts
    public void showContacts() {
        System.out.println("Contacts: " + contacts);
    }

    // usePhone - decreases battery by minutes used
    public void usePhone(int minutes) {
        batteryLevel -= minutes;
        if (batteryLevel < 0) batteryLevel = 0;
        System.out.println("Used phone for " + minutes + " minutes. Battery now at " + batteryLevel + "%");
    }
}

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

        // Add 3 contacts to each
        phone1.addContact("Alice");
        phone1.addContact("Bob");
        phone1.addContact("Charlie");

        phone2.addContact("David");
        phone2.addContact("Eve");
        phone2.addContact("Frank");

        // Use phones for some minutes
        phone1.usePhone(30);
        phone2.usePhone(45);

        // Display all information
        System.out.println("\nPhone 1 Info:");
        phone1.displayInfo();
        phone1.showContacts();

        System.out.println("\nPhone 2 Info:");
        phone2.displayInfo();
        phone2.showContacts();
    }
}

PhoneTest.main(null);
Used phone for 30 minutes. Battery now at 70%
Used phone for 45 minutes. Battery now at 55%

Phone 1 Info:
Brand: Apple
Model: iPhone 14
Battery Level: 70%
Contacts: [Alice, Bob, Charlie]

Phone 2 Info:
Brand: Samsung
Model: Galaxy S23
Battery Level: 55%
Contacts: [David, Eve, Frank]