Circle 5tring
Subclasses inherit, or receive, the methods of its superclass. The Object class includes methods for comparing objects and representing an object as a string:
Class Object (java.lang.Object)
Method
equals (Object obj) returns true if obj is equal to the object. toString ( ) returns a String that represents the object.
A subclass typically contains its own version of the equals() and toString() superclass methods to better suit the object of the subclass. For example, two Circle objects are equal when they both have the same radius, and two String objects are equal when they consist of the same set of characters. When a subclass redefines a superclass method, the subclass method is said to override the superclass method. The Circle class should contain an equals() method that compares the state of the object to another Circle object and a toString() method that returns a String describing the object:
/** * Determines if the object is equal to another * Circle object. * pre: c is a Circle object. * post: true has been returned if the objects have * the same radii. false has been returned otherwise. public boolean equals(Object c)
{
Circle testObj = (Circle)c;
if (testObj.getRadius() == radius) return(true); else return(false);
}
* Returns a String that represents the Circle object. * pre: none
* post: A string representing the Circle object has * been returned. public String toString()
{ String circleString; circleString = "Circle has radius " + radius; return (circleString);
}
The equals() method requires an Object parameter. In the body of the method, the obj parameter must be cast as the appropriate type, in this case Circle, and then assigned to an