Write a class named Octagon (Octagon.java) that extends the following abstract G
ID: 3912369 • 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 Geometric Object {
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(booleanfilled) {
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
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Octagon.java
------
public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {
private double side;
public Octagon() {
super();
}
public Octagon(double side, String color, boolean filled) {
super(color, filled);
this.side = side;
}
public void setSide(double s){
side = s;
}
public double getSide(){
return side;
}
@Override
public double getArea() {
double area = (2 + 4 / Math.sqrt(2)) * side * side;
return area;
}
@Override
public double getPerimeter() {
return 8 * side;
}
@Override
public int compareTo(Octagon o) {
if(side == o.side)
return 0;
else if(side > o.side)
return 1;
else
return -1;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return new Octagon(side, getColor(), isFilled());
}
}
OctagonTest.java
------
public class OctagonTest {
public static void main(String[] args) throws CloneNotSupportedException {
Octagon oct = new Octagon(5, "blue", true);
Octagon clone = (Octagon) oct.clone();
System.out.printf("Area of the octagon with side value %.2f is %.2f ", oct.getSide(), oct.getArea());
System.out.printf("Perimeter of the octagon with side value %.2f is %.2f ", oct.getSide(), oct.getPerimeter());
System.out.printf("Comparison result between an octagon and its clone: %d ", oct.compareTo(clone));
}
}
output
-----
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.