Lesson 1.12 Homework
Popcorn Hack 1
class Book {
private String title;
private int pages;
public Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
public void printInfo() {
System.out.println("Title: " + title + ", Pages: " + pages);
}
}
class MainPopcorn {
public static void main(String[] args) {
Book book = new Book("[Insert Genius Book Name]", 256);
book.printInfo();
}
}
MainPopcorn.main(null);
Title: [Insert Genius Book Name], Pages: 256
Quick Self Check
1. A class is a blueprint that defines the properties and behaviors of objects, while an object is an instance created from that class.
2. A reference variable stores the memory address (reference) of an object rather than the object itself.
3. Every object has the toString()
method because it is inherited from the Object
class.
Homework Hack
class Student {
public String name;
public int grade;
public int pets;
private int siblings;
public int age;
public Student(String name, int grade, int pets, int siblings, int age) {
this.name = name;
this.grade = grade;
this.pets = pets;
this.siblings = siblings;
this.age = age;
}
}
Student student1 = new Student("Alice", 10, 2, 1, 15);
Student student2 = new Student("Bob", 11, 0, 3, 16);
Student student3 = new Student("Charlie", 12, 1, 2, 17);
System.out.println("Student 1's name is: " + student1.name);
System.out.println("Student 2's name is: " + student2.name);
System.out.println("Student 3's name is: " + student3.name);
// Charlie aparently goes by charles
student3.name = "Charles";
System.out.println("Student 3's new name is: " + student3.name);
Student 1's name is: Alice
Student 2's name is: Bob
Student 3's name is: Charlie
Student 3's new name is: Charles