🍿 Popcorn Hack (one cell)

Task: Complete the code below.

  1. Finish the Book class by adding a printInfo() method that prints title and pages.
  2. In Main, create one Book object, set its fields, and call printInfo().

Keep it short—this is a quick check that you can define a class and create an instance.

// TODO: Finish and run in your Java environment
class Book {
    String title;
    int pages;
    
    // TODO: write printInfo() to show: Title: <title>, Pages: <pages>
    void printInfo() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

class MainPopcorn {
    public static void main(String[] args) {
        // TODO: make one Book, set its fields, and call printInfo()
        Book myBook = new Book();
        myBook.title = "The Great Gatsby";
        myBook.pages = 180;
        myBook.printInfo();
    }
}

MainPopcorn.main(null);
Title: The Great Gatsby, Pages: 180

Quick Self-Check (concepts)

  1. In one sentence, explain class vs. object. Answer: A class is a blueprint or template that defines the structure and behavior, while an object is a specific instance created from that class.

  2. What does a reference variable store? Answer: A reference variable stores the memory address (location) of an object, not the actual object data itself.

  3. Name one method every object has because it extends Object. Answer: toString() (other examples include equals(), hashCode(), getClass())

Homework


Scenario

Its your first day of class when Mr. Mortenson asks you to identify all the students and list some characteristics about them. Create a class to use as a template to create student objects and create 3 imaginary students.

Instructions

  1. Create a class
    • Instance variables:
    • String name
    • int grade
    • int pets
    • int siblings
    • MAKE YOUR OWN
  2. In main:
    • Create three student objects.
// Student Class Implementation - Homework by Anvay Vahia

class Student {
    // Required instance variables
    String name;
    int grade;
    int pets;
    int siblings;
    
    // My own custom variable
    String favoriteSubject;
    
    // Constructor for easy object creation
    public Student(String name, int grade, int pets, int siblings, String favoriteSubject) {
        this.name = name;
        this.grade = grade;
        this.pets = pets;
        this.siblings = siblings;
        this.favoriteSubject = favoriteSubject;
    }
    
    // Method to display student information
    public void displayInfo() {
        System.out.println("Student Name: " + name);
        System.out.println("Grade: " + grade);
        System.out.println("Number of Pets: " + pets);
        System.out.println("Number of Siblings: " + siblings);
        System.out.println("Favorite Subject: " + favoriteSubject);
        System.out.println("---");
    }
}

class MainHomework {
    public static void main(String[] args) {
        System.out.println("=== Mr. Mortenson's Class Roster ===");
        System.out.println();
        
        // Create three student objects with different characteristics
        Student student1 = new Student("Emma Rodriguez", 10, 2, 1, "Computer Science");
        Student student2 = new Student("Marcus Chen", 11, 0, 3, "Mathematics");
        Student student3 = new Student("Sophia Kim", 10, 1, 2, "Biology");
        
        // Display information for all three students
        System.out.println("Student 1:");
        student1.displayInfo();
        
        System.out.println("Student 2:");
        student2.displayInfo();
        
        System.out.println("Student 3:");
        student3.displayInfo();
    }
}

MainHomework.main(null);
=== Mr. Mortenson's Class Roster ===

Student 1:
Student Name: Emma Rodriguez
Grade: 10
Number of Pets: 2
Number of Siblings: 1
Favorite Subject: Computer Science
---
Student 2:
Student Name: Marcus Chen
Grade: 11
Number of Pets: 0
Number of Siblings: 3
Favorite Subject: Mathematics
---
Student 3:
Student Name: Sophia Kim
Grade: 10
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Biology
---

Student 1:
Student Name: Emma Rodriguez
Grade: 10
Number of Pets: 2
Number of Siblings: 1
Favorite Subject: Computer Science
---
Student 2:
Student Name: Marcus Chen
Grade: 11
Number of Pets: 0
Number of Siblings: 3
Favorite Subject: Mathematics
---
Student 3:
Student Name: Sophia Kim
Grade: 10
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Biology
---

New Scenario

You find out that one of the students that you created an object for goes by a nickname. Using your knowledge of reference variables, create another object that references back to the same student.

Instructions

  1. Use your previous code

  2. In main:

    • Create a reference variable with a different name that points to the same student.
    • Output the instance attributes of this student
// Reference Variable Demonstration - Homework by Anvay Vahia

class StudentWithNickname {
    // Same instance variables as before
    String name;
    int grade;
    int pets;
    int siblings;
    String favoriteSubject;
    
    public StudentWithNickname(String name, int grade, int pets, int siblings, String favoriteSubject) {
        this.name = name;
        this.grade = grade;
        this.pets = pets;
        this.siblings = siblings;
        this.favoriteSubject = favoriteSubject;
    }
    
    public void displayInfo() {
        System.out.println("Student Name: " + name);
        System.out.println("Grade: " + grade);
        System.out.println("Number of Pets: " + pets);
        System.out.println("Number of Siblings: " + siblings);
        System.out.println("Favorite Subject: " + favoriteSubject);
        System.out.println("---");
    }
}

class MainReferenceDemo {
    public static void main(String[] args) {
        System.out.println("=== Reference Variable Demonstration ===");
        System.out.println();
        
        // Create original student object
        StudentWithNickname originalStudent = new StudentWithNickname("Alexander Johnson", 11, 1, 2, "Physics");
        
        System.out.println("Original student information:");
        originalStudent.displayInfo();
        
        // Create reference variable that points to the same student object
        // This student goes by "Alex" as a nickname
        StudentWithNickname alex = originalStudent;  // Reference to same object!
        
        System.out.println("Same student accessed through nickname reference:");
        alex.displayInfo();
        
        System.out.println("=== Demonstrating they are the same object ===");
        
        // Modify the student through the nickname reference
        alex.favoriteSubject = "Computer Science";  // Changed through alex reference
        
        System.out.println("After changing favorite subject through 'alex' reference:");
        System.out.println("Original reference shows:");
        originalStudent.displayInfo();
        
        System.out.println("Nickname reference shows:");
        alex.displayInfo();
        
        // Prove they point to the same object in memory
        System.out.println("Are they the same object? " + (originalStudent == alex)); // true
        
        System.out.println();
        System.out.println("This demonstrates that both variables point to the same object in memory!");
    }
}

MainReferenceDemo.main(null);
=== Reference Variable Demonstration ===

Original student information:
Student Name: Alexander Johnson
Grade: 11
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Physics
---
Same student accessed through nickname reference:
Student Name: Alexander Johnson
Grade: 11
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Physics
---
=== Demonstrating they are the same object ===
After changing favorite subject through 'alex' reference:
Original reference shows:
Student Name: Alexander Johnson
Grade: 11
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Computer Science
---
Nickname reference shows:
Student Name: Alexander Johnson
Grade: 11
Number of Pets: 1
Number of Siblings: 2
Favorite Subject: Computer Science
---
Are they the same object? true

This demonstrates that both variables point to the same object in memory!