Write a class named Hexagon whose objects represent regular hexagons (6-sided polygons). Your class should implement the Shape interface defined in this chapter.
package inheritance;
// the Hexagon class implemetns the Shape interface
public class Hexagon implements Shape {
double sideLength;
// create constructor which takes the sidelength as argument
public Hexagon(double sideLength) {
this.sideLength = sideLength;
}
// below method return the area Hexagon
@Override
public double getArea() {
return ((Math.sqrt(3) * 3) / 2) * sideLength * sideLength;
}
@Override
public double getPerimeter() {
return 6 * sideLength;
}
public static void main(String args[]) {
Hexagon hex = new Hexagon(6);
System.out.println("Area of hexagon is: " + hex.getArea());
System.out.println("Perimeter of hexagon is: " + hex.getPerimeter());
}
}
Output:
Area of hexagon is: 93.53074360871938
Perimeter of hexagon is: 36.0