CSA Unit 1.7

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 square root of 64
        double sqrtResult = Math.sqrt(64);
        System.out.println("Square root of 64 = " + sqrtResult);

        // Create 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
        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 {
    // 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 all book info
    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
    public static void main(String[] args) {
        Book myBook = new Book("Java Basics", "John Doe", 350);
        myBook.displayInfo();
        System.out.println("Is the book long? " + myBook.isLong());
    }
}

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

Homework Hack

import java.util.ArrayList;

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

    // Constructor
    public Phone(String brand, String model) {
        this.brand = brand;
        this.model = model;
        this.batteryLevel = 100;
        this.contacts = new ArrayList<>();
    }

    // Display phone info
    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Battery Level: " + batteryLevel + "%");
    }

    // Add a contact
    public void addContact(String name) {
        contacts.add(name);
        System.out.println(name + " added to contacts.");
    }

    // Show all contacts
    public void showContacts() {
        System.out.println("Contacts for " + model + ": " + contacts);
    }

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

// Test class
public 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
        phone1.addContact("Alice");
        phone1.addContact("Bob");
        phone1.addContact("Charlie");

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

        // Use phones
        phone1.usePhone(20);
        phone2.usePhone(45);

        // Display info
        System.out.println("\n--- Phone 1 Info ---");
        phone1.displayInfo();
        phone1.showContacts();

        System.out.println("\n--- Phone 2 Info ---");
        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 20 minutes. Battery now: 80%
Used phone for 45 minutes. Battery now: 55%

--- Phone 1 Info ---
Brand: Apple
Model: iPhone 15
Battery Level: 80%
Contacts for iPhone 15: [Alice, Bob, Charlie]

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