Skip to the content.

Lesson 1.7 - Rohan Bojja

// Popcorn Hack #1: Complete this code

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 squareRoot = Math.sqrt(64);
        System.out.println("√64 = " + squareRoot);

        // 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 of the ArrayList
        System.out.println("# of colors: " + colors.size());
    }
}

PopcornHack1.main(null);

3^4 = 81.0
√64 = 8.0
# of colors: 3
// Popcorn Hack #2: Complete the Book class

public class Book {

    private String title;
    private String author;
    private int pages;

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

    public void displayInfo() {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Pages: " + pages);
    }

    public boolean isLong() {
        return pages > 300;
    }

    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());
    }
}

Book.main(null);

Title: Java Basics
Author: John Doe
Pages: 350
Is this book long? true
// Homework Hack: Complete the Phone class

import java.util.ArrayList;

public class Phone {

    private String brand;
    private String model;
    private double battery;
    private ArrayList<String> contacts;

    public Phone(String brand, String model, double battery) {
        this.brand = brand;
        this.model = model;
        this.battery = battery;
        this.contacts = new ArrayList<>();
    }

    public void displayInfo() {
        System.out.println(brand + " " + model + " - Battery: " + battery + "%");
    }

    public void addContact(String name) {
        contacts.add(name);
        System.out.println(name + " added to contacts on " + model);
    }

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

    public void usePhone(int minutes) {
        double drain = minutes * 0.5; 
        battery -= drain;
        if (battery < 0) battery = 0;
        System.out.println(model + " used for " + minutes + " min. Battery now: " + battery + "%");
    }
}

public class PhoneTest {
    public static void main(String[] args) {
        Phone phone1 = new Phone("Apple", "iPhone 2000", 40);
        Phone phone2 = new Phone("Samsung", "iPhone 100000", 3);

        phone1.addContact("Rohan");
        phone1.addContact("Pranav");
        phone1.addContact("Nikhil");

        phone2.addContact("Bob");
        phone2.addContact("Smith");
        phone2.addContact("JOE");

        phone1.usePhone(30);
        phone2.usePhone(50);

        phone1.displayInfo();
        phone1.showContacts();

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

PhoneTest.main(null);

Rohan added to contacts on iPhone 2000
Pranav added to contacts on iPhone 2000
Nikhil added to contacts on iPhone 2000
Bob added to contacts on iPhone 100000
Smith added to contacts on iPhone 100000
JOE added to contacts on iPhone 100000
iPhone 2000 used for 30 min. Battery now: 25.0%
iPhone 100000 used for 50 min. Battery now: 0.0%
Apple iPhone 2000 - Battery: 25.0%
iPhone 2000 Contacts: [Rohan, Pranav, Nikhil]
Samsung iPhone 100000 - Battery: 0.0%
iPhone 100000 Contacts: [Bob, Smith, JOE]