Skip to content

Commit 9e36810

Browse files
authored
Add surface area of Cone and Hemisphere (TheAlgorithms#2791)
1 parent 664bcf4 commit 9e36810

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Maths/Area.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public static void main(String[] args) {
3131

3232
/* test cylinder */
3333
assert Double.compare(surfaceAreaCylinder(1,2), 18.84955592153876) == 0;
34+
35+
/* test hemisphere */
36+
assert Double.compare(surfaceAreaHemisphere(5), 235.61944901923448) == 0;
37+
assert Double.compare(surfaceAreaHemisphere(1), 9.42477796076938) == 0;
38+
39+
/* test cone */
40+
assert Double.compare(surfaceAreaCone(6, 8), 301.59289474462014) == 0;
41+
assert Double.compare(surfaceAreaCone(10, 24), 1130.9733552923256) == 0;
42+
3443
}
3544

3645
/**
@@ -127,4 +136,25 @@ private static double surfaceAreaTrapezium(double base1, double base2, double he
127136
private static double surfaceAreaCircle(double radius) {
128137
return Math.PI * radius * radius;
129138
}
139+
140+
/**
141+
* Calculate the surface area of a hemisphere.
142+
*
143+
* @param radius radius of hemisphere
144+
* @return surface area of given hemisphere
145+
*/
146+
private static double surfaceAreaHemisphere(double radius) {
147+
return 3 * Math.PI * radius * radius;
148+
}
149+
150+
/**
151+
* Calculate the surface area of a cone.
152+
*
153+
* @param radius radius of cone.
154+
* @param height of cone.
155+
* @return surface area of given cone.
156+
*/
157+
private static double surfaceAreaCone(double radius, double height) {
158+
return Math.PI * radius * (radius + Math.pow((height * height + radius * radius), 0.5));
159+
}
130160
}

0 commit comments

Comments
 (0)