API HW
For the grinders group. 1.7.
Popcorn Hack 1
import java.util.ArrayList;
public class PopcornHack1 {
public static void main(String[] args) {
System.out.println(Math.pow(3, 4));
System.out.println(Math.sqrt(64));
ArrayList<String> colors = new ArrayList<>();
colors.add("red");
colors.add("blue");
colors.add("green");
System.out.println("Colors: " + colors);
System.out.println("Color array list size: " + colors.size());
}
}
PopcornHack1.main(null);
81.0
8.0
Colors: [red, blue, green]
Color array list size: 3
Popcorn Hack 2
public class Book {
private String title;
private String author;
private int pages;
public static void displayInfo(String title, String author, int pages) {
System.out.println(title + " - Title " + author + " - Author Name " + pages + " - number of pages");
}
public static void isLong(int pages) {
if (pages > 300) {
System.out.println("This is a long book.");
} else {
System.out.println("This is not a long book.");
}
}
}
// Book book = new Book("cabin", "herry beecher stowe", 2345);
// book.displayInfo();
Book.displayInfo("null", "fewaio", 2134);
Book.isLong(223);
Book.isLong(9994999);
null - Title fewaio - Author Name 2134 - number of pages
This is not a long book.
This is a long book.
Phone Homework Hack
import java.util.ArrayList;
import java.util.Arrays;
public class Phone {
public String brand;
public String model;
public int batteryLevel;
public ArrayList<String> contacts;
public Phone(String brand, String model, int batteryLevel, ArrayList<String> contacts) {
this.brand = brand;
this.model = model;
this.batteryLevel = batteryLevel;
this.contacts = new ArrayList<>();
}
public void displayInfo() {
System.out.println(brand + " brand, " + model + " model, " + batteryLevel + " battery level. " + contacts + " contacts arraylist");
}
// Add addContact(String name) method
public void addContact(String name) {
contacts.add(name);
System.out.println(name + " has been added to contacts.");
}
// TODO: Add showContacts() method
public void showContacts() {
System.out.println(contacts);
}
// TODO: Add usePhone(int minutes) method
public void usePhone(int minutes) {
batteryLevel = batteryLevel - minutes;
System.out.println("battery level: "+batteryLevel);
}
}
// Test your Phone class
public class PhoneTest {
public static void main(String[] args) {
// TODO: Create 2 Phone objects
Phone phone1 = new Phone("Nokia", "5e", 67, new ArrayList<>(Arrays.asList("John", "Person", "Jill")));
Phone phone2 = new Phone("Apple", "iPhone 31", 100, new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie")));
// TODO: Add 3 contacts to each
phone1.addContact("joe");
phone1.addContact("Jill");
phone1.addContact("Mark");
phone2.addContact("feij");
phone2.addContact("Bob");
phone2.addContact("Charlie");
// TODO: Use phones for some minutes
phone1.usePhone(23);
phone2.usePhone(3);
// TODO: Display all information
phone1.displayInfo();
phone2.displayInfo();
}
}
PhoneTest.main(null);
joe has been added to contacts.
Jill has been added to contacts.
Mark has been added to contacts.
feij has been added to contacts.
Bob has been added to contacts.
Charlie has been added to contacts.
battery level: 44
battery level: 97
Nokia brand, 5e model, 44 battery level. [joe, Jill, Mark] contacts arraylist
Apple brand, iPhone 31 model, 97 battery level. [feij, Bob, Charlie] contacts arraylist
i didn’t use chatgpt… yay.