Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Cosmic Superclass Every Java class youve ever written isautomatically a subclass of java.lang.Object. This means thatpublic methods from Object are inherited by your classes,

The Cosmic Superclass Every Java class you’ve ever written isautomatically a subclass of java.lang.Object. This means thatpublic methods from Object are inherited by your classes, and youmay override them. Several of these methods have signficantimplications, and we will concern ourselves with 2 of them:toString() and equals(Object other) To start Write a Student classthat has two instance variables: String name and int age. Write amain method that constructs such an object, sets the instancevariables, and then prints out the object. Something like this:Student s = new Student ( " Brian " , 22); System . out . println (" Student : " + s ); What does it print out? toString() WheneverJava needs to treat an object like a String, whether that be toprint out the object, or to use it to build up text (such as in theprevious example), it automatically calls the toString method toget the appropriate value. This works because every object has thismethod! You can override the method to change the Stringrepresentation for your custom objects. 1 Turn in a class thatloads an ArrayList with Student objects and prints out the entirelist: ArrayList < Student > students = new ArrayList (); students . add ( new Student ( " Brian " , 22));students . add ( new Student ( " Sandy " , 28)); System . out .println ( students ); What do you think is going on here?equals(Object other) Another method that all objects have isboolean equals(Object other). This method exists so that thedesigner of a new class can decide what makes two objects equal.Write a main method that creates two Student objects with the sameinstance variables. Are they equal? Should they be? Student s1 =new Student (" Craig " , 40); Student s2 = new Student (" Craig " ,40); if ( s1 == s2 ) { System . out . println ( " They are the same" ); } Hey, that’s never going to work! The equals operator (==)never looks at the contents of the objects it’s comparing. (That’swhy you shouldn’t use == to compare String objects) What does ==actually compare? Student s1 = new Student (" Craig " , 40);Student s2 = new Student (" Craig " , 40); if ( s1 . equals ( s2 )){ System . out . println ( " They are the same " ); } 2 Turn in aversion of the Student class that overrides equals to make theabove work as expected. Make sure to look at the javadocs forObject.equals to make sure you know how it should work

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

Studentjava public class Student instance variables private String name private int age constructor ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Concepts Late Objects

Authors: Cay S. Horstmann

3rd Edition

1119186714, 978-1119186717

More Books

Students also viewed these Programming questions

Question

If X has distribution function F(t) = 0, t Answered: 1 week ago

Answered: 1 week ago

Question

Web Application Security Analysis Using OWASP - ZAP

Answered: 1 week ago

Question

please dont use chat gpt or other AI 4 8 5 . .

Answered: 1 week ago