Write a ctass named Hexagon that extends GeometricObject and implements the Comp
ID: 3537107 • Letter: W
Question
Write a ctass 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 Comparable {
private double side;
/** Construct a Hexagon with the specified side */
public Hexagon(double side){
//implemenl il
)
/** lmplement the abstract method getArea in GeometricObject ./
public double getAreafl {
// lmplemenlil(area =3 *3*Side*Side);
}
/** lmplement the abstract method getPerimeter in GeometricObject ./
public double getPerimeterQ {
// lmplement it
)
/*" lmplement the compareTo method in the Comparable interface to */
public int compareTo(Object obj) {
// lmplement it (compare two Hexagons based on their areas)
)
)
Explanation / Answer
public class Hexagon extends GeometricObject implements Comparable {
private double side;
/** Construct a Hexagon with the specified side */
public Hexagon(double side){
this.side=side;
}
/** lmplement the abstract method getArea in GeometricObject .*/
public double getArea() {
return (2.5981*side*side);
}
/** lmplement the abstract method getPerimeter in GeometricObject . */
public double getPerimeter() {
// lmplement it
return (6*side);
}
/*" lmplement the compareTo method in the Comparable interface to */
public int compareTo(Object obj) {
// lmplement it (compare two Hexagons based on their areas)
Hexagon h2=(Hexagon)obj;
if( this.getArea() > h2.getArea() )
{ return 1; }
else if (this.getArea() < h2.getArea() )
{ return (-1); }
else
{ return 0; }
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.