182k views
1 vote
Homework Assignment 2 1. Consider the skeletal definition for a class called circle given as: public class Circle { private int radius; private Location location; private String = name; … } Override the equals and hashCode methods of the Object class for the Circle class. Assume that two circles are considered to be equal if they have the same radius and location (i.e. name field does not matter). Further assume that the Location class has overridden the hashCode method of the Object class. 2. Answer questions 12.35, 12.36, and 12.37 in the BlueJ text. 3. For the Date class provided with this assignment (Note: this is a BlueJ project) , implement the Comparable interface for the Date class. A date is considered less than another date if it comes before it in time.

User Macroland
by
5.8k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

public class Circle {

private double radius;

private Location location;

private String name;

/* (non-Javadoc)

* atsee java.lang.Object#hashCode()

*/

atOverride

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((location == null) ? 0 : location.hashCode());

long temp;

temp = Double.doubleToLongBits(radius);

result = prime * result + (int) (temp ^ (temp >>> 32));

return result;

}

/* (non-Javadoc)

* atsee java.lang.Object#equals(java.lang.Object)

*/

atOverride

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Circle other = (Circle) obj;

if (location == null) {

if (other.location != null)

return false;

} else if (!location.equals(other.location))

return false;

if (Double.doubleToLongBits(radius) != Double.doubleToLongBits(other.radius))

return false;

return true;

}

NOTE: Replace all the "at" within the program with the at symbol ie shift 2.

User Zourbuth
by
6.0k points