Popcorn Hack #1

// 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
        Math.pow(3, 4);
        System.out.println(Math.pow(3, 4));
        // TODO: Use Math.sqrt() to find square root of 64
        Math.sqrt(64);
        System.out.println(Math.sqrt(64));
        // 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(colors.size());
        
    }
}

PopcornHack1.main(null);
81.0
8.0
3

Popcorn Hack #2: Attributes and Behaviors

// Popcorn Hack #2: Complete the Book class

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

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

    // Method to display book information
    public void displayInfo() {
        System.out.println("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;
    }

    // Main method to test the Book class
    public static void main(String[] args) {
        // Create a Book object and test all methods
        Book myBook = new Book("Java Basics", "John Doe", 350);
        myBook.displayInfo();
        System.out.println("Is the book long? " + myBook.isLong());
    }
}

// Call the main method to execute the code
Book.main(null);
Title: Java Basics
Author: John Doe
Pages: 350
Is the book long? true

Homework Hack - Phone Class

// Homework Hack: Complete the Phone class

import java.util.ArrayList;

public class Phone {
    ArrayList<String> contacts;
    String model;
    String battery;
    int batteryLevel;

    public Phone(String model, String battery, int batteryLevel) {
        this.contacts = new ArrayList<>();
        this.model = model;
        this.battery = battery;
        this.batteryLevel = batteryLevel;
    }
    public Phone(String model, String battery) {
        this.contacts = new ArrayList<>();
        this.model = model;
        this.battery = battery;
        this.batteryLevel = 100; // Default battery level is 100%
    }
    void displayInfo() {
        System.out.println("Model: " + model);
        System.out.println("Battery: " + battery);
        System.out.println("Battery Level: " + batteryLevel + "%");
    }
    void addContact(String name) {
        contacts.add(name);
        System.out.println(name + " added to contacts.");
    }
    void showContacts() {
        System.out.println("Contacts: " + contacts);
    }
    void usePhone(int minutes) {
        int batteryUsage = minutes / 2; // Assume 1% battery used every 2 minutes
        batteryLevel -= batteryUsage;
        if (batteryLevel < 0) batteryLevel = 0;
        System.out.println("Used phone for " + minutes + " minutes. Battery level is now " + batteryLevel + "%.");
    }
}

// Test your Phone class
public class PhoneTest {
    public static void main(String[] args) {
        // Create 2 Phone objects
        Phone phone1 = new Phone("Model X", "Lithium-Ion", 80);
        Phone phone2 = new Phone("Model Y", "Nickel-Cadmium");

        // 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(50);

        // Display all information
        phone1.displayInfo();
        phone1.showContacts();

        phone2.displayInfo();
        phone2.showContacts();
    }
}

PhoneTest.main(null);
Alice added to contacts.
Bob added to contacts.
Charlie added to contacts.
David added to contacts.
Eve added to contacts.
Frank added to contacts.
Used phone for 30 minutes. Battery level is now 65%.
Used phone for 50 minutes. Battery level is now 75%.
Model: Model X
Battery: Lithium-Ion
Battery Level: 65%
Contacts: [Alice, Bob, Charlie]
Model: Model Y
Battery: Nickel-Cadmium
Battery Level: 75%
Contacts: [David, Eve, Frank]