Skip to the content.

1.7 Application Program Interface (API) and Libraries

Popcorn Hack 1

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

import java.util.ArrayList;

public class PopcornHack1 {
    public static void main(String[] args) {
        // Calculate 3^4
        double power = Math.pow(3, 4);
        System.out.println("3^4 = " + power);
        
        // Square root of 64
        double sqrt = Math.sqrt(64);
        System.out.println("Square root of 64 = " + sqrt);
        
        // Create ArrayList
        ArrayList<String> colors = new ArrayList<>();
        
        // Add 3 colors
        colors.add("red");
        colors.add("blue");
        colors.add("green");
        
        // Print size
        System.out.println("Size: " + colors.size());
    }
}

PopcornHack1.main(null);
3^4 = 81.0
Square root of 64 = 8.0
Size: 3

Popcorn Hack 2

Task: Create a Book class with attributes and behaviors.

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;
    }
    
    // Display info
    public void displayInfo() {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Pages: " + pages);
    }
    
    // Check if long
    public boolean isLong() {
        return pages > 300;
    }
    
    // Test
    public static void main(String[] args) {
        Book myBook = new Book("Java Basics", "Soni Dhenuva", 350);
        myBook.displayInfo();
        System.out.println("Is long? " + myBook.isLong());
    }
}

Homework

Create a Phone class that demonstrates all concepts from this lesson: Libraries/Classes Packages (use ArrayList from java.util) Attributes Behaviors

import java.util.ArrayList;

public class Phone {
    // Attributes
    private String brand;
    private String model;
    private int battery;
    private ArrayList<String> contacts;
    
    // Constructor
    public Phone(String brand, String model) {
        this.brand = brand;
        this.model = model;
        this.battery = 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: " + battery + "%");
    }
    
    // Add contact
    public void addContact(String name) {
        contacts.add(name);
    }
    
    // Show all contacts
    public void showContacts() {
        System.out.println("Contacts: " + contacts);
    }
    
    // Use phone
    public void usePhone(int minutes) {
        battery = battery - minutes;
    }
}

// Test class
class PhoneTest {
    public static void main(String[] args) {
        // Create 2 phones
        Phone phone1 = new Phone("Apple", "iPhone");
        Phone phone2 = new Phone("Samsung", "Galaxy");
        
        // Add 3 contacts to each
        phone1.addContact("Percy");
        phone1.addContact("Annabeth");
        phone1.addContact("Grover");
        
        phone2.addContact("Harry");
        phone2.addContact("Hermione");
        phone2.addContact("Ron");
        
        // Use phones
        phone1.usePhone(10);
        phone2.usePhone(20);
        
        // Display info
        phone1.displayInfo();
        phone1.showContacts();
        System.out.println();
        phone2.displayInfo();
        phone2.showContacts();
    }
}

PhoneTest.main(null);
Brand: Apple
Model: iPhone
Battery: 90%
Contacts: [Percy, Annabeth, Grover]

Brand: Samsung
Model: Galaxy
Battery: 80%
Contacts: [Harry, Hermione, Ron]