By default, when a object is created, the hash code of the object is returned such as Classname@6a41eaa2 which is not meaningful. Thus, it is better to Override the toString method of the Object (every class by default inherits the toString method from class Object). Do take note that when we override, it has to be public.
For example:
public String toString() {
return "Circle has a centre of" + this.centre + "with a radius of" + this.radius
}
Very often as observed in labs, the questions always ask us to return in 3 decimal places. Thus, here are some useful String formats you can use when you use the method String.format(_________):
%s is for string; %f is for float; %.3f is for float to 3 decimal places; %d is for integers;
An example on how to use String.format will be :
public String toString() {
return String.format("Today I folded %d paper cranes at a rate of %.3f cranes per hour", numOfCranes, rateOfFold)
}
where numOfCranes = 30, and rateOfFold = 5.9322 The return output of the String will be:
Today I folded 30 paper cranes at a rate of 5.932 cranes per hour
Classes inherit Object's equal method, which is not very useful because the method only implies if two objects' references to another obhject are the same or not. However, we want to use .equal() method to find equality. For example, lecture notes talk about how a Point is equal. It considers p.x and p.y and compares them with another point (this.x and this.y) in the given method below.
A more correct way of comparing two object method is via the equals method.
An example from the lecture notes is:
@Override
boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof Point) {
Point p = (Point) obj;
return Math.abs(this.x - p.x) < 1E-15 &&
Math.abs(this.y - p.y) < 1E-15;
} else {
return False;
}
}