Popcorn Hack #1
class Book {
String title;
int pages;
// Method to print book information
void printInfo() {
System.out.println("Title: " + title + ", Pages: " + pages);
}
}
class MainPopcorn {
public static void main(String[] args) {
// Create a Book object
Book myBook = new Book();
// Set the fields
myBook.title = "Popcorn Adventures";
myBook.pages = 120;
// Call the printInfo method
myBook.printInfo();
}
}
Homework Hack
class Student {
String name;
int grade;
int pets;
int siblings;
// Method to print student information
void printInfo() {
System.out.println("Name: " + name + ", Grade: " + grade + ", Pets: " + pets + ", Siblings: " + siblings);
}
}
class MainClass {
public static void main(String[] args) {
// Create three student objects
Student student1 = new Student();
student1.name = "Alice";
student1.grade = 10;
student1.pets = 2;
student1.siblings = 1;
Student student2 = new Student();
student2.name = "Bob";
student2.grade = 11;
student2.pets = 1;
student2.siblings = 2;
Student student3 = new Student();
student3.name = "Charlie";
student3.grade = 12;
student3.pets = 0;
student3.siblings = 3;
// Print information of all students
student1.printInfo();
student2.printInfo();
student3.printInfo();
// Create a reference variable for a student with a nickname
Student nicknameStudent = student1;
// Output the instance attributes of the student with the nickname
nicknameStudent.printInfo();
}
}
MainClass.main(null);
Name: Alice, Grade: 10, Pets: 2, Siblings: 1
Name: Bob, Grade: 11, Pets: 1, Siblings: 2
Name: Charlie, Grade: 12, Pets: 0, Siblings: 3
Name: Alice, Grade: 10, Pets: 2, Siblings: 1