Question: Write a class named Octagon ( Octagon.java ) that extends the following abstract GeometricObject class (same as the code in Listing 13.1 on pages 496-498
Write a class named Octagon (
Octagon.java
) that extends the following abstract
GeometricObject
class (same as the code in Listing 13.1 on pages 496-498 in the
required textbook by Liang, 10th edition) and implements the
Comparable
and
Cloneable
interfaces.
//GeometricObject.java: The abstract GeometricObject 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 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();
}
Assume that all eight sides of the octagon are of equal length. The area can be computed
using the following formula:
area
= (2 + 4 / sqrt(2)) *
side
*
side
Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and
Cloneable.
Write a test program (
OctagonTest.java
) that creates an Octagon object with side value
5.00 and displays its area and perimeter. Create a new object using the
clone
method and
compare the two objects using the
compareTo
method.
The output should look exactly like the following:
Area of the octagon with side value 5.00 is 120.71
Perimeter of the octagon with side value 5.00 is 40.00
Comparison result between an octagon and its clone: 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
