Question
Using the code below: // construct an Octagon // clone an Octagon // Write code to output area and perimeter, and compare Octagon objects o1
Using the code below:
// construct an Octagon
// clone an Octagon
// Write code to output area and perimeter, and compare Octagon objects o1 and o2.
public class Octagon {
private double side;
public Octagon() {
this.side = 1.0;
}
public Octagon(double side) {
this.side = side;
}
public double getArea() {
return (2 + 4 / Math.sqrt(2)) * side * side;
}
public double getPerimeter() {
return 8 * side;
}
public int compareTo(Object obj) {
if (this.getArea() > ((Octagon) obj).getArea()) {
return 1;
} else if (this.getArea() < ((Octagon) obj).getArea()) {
return -1;
} else {
return 0;
}
}
public boolean equals(Object obj) {
return this.side == ((Octagon) obj).side;
}
@Override
public Octagon clone() { //
try {
return (Octagon) super.clone();
} catch (CloneNotSupportedException ex) {
return null;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started