1.12 Homework
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 name
int grade
int pets
int siblings
MAKE YOUR OWN
- In
main
:- Create three student objects.
// Main.java
public class Main {
public static void main(String[] args) {
// Create three student objects
Student s1 = new Student("Person 1", 10, 1, 0, "Robotics");
Student s2 = new Student("Person 2", 11, 2, 3, "Cyber");
Student s3 = new Student("Person 3", 9, 0, 1, "Piano");
// Print them out
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
/**
* Template (class) for creating Student objects.
*/
class Student {
// Instance variables
private String name; // student's full name
private int grade; // grade level (e.g., 9, 10, 11, 12)
private int pets; // how many pets they have
private int siblings; // how many siblings they have
private String favoriteHobby; // <-- MAKE YOUR OWN (custom field)
// Constructor
public Student(String name, int grade, int pets, int siblings, String favoriteHobby) {
this.name = name;
this.grade = grade;
this.pets = pets;
this.siblings = siblings;
this.favoriteHobby = favoriteHobby;
}
// Getters (optional, but handy)
public String getName() { return name; }
public int getGrade() { return grade; }
public int getPets() { return pets; }
public int getSiblings() { return siblings; }
public String getFavoriteHobby() { return favoriteHobby; }
// toString to print a friendly description
@Override
public String toString() {
return String.format(
"Student{name='%s', grade=%d, pets=%d, siblings=%d, favoriteHobby='%s'}",
name, grade, pets, siblings, favoriteHobby
);
}
}
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
// Main.java
public class Main {
public static void main(String[] args) {
// Original three students
Student s1 = new Student("Avery Kim", 10, 1, 0, "Robotics");
Student s2 = new Student("Luca Ramirez", 11, 2, 3, "Basketball");
Student s3 = new Student("Maya Patel", 9, 0, 1, "Piano");
// Create a second reference to the SAME object as s1
Student Ace = s1; // Avery goes by the nickname "Ace"
// Output the instance attributes via the nickname reference
System.out.println("Nickname reference ->");
System.out.println("Name: " + Ace.getName());
System.out.println("Grade: " + Ace.getGrade());
System.out.println("Pets: " + Ace.getPets());
System.out.println("Siblings: " + Ace.getSiblings());
System.out.println("Favorite Hobby: " + Ace.getFavoriteHobby());
// (Optional) Show it's truly the same object:
// changing through one reference is visible via the other.
// For example, if we had a setter for hobby we could change it here.
// Since we don't, we can still show they share the same identity:
System.out.println("\nAre s1 and Ace the same object? " + (s1 == Ace)); // true
// Print both references (they'll show the same data)
System.out.println("\ns1: " + s1);
System.out.println("Ace: " + Ace);
}
}
/**
* Template (class) for creating Student objects.
*/
class Student {
// Instance variables
private String name; // student's full name
private int grade; // grade level (e.g., 9, 10, 11, 12)
private int pets; // how many pets they have
private int siblings; // how many siblings they have
private String favoriteHobby; // custom field
// Constructor
public Student(String name, int grade, int pets, int siblings, String favoriteHobby) {
this.name = name;
this.grade = grade;
this.pets = pets;
this.siblings = siblings;
this.favoriteHobby = favoriteHobby;
}
// Getters
public String getName() { return name; }
public int getGrade() { return grade; }
public int getPets() { return pets; }
public int getSiblings() { return siblings; }
public String getFavoriteHobby() { return favoriteHobby; }
// toString for easy printing
@Override
public String toString() {
return String.format(
"Student{name='%s', grade=%d, pets=%d, siblings=%d, favoriteHobby='%s'}",
name, grade, pets, siblings, favoriteHobby
);
}
}