33.3k views
5 votes
Question 2: [20 points] Write a class named Hexagon that extends GeometricObject and has the following specification. Assume all six sides of the hexagon are of equal size. public class Hexagon extends GeometricObject { private double side; /** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it } /** Implement the abstract method findArea in GeometricObject */ public double findArea() { // Implement it ( ) } /** Implement the abstract method findPerimeter in GeometricObject */ public double findPerimeter() { // Implement it } }

User Addis
by
4.9k points

1 Answer

4 votes

Answer:

see explaination

Step-by-step explanation:

public Hexagon(double side)

{

// save constructor parameters in instance variables

this.side = side;

}

public double findArea()

{

// area of a hexagon is side*3sqrt(3)/2

return side*1.5*Math.sqrt(3);

}

public double findPerimeter()

{

// a hexagon has 6 sides

return side*6;

}

public int compareTo(Object obj)

{

// compare 2 hexagons based on area

// equivalent to comparing by side

Hexagon rhs = (Hexagon)obj;

if(side > rhs.side)

return 1;

else if(side < rhs.side)

return -1;

else

return 0;

}

User Carlosvini
by
6.0k points