Write a class named Hexagon that extends GeometricObject and implements the Comp
ID: 3805899 • Letter: W
Question
Write a class named Hexagon that extends GeometricObject and implements the Comparable interface. Assume all six sides of the hexagon are of equal size. The Hexagon class is defined as follows:
public class Hexagon extends GeometricObject implements Cloneable, Comparable { private double side; /** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it }
@Override public double getArea() { // Implement it () }
@Override public double getPerimeter() { // Implement it }
@Override public int compareTo(Hexagon obj) { // Implement it (compare two Hexagons based on their sides) }
@Override public Object clone() { // Implement it } }
Explanation / Answer
Hexagon.java
public class Hexagon extends GeometricObject implements
Comparable , Cloneable {
private double side;
/** Constructs a Hexagon with the specified side */
public Hexagon(double side) {
// Implement it
this.side = side;
}
/** Implement the abstract method getArea */
public double getArea() {
// The area = 1.5 * side * side * the-square-root-of-3
// Implement it
return 1.5 * Math.sqrt(3) * side *side;
}
/** Implement the abstract method getPerimeter */
public double getPerimeter() {
// Implement it
return 6 * side;
}
/** Implement the compareTo method */
public int compareTo(Object obj) {
// Compares two Hexagons based on their sides
// Implement it
Hexagon h = (Hexagon)obj;
if (h.side < this.side) return -1;
if (h.side > this.side) return 1;
return 0;
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.