Application Program Interface (API) and Libraries Homework
Understanding Application Program Interfaces and Java Libraries - Homework
- 🍿 Popcorn Hack #1: Using Documentation (5 minutes)
- 🍿 Popcorn Hack #2: Attributes and Behaviors (5 minutes)
- 📝 Homework Hack: Phone Class
🍿 Popcorn Hack #1: Using Documentation (5 minutes)
Task: Use the Math class and ArrayList class based on their documentation.
Complete the following:
- Use
Math.pow()to calculate 3^4 - Use
Math.sqrt()to find square root of 64 - Create an ArrayList of Strings
- Add 3 colors to the ArrayList
- Print the ArrayList size
// 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
System.out.println("3 to the power of 4: " + Math.pow(3, 4));
// TODO: Use Math.sqrt() to find square root of 64
System.out.println("Square root of 64: " + 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("Number of colors: " + colors.size());
}
}
PopcornHack1.main(null);
3 to the power of 4: 81.0
Square root of 64: 8.0
Number of colors: 3
🍿 Popcorn Hack #2: Attributes and Behaviors (5 minutes)
Task: Create a Book class with attributes and behaviors.
Requirements:
Attributes (3):
title(String)author(String)pages(int)
Behaviors (3 methods):
- Constructor to set all attributes
displayInfo()- print all book infoisLong()- return true if pages > 300
Test: Create a Book object and call all methods.
// Popcorn Hack #2: Complete the Book class
public class Book {
// TODO: Add 3 attributes (title, author, pages)
private String title;
private String author;
private int pages;
// TODO: Add constructor
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// TODO: Add displayInfo() method
public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Pages: " + pages);
}
// TODO: Add isLong() method (returns true if pages > 300)
public boolean isLong() {
return pages > 300;
}
}
// Create a Book object and test all methods
Book myBook = new Book("Java Basics", "Nolan Hightower", 5678);
// Test the methods
myBook.displayInfo();
System.out.println("Is it a long book? " + myBook.isLong());
Title: Java Basics
Author: Nolan Hightower
Pages: 5678
Is it a long book? true
Author: Nolan Hightower
Pages: 5678
Is it a long book? true
📝 Homework Hack: Phone Class
Create a Phone class that demonstrates all concepts from this lesson:
- Libraries/Classes
- Packages (use ArrayList from java.util)
- Attributes
- Behaviors
Requirements:
Attributes (4):
brand(String)model(String)batteryLevel(int) - starts at 100contacts(ArrayList)
Behaviors (5 methods):
- Constructor - sets brand and model, initializes empty contacts list
displayInfo()- prints brand, model, and battery leveladdContact(String name)- adds name to contacts listshowContacts()- prints all contactsusePhone(int minutes)- decreases battery by minutes used
Testing:
- Create 2 Phone objects
- Add 3 contacts to each phone
- Use phone for some minutes
- Display all information
Grading:
- Correct attributes (4 points)
- Correct methods (5 points)
- Proper use of ArrayList from java.util (3 points)
- Complete testing (3 points)
- Total: 15 points
// Homework Hack: Complete the Phone class
import java.util.ArrayList;
public class Phone {
// TODO: Add 4 attributes
private String brand;
private String model;
private int batteryLevel;
private ArrayList<String> contacts;
// TODO: Add constructor
public Phone(String brand, String model) {
this.brand = brand;
this.model = model;
this.batteryLevel = 100; // starts at 100
this.contacts = new ArrayList<>();
}
// TODO: Add displayInfo() method
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Battery Level: " + batteryLevel + "%");
}
// TODO: Add addContact(String name) method
public void addContact(String name) {
contacts.add(name);
}
// TODO: Add showContacts() method
public void showContacts() {
System.out.println("Contacts:");
for (String contact : contacts) {
System.out.println("- " + contact);
}
}
// TODO: Add usePhone(int minutes) method
public void usePhone(int minutes) {
batteryLevel -= minutes;
if (batteryLevel < 0) {
batteryLevel = 0;
}
}
}
// Test your Phone class
public class PhoneTest {
public static void main(String[] args) {
// TODO: Create 2 Phone objects
Phone phone1 = new Phone("Apple", "iPhone 14");
Phone phone2 = new Phone("Samsung", "Galaxy S23");
// TODO: Add 3 contacts to each
phone1.addContact("Alice");
phone1.addContact("Bob");
phone1.addContact("Charlie");
phone2.addContact("David");
phone2.addContact("Eve");
phone2.addContact("Frank");
// TODO: Use phones for some minutes
phone1.usePhone(30);
phone2.usePhone(45);
// TODO: Display all information
phone1.displayInfo();
phone1.showContacts();
phone2.displayInfo();
phone2.showContacts();
}
}
PhoneTest.main(null);
Brand: Apple
Model: iPhone 14
Battery Level: 70%
Contacts:
- Alice
- Bob
- Charlie
Brand: Samsung
Model: Galaxy S23
Battery Level: 55%
Contacts:
- David
- Eve
- Frank