In: Computer Science
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.
SOLUTION-
code -
Program code screen shot:
Program code:
Program code:
//define the GeometricObject of the abstract class
public abstract class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
// Construct a default geometric object
protected GeometricObject()
{
dateCreated = new java.util.Date();
}
// Construct a default geometric object with
//color and filled value
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
// Returns color
public String getColor()
{
return color;
}
// assign a new color
public void setColor(String color)
{
this.color = color;
}
// returns filled value
public boolean isFilled()
{
return filled;
}
// Set a new filled value
public void setFilled(boolean filled)
{
this.filled = filled;
}
// returns creation date
public java.util.Date getDateCreated()
{
return dateCreated;
}
// returns string representation of object
public String toString()
{
return "created on "+dateCreated+"\ncolor: "+color+" and filled: "+filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
// Defines rectangle class
public class Rectangle extends GeometricObject
{
public double width;
public double height;
// construct a rectangle
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
// method to find out perimeter
public double getPerimeter()
{
return 2 * (width + height);
}
// method to find out area
public double getArea()
{
return width * height;
}
// method to check equality
public boolean equals(Object obj)
{
System.out.println(this.getArea()+" "+((Rectangle)obj).getArea());
if(this.getArea() == ((Rectangle)obj).getArea())
return true;
return false;
}
}
//Definition of the ComparableRectangle that implements the Comparable
//interface
public class ComparableRectangle extends Rectangle implements Comparable
{
//constructor
public ComparableRectangle(double width, double height)
{
super(width, height);
}
//overriding the compareTo method
public int compareTo(Object o)
{
//logic to compare the Areas of the Rectangle objects
if(getArea() > ((ComparableRectangle)o).getArea())
return 1;
else if(getArea()
< ((ComparableRectangle)o).getArea())
return -1;
else
return 0;
}
}
//definition of the TestEqualRectangle
public class TestEqualRectangle
{
public static void main(String[] args)
{
//create objects of the Rectangle classes
Rectangle cc = new Rectangle(6, 7);
Rectangle cc1 = new Rectangle(8, 8);
//call the equals function to compare the two
//objects
boolean res = cc.equals(cc1);
//print the results
System.out.println("Result: "+res);
}
}
Sample Output:
42.0 64.0
Result: false
UML diagram:
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------