Skip to the content.

Big Idea 1 Lesson 1.12

Big Idea 1 Lesson 1.12

Popcorn Hack 1

class Book {
    String title;
    int pages;
    
    void printInfo() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

class MainPopcorn {
    public static void main(String[] args) {
        Book myBook = new Book();
        myBook.title = "Java Basics";
        myBook.pages = 150;
        myBook.printInfo();
    }
}

MainPopcorn.main(null);
Title: Java Basics, Pages: 150

Homework Hack

class Student {
    String name;
    int grade;
    int pets;
    int siblings;
    String favoriteSubject;  // New instance variable

    // Method to print student info
    void printInfo() {
        System.out.println("Name: " + name + ", Grade: " + grade +
                           ", Pets: " + pets + ", Siblings: " + siblings +
                           ", Favorite Subject: " + favoriteSubject);
    }

    public static void main(String[] args) {
        // Create 3 student objects
        Student kush = new Student();
        kush.name = "Kush";
        kush.grade = 10;
        kush.pets = 2;
        kush.siblings = 1;
        kush.favoriteSubject = "Math";

        Student student2 = new Student();
        student2.name = "Brian";
        student2.grade = 11;
        student2.pets = 0;
        student2.siblings = 3;
        student2.favoriteSubject = "Science";

        Student student3 = new Student();
        student3.name = "Catherine";
        student3.grade = 12;
        student3.pets = 1;
        student3.siblings = 2;
        student3.favoriteSubject = "English";

        // Output info for all students
        kush.printInfo();
        student2.printInfo();
        student3.printInfo();

        // Create a reference variable pointing to kush (nickname)
        Student k = kush;

        // Output attributes using the new reference variable
        System.out.println("\nUsing the nickname reference:");
        k.printInfo();
    }
}

Student.main(null);
Name: Kush, Grade: 10, Pets: 2, Siblings: 1, Favorite Subject: Math
Name: Brian, Grade: 11, Pets: 0, Siblings: 3, Favorite Subject: Science
Name: Catherine, Grade: 12, Pets: 1, Siblings: 2, Favorite Subject: English

Using the nickname reference:
Name: Kush, Grade: 10, Pets: 2, Siblings: 1, Favorite Subject: Math