Write a class named Octagon ( Octagon.java ) that extends the following abstract
ID: 3912212 • Letter: W
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 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
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Octagon.java
public class Octagon extends GeometricObject implements Comparable<Octagon>,
Cloneable {
// attribute
private double side;
/**
* constructor with value to initialize the side field
*
* @param side
* - side of an octagon (all sides equal)
*/
public Octagon(double side) {
super();
this.side = side;
}
/**
* constructor with value to initialize the side field, and also the color
* and filled fields of super class
*/
public Octagon(double side, String color, boolean filled) {
// passing values to super class
super(color, filled);
this.side = side;
}
@Override
public int compareTo(Octagon other) {
/**
* comparing by area
*/
if (this.getArea() > other.getArea()) {
return -1;
} else if (this.getArea() < other.getArea()) {
return 1;
}
return 0; // equal area
}
@Override
public double getArea() {
/**
* calculating and returning area of octagon using the equation
*/
double area = (2.0 + 4.0 / (Math.sqrt(2))) * side * side;
return area;
}
@Override
protected Octagon clone() {
/**
* Defining a new Octagon with all values similar to this object and
* returning it
*/
Octagon octagon = new Octagon(side, getColor(), isFilled());
return octagon;
}
@Override
public double getPerimeter() {
/**
* perimeter of octagon= 8*side
*/
return 8.0 * side;
}
@Override
public String toString() {
return "Octagon with side: " + side + " " + super.toString();
}
}
// OctagonTest.java
public class OctagonTest {
public static void main(String[] args) {
/**
* creating an octagon with side 5.00
*/
Octagon octagon1=new Octagon(5);
/**
* displaying area and perimeter
*/
System.out.printf("%.2f ",octagon1.getArea());
System.out.printf("%.2f ",octagon1.getPerimeter());
/**
* cloning the octagon to create another one
*/
Octagon octagon2=octagon1.clone();
/**
* displaying the comparison result
*/
System.out.println(octagon1.compareTo(octagon2));
}
}
/*OUTPUT*/
120.71
40.00
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.