Add an equals method to each of the Rectangle, Circle, and Triangle classes introduced in this chapter. Two shapes are considered equal if their fields have equivalent values.
Rectangle.java
// package inheritance;
public class Rectangle implements Shape {
private double width;
private double height;
// constructs a new rectangle with the given dimensions
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// returns the area of this rectangle
public double getArea() {
return width * height;
}
// returns the perimeter of this rectangle
public double getPerimeter() {
return 2.0 * (width + height);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Rectangle rec = (Rectangle) obj;
if ((Double.valueOf(this.width) == null) ? (Double.valueOf(rec.height) != null)
: !((Double.valueOf(this.width)).equals(Double.valueOf(rec.width)))) {
return false;
}
if (this.height != rec.height) {
return false;
}
return true;
}
@Override
public int hashCode() {
// chose a random prime number for better hash functionality
int hash = 31;
hash = 17 * hash + (Double.valueOf(this.width) != null ? Double.valueOf(this.width).hashCode() : 0);
hash = (int) (17 * hash + this.height);
return hash;
}
}
Circle.java
// package inheritance;
public class Circle implements Shape {
private double radius;
// constructs a new circle with the given radius
public Circle(double radius) {
this.radius = radius;
}
// returns the area of this circle
public double getArea() {
return Math.PI * radius * radius;
}
// returns the perimeter of this circle
public double getPerimeter() {
return 2.0 * Math.PI * radius;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Circle cir = (Circle) obj;
if (this.radius != cir.radius) {
return false;
}
return true;
}
@Override
public int hashCode() {
// chose a random prime number for better hash functionality
int hash = 31;
hash = 17 * hash + (Double.valueOf(this.radius) != null ? Double.valueOf(this.radius).hashCode() : 0);
hash = (int) (17 * hash + this.radius);
return hash;
}
}
Triangle.java
// package inheritance;
public class Triangle implements Shape {
private double a;
private double b;
private double c;
// constructs a new triangle with the given side lengths
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
// returns this triangle's area using Heron's formula
public double getArea() {
double s = (a + b + c) / 2.0;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
// returns the perimeter of this triangle
public double getPerimeter() {
return a + b + c;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Triangle tri = (Triangle) obj;
if (!(Double.valueOf(this.a).equals(Double.valueOf(tri.a))) || !(Double.valueOf(this.b).equals(Double.valueOf(tri.b)))
|| !(Double.valueOf(this.c).equals(Double.valueOf(tri.c)))) {
return false;
}
return true;
}
@Override
public int hashCode() {
// chose a random prime number for better hash functionality
int hash = 31;
hash = 17 * hash + (Double.valueOf(this.a) != null ? Double.valueOf(this.b).hashCode() : 0);
hash = (int) (17 * hash + this.c);
return hash;
}
}
CheckTwoShapes.java
public class CheckTwoShapes {
public static void main(String[] args) {
Rectangle rec = new Rectangle(4,8);
Rectangle rec1 = new Rectangle(2,7);
Rectangle rec2 = new Rectangle(4,8);
Circle c1 = new Circle(7);
Circle c2 = new Circle(4);
Circle c3 = new Circle(4);
Triangle tri = new Triangle(9,7,3);
Triangle tri1 = new Triangle(3,4,8);
Triangle tri2 = new Triangle(9,7,3);
System.out.println(rec.equals(rec2));
System.out.println(rec.equals(rec1));
System.out.println(c3.equals(c2));
System.out.println(c1.equals(c3));
System.out.println(tri.equals(tri2));
System.out.println(tri1.equals(tri2));
}
}
Output:
true
false
true
false
true
false