-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPentagon.java
More file actions
41 lines (34 loc) · 1.02 KB
/
Pentagon.java
File metadata and controls
41 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// subclass of polygon
public class Pentagon extends Polygon {
private double sideLength;
private double apothem; // perpendicular distance from center to the middle of any side
public Pentagon(double sideLength, double apothem) {
super(5);
this.sideLength = sideLength;
this.apothem = apothem;
}
@Override
public String getName() {
return "Pentagon";
}
@Override
public double getPerimeter() {
return 5 * sideLength;
}
@Override
public double getArea() {
// Area of a regular polygon: (perimeter * apothem) / 2
return 0.5 * getPerimeter() * apothem;
}
@Override
public String toString() {
return String.format(
"Pentagon: sideLength: %.2f, Apothem: %.2f, Sides: %d, Perimeter: %.2f, Area: %.2f",
sideLength,
apothem,
getSides(),
getPerimeter(),
getArea()
);
}
}