Unit 1.7 - Hacks (Answers)
Categories: homeworkJava homework assignment covering documentation usage, attributes, behaviors, and object-oriented programming concepts
Unit 1.7 – Hacks (Answers)
Popcorn Hack #1: Using Documentation
import java.util.ArrayList;
public class PopcornHack1 {
public static void main(String[] args) {
// 1) 3^4 using Math.pow
double threePowFour = Math.pow(3, 4);
System.out.println("3^4 = " + threePowFour);
// 2) sqrt(64) using Math.sqrt
double sqrt64 = Math.sqrt(64);
System.out.println("sqrt(64) = " + sqrt64);
// 3) Create ArrayList of Strings
ArrayList<String> colors = new ArrayList<>();
// 4) Add 3 colors
colors.add("red");
colors.add("blue");
colors.add("green");
// 5) Print the size
System.out.println("colors size = " + colors.size());
// Optional: quick sanity print
System.out.println("colors = " + colors);
}
}
Popcorn Hack 2: Attributes and Behaviors
```python
public class Book {
//Attributes
private String title;
private String author;
private int pages;
// Constructor
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// displayInfo(): print all book info
public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Pages: " + pages);
}
// isLong(): return true if pages > 300
public boolean isLong() {
return pages > 300;
}
// Simple test runner
public static void main(String[] args) {
Book myBook = new Book("Java Basics", "John Doe", 350);
myBook.displayInfo();
System.out.println("isLong = " + myBook.isLong());
}
}
Expected Output:
3^4 = 81.0 sqrt(64) = 8.0 colors size = 3 colors = [red, blue, green]
Expected Output:
Title: Java Basics Author: John Doe Pages: 350 isLong = true
// Phone.java
import java.util.ArrayList;
public class Phone {
// Attributes (4)
private String brand;
private String model;
private int batteryLevel;
private ArrayList<String> contacts;
// Constructor - sets brand and model, initializes empty contacts list
public Phone(String brand, String model) {
this.brand = brand;
this.model = model;
this.batteryLevel = 100;
this.contacts = new ArrayList<String>();
}
// displayInfo() - prints brand, model, and battery level
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Battery Level: " + batteryLevel + "%");
}
// addContact(String name) - adds name to contacts list
public void addContact(String name) {
contacts.add(name);
System.out.println("Contact '" + name + "' added successfully!");
}
// showContacts() - prints all contacts
public void showContacts() {
System.out.println("\nContacts List:");
if (contacts.isEmpty()) {
System.out.println("No contacts available.");
} else {
for (int i = 0; i < contacts.size(); i++) {
System.out.println((i + 1) + ". " + contacts.get(i));
}
}
}
// usePhone(int minutes) - decreases battery by minutes used
public void usePhone(int minutes) {
batteryLevel -= minutes;
if (batteryLevel < 0) {
batteryLevel = 0;
}
System.out.println("Used phone for " + minutes + " minutes. Battery remaining: " + batteryLevel + "%");
}
}
// PhoneTest.java
public class PhoneTest {
public static void main(String[] args) {
// Create 2 Phone objects
Phone phone1 = new Phone("Apple", "iPhone 15");
Phone phone2 = new Phone("Samsung", "Galaxy S24");
System.out.println("=== PHONE 1 ===");
// Add 3 contacts to phone1
phone1.addContact("Alice Johnson");
phone1.addContact("Bob Smith");
phone1.addContact("Charlie Brown");
// Use phone1 for some minutes
phone1.usePhone(25);
// Display phone1 information
System.out.println("\n--- Phone 1 Information ---");
phone1.displayInfo();
phone1.showContacts();
System.out.println("\n\n=== PHONE 2 ===");
// Add 3 contacts to phone2
phone2.addContact("David Lee");
phone2.addContact("Emma Watson");
phone2.addContact("Frank Miller");
// Use phone2 for some minutes
phone2.usePhone(40);
// Display phone2 information
System.out.println("\n--- Phone 2 Information ---");
phone2.displayInfo();
phone2.showContacts();
}
}
output
=== PHONE 1 === Contact ‘Alice Johnson’ added successfully! Contact ‘Bob Smith’ added successfully! Contact ‘Charlie Brown’ added successfully! Used phone for 25 minutes. Battery remaining: 75%
— Phone 1 Information — Brand: Apple Model: iPhone 15 Battery Level: 75%
Contacts List:
- Alice Johnson
- Bob Smith
- Charlie Brown
=== PHONE 2 === Contact ‘David Lee’ added successfully! Contact ‘Emma Watson’ added successfully! Contact ‘Frank Miller’ added successfully! Used phone for 40 minutes. Battery remaining: 60%
— Phone 2 Information — Brand: Samsung Model: Galaxy S24 Battery Level: 60%
Contacts List:
- David Lee
- Emma Watson
- Frank Miller