3^4 = 81.0
√64 = 8.0
# of colors: 3
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]