// TODO: Finish and run in your Java environment
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 = "The Great Gatsby";
myBook.pages = 180;
myBook.printInfo();
}
}
// ## Quick Self-Check (concepts)
// 1. In one sentence, explain **class vs. object**.
// - A **class** is a blueprint for creating objects, defining their properties and behaviors, while an **object** is an instance of a class that represents a specific entity with its own state and behavior.
// 2. What does a **reference variable** store?
// - A **reference variable** stores the memory address of an object, allowing access to the object's properties and methods.
// 3. Name one method every object has because it extends **`Object`**.
// - One method every object has is **`toString()`**, which returns a string representation of the object.
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
- Create a class
- Instance variables:
String nameint gradeint petsint siblingsMAKE YOUR OWN
- In
main:- Create three student objects.
//Code Here
public class StudentDemo {
// Define the Student class
static class Student {
// Instance variables
String name;
int grade;
int pets;
int siblings;
String favoriteSubject; // your own custom variable
// Constructor
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 info
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Grade: " + grade);
System.out.println("Pets: " + pets);
System.out.println("Siblings: " + siblings);
System.out.println("Favorite Subject: " + favoriteSubject);
System.out.println("--------------------------");
}
}
// Main method
public static void main(String[] args) {
// Create three student objects
Student s1 = new Student("Ava", 10, 1, 2, "Biology");
Student s2 = new Student("Liam", 11, 0, 1, "Computer Science");
Student s3 = new Student("Sophia", 9, 3, 0, "History");
// Display their information
s1.displayInfo();
s2.displayInfo();
s3.displayInfo();
}
}
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
-
Use your previous code
-
In
main:- Create a reference variable with a different name that points to the same student.
- Output the instance attributes of this student
public class StudentDemo {
// Define the Student class
static class Student {
String name;
int grade;
int pets;
int siblings;
String favoriteSubject;
// Constructor
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;
}
// Display student info
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Grade: " + grade);
System.out.println("Pets: " + pets);
System.out.println("Siblings: " + siblings);
System.out.println("Favorite Subject: " + favoriteSubject);
System.out.println("--------------------------");
}
}
public static void main(String[] args) {
// Create three student objects
Student s1 = new Student("Ava", 10, 1, 2, "Biology");
Student s2 = new Student("Liam", 11, 0, 1, "Computer Science");
Student s3 = new Student("Sophia", 9, 3, 0, "History");
// Create a reference variable pointing to the same student
Student nickname = s1; // 'nickname' now refers to the same object as 's1'
// Change the name to reflect the nickname
nickname.name = "A.J.";
// Display using both references
System.out.println("Displaying s1 info:");
s1.displayInfo();
System.out.println("Displaying nickname info (same object):");
nickname.displayInfo();
}
}