Lesson 1.7 Homework
Popcorn Hack 1
import java.util.ArrayList;
import java.lang.Math;
public class PopcornHack1 {
public static void main(String[] args) {
double power = Math.pow(3, 4);
double root = Math.sqrt(64);
ArrayList<String> lst = new ArrayList<>();
lst.add("red");
lst.add("green");
lst.add("blue");
System.out.println("Amount of colors: " + lst.size());
}
}
PopcornHack1.main(null);
Amount of colors: 3
Popcorn Hack 2
public class Book {
private String title;
private String author;
private int pages;
Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public void displayInfo() {
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.author);
System.out.println("Pages: " + this.pages);
}
public boolean isLong() {
if (this.pages >= 300) {
return true;
}
else {
return false;
}
}
}
Book book = new Book("Good & Evil", "Tally Hall", 14);
book.displayInfo();
if (book.isLong()) {
System.out.println("This book is very long.");
}
else {
System.out.println("This book is not long.");
}
Title: Good & Evil
Author: Tally Hall
Pages: 14
This book is not long.
Homework Hack
import java.util.ArrayList;
import java.lang.Math;
class Phone {
private String brand;
private String model;
private double battery;
private ArrayList<String> contacts;
Phone(String brand, String model) {
this.brand = brand;
this.model = model;
this.battery = 100;
this.contacts = new ArrayList<>();
System.out.println("New Phone Initialized");
System.out.println("");
System.out.println("");
}
public void displayInfo() {
System.out.println("Grabbed Phone Information");
System.out.println("");
System.out.println("Brand: " + this.brand);
System.out.println("Model: " + this.model);
System.out.println("");
}
public void addContact(String person) {
System.out.println("Add " + person + " to contacts");
System.out.println("");
this.contacts.add(person);
}
public void showContacts() {
System.out.println("Contacts:");
for (int i = 0; i <= this.contacts.size()-1; i++) {
System.out.println(this.contacts.get(i));
}
System.out.println("");
}
public void usePhone(int minutes) {
double base = 100.0;
double usage = base * Math.pow(0.99, minutes);
double diff = battery - usage;
System.out.println("Used Phone for " + minutes + " minute(s) and lost " + (int)diff + "% of Battery");
this.battery = usage;
System.out.println("");
}
}
public class PhoneTest {
public static void main(String[] args) {
Phone phone1 = new Phone("Android", "74KA");
phone1.addContact("Sandra");
phone1.addContact("Carl");
phone1.addContact("Mr.");
phone1.showContacts();
phone1.usePhone(10);
phone1.displayInfo();
Phone phone2 = new Phone("Apple", "61M");
phone1.addContact("Bullock");
phone1.addContact("Smite");
phone1.addContact(" McCarsman");
phone1.showContacts();
phone1.usePhone(60);
phone1.displayInfo();
}
}
PhoneTest.main(null);
New Phone Initialized
Add Sandra to contacts
Add Carl to contacts
Add Mr. to contacts
Contacts:
Sandra
Carl
Mr.
Used Phone for 10 minute(s) and lost 9% of Battery
Grabbed Phone Information
Brand: Android
Model: 74KA
New Phone Initialized
Add Bullock to contacts
Add Smite to contacts
Add McCarsman to contacts
Contacts:
Sandra
Carl
Mr.
Bullock
Smite
McCarsman
Used Phone for 60 minute(s) and lost 35% of Battery
Grabbed Phone Information
Brand: Android
Model: 74KA