13.10 Write 2-3 paragraphs explaining the design rationale for making Rectangle
ID: 3831864 • Letter: 1
Question
13.10 Write 2-3 paragraphs explaining the design rationale for making Rectangle extend Geometric Object and implement the Comparable interface. How is making Rectangle a child class of GeometricObject and one that implements Comparable interface a better idea than the alternative of not doing these things, from a design perspective?
[JAVA] (Do not need the program, just the written paragraphs)
13.10 (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 ComparableExplanation / Answer
Please find my code.
############
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 geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
################
public class Rectangle extends GeometricObject implements Comparable<Rectangle>{
private double length;
private double width;
/**
* Constructor
*/
public Rectangle(double len, double w)
{
length = len;
width = w;
}
/**
* The setLength method accepts an argument
* which is stored in the length field.
*/
public void setLength(double len)
{
length = len;
}
/**
* The setWidth method accepts an argument
* which is stored in the width field.
*/
public void setWidth(double w)
{
width = w;
}
/**
* The set method accepts two arguments
* which are stored in the length and width
* fields.
*/
public void set(double len, double w)
{
length = len;
width = w;
}
/**
* The getLength method returns the value
* stored in the length field.
*/
public double getLength()
{
return length;
}
/**
* The getWidth method returns the value
* stored in the width field.
*/
public double getWidth()
{
return width;
}
/**
* The getArea method returns the value of the
* length field times the width field.
*/
@Override
public double getArea()
{
return length * width;
}
@Override
public int compareTo(Rectangle o) {
if(getArea() < o.getArea())
return -1;
else if(getArea() > o.getArea())
return 1;
else
return 0;
}
@Override
public String toString() {
return "Length: "+length+", Width: "+width+", Area: "+getArea();
}
@Override
public double getPerimeter() {
return 2*(length+width);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.