9.7 Object Superclass
// Demonstration of toString method
// Utilized toString methods in all FRQs so far, here's an example from FRQ1
public String dayOfWeekToString() {
return ("{ \"month\": " + this.month + ", " + "\"day\": " + this.day + ", " + "\"year\": " + this.year + ", "
+ "\"dayOfWeek\": " + this.dayOfWeek + " }");
}
public String toString() {
return dayOfWeekToString();
}
// Demonstration of equals method
// Outputs boolean value of true or false
// If one object equals another
public class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
public static void main(String[] args)
{
Student student1 = new Student("Bob");
Student student2 = new Student("Jeff");
Student student3 = student1;
Student student4 = new Student("A");
Student student5 = student4;
System.out.println(student1.equals(student2));
System.out.println(student2.equals(student3));
System.out.println(student1.equals(student3));
System.out.println(student3.equals(student4));
System.out.println(student3.equals(student4));
System.out.println(student5.equals(student4));
}
}
Student.main(null);