55 */
66public class Area {
77
8- public static void main (String [] args ) {
9- /* test cube */
10- assert Double .compare (surfaceAreaCube (1 ), 6.0 ) == 0 ;
11-
12- /* test sphere */
13- assert Double .compare (surfaceAreaSphere (5 ), 314.1592653589793 ) == 0 ;
14- assert Double .compare (surfaceAreaSphere (1 ), 12.566370614359172 ) == 0 ;
15-
16- /* test rectangle */
17- assert Double .compare (surfaceAreaRectangle (10 , 20 ), 200.0 ) == 0 ;
18-
19- /* test square */
20- assert Double .compare (surfaceAreaSquare (10 ), 100.0 ) == 0 ;
21-
22- /* test triangle */
23- assert Double .compare (surfaceAreaTriangle (10 , 10 ), 50.0 ) == 0 ;
24-
25- /* test parallelogram */
26- assert Double .compare (surfaceAreaParallelogram (10 , 20 ), 200.0 ) == 0 ;
27-
28- /* test trapezium */
29- assert Double .compare (surfaceAreaTrapezium (10 , 20 , 30 ), 450.0 ) == 0 ;
30-
31- /* test circle */
32- assert Double .compare (surfaceAreaCircle (20 ), 1256.6370614359173 ) == 0 ;
33-
34- /* test cylinder */
35- assert Double .compare (surfaceAreaCylinder (1 , 2 ), 18.84955592153876 ) ==
36- 0 ;
8+ /**
9+ * String of IllegalArgumentException for radius
10+ */
11+ private static final String POSITIVE_RADIUS = "Must be a positive radius" ;
3712
38- /* test hemisphere */
39- assert Double . compare ( surfaceAreaHemisphere ( 5 ), 235.61944901923448 ) ==
40- 0 ;
41- assert Double . compare ( surfaceAreaHemisphere ( 1 ), 9.42477796076938 ) == 0 ;
13+ /**
14+ * String of IllegalArgumentException for height
15+ */
16+ private static final String POSITIVE_HEIGHT = "Must be a positive height" ;
4217
43- /* test cone */
44- assert Double . compare ( surfaceAreaCone ( 6 , 8 ), 301.59289474462014 ) == 0 ;
45- assert Double . compare ( surfaceAreaCone ( 10 , 24 ), 1130.9733552923256 ) == 0 ;
46- }
18+ /**
19+ * String of IllegalArgumentException for base
20+ */
21+ private static final String POSITIVE_BASE = "Must be a positive base" ;
4722
4823 /**
4924 * Calculate the surface area of a cube.
5025 *
5126 * @param sideLength side length of cube
5227 * @return surface area of given cube
5328 */
54- private static double surfaceAreaCube (double sideLength ) {
29+ public static double surfaceAreaCube (final double sideLength ) {
30+ if (sideLength <= 0 ) {
31+ throw new IllegalArgumentException ("Must be a positive sideLength" );
32+ }
5533 return 6 * sideLength * sideLength ;
5634 }
5735
@@ -61,87 +39,125 @@ private static double surfaceAreaCube(double sideLength) {
6139 * @param radius radius of sphere
6240 * @return surface area of given sphere
6341 */
64- private static double surfaceAreaSphere (double radius ) {
42+ public static double surfaceAreaSphere (final double radius ) {
43+ if (radius <= 0 ) {
44+ throw new IllegalArgumentException (POSITIVE_RADIUS );
45+ }
6546 return 4 * Math .PI * radius * radius ;
6647 }
6748
6849 /**
69- * Calculate the area of a rectangle
50+ * Calculate the area of a rectangle.
7051 *
71- * @param length length of rectangle
72- * @param width width of rectangle
52+ * @param length length of a rectangle
53+ * @param width width of a rectangle
7354 * @return area of given rectangle
7455 */
75- private static double surfaceAreaRectangle (double length , double width ) {
56+ public static double surfaceAreaRectangle (final double length , final double width ) {
57+ if (length <= 0 ) {
58+ throw new IllegalArgumentException ("Must be a positive length" );
59+ }
60+ if (width <= 0 ) {
61+ throw new IllegalArgumentException ("Must be a positive width" );
62+ }
7663 return length * width ;
7764 }
7865
7966 /**
80- * Calculate surface area of a cylinder
67+ * Calculate surface area of a cylinder.
8168 *
8269 * @param radius radius of the floor
8370 * @param height height of the cylinder.
8471 * @return volume of given cylinder
8572 */
86- private static double surfaceAreaCylinder (double radius , double height ) {
73+ public static double surfaceAreaCylinder (final double radius , final double height ) {
74+ if (radius <= 0 ) {
75+ throw new IllegalArgumentException (POSITIVE_RADIUS );
76+ }
77+ if (height <= 0 ) {
78+ throw new IllegalArgumentException (POSITIVE_RADIUS );
79+ }
8780 return 2 * (Math .PI * radius * radius + Math .PI * radius * height );
8881 }
8982
9083 /**
91- * Calculate the area of a square
84+ * Calculate the area of a square.
9285 *
9386 * @param sideLength side length of square
9487 * @return area of given square
9588 */
96- private static double surfaceAreaSquare (double sideLength ) {
89+ public static double surfaceAreaSquare (final double sideLength ) {
90+ if (sideLength <= 0 ) {
91+ throw new IllegalArgumentException ("Must be a positive sideLength" );
92+ }
9793 return sideLength * sideLength ;
9894 }
9995
10096 /**
101- * Calculate the area of a triangle
97+ * Calculate the area of a triangle.
10298 *
103- * @param base base of triangle
99+ * @param base base of triangle
104100 * @param height height of triangle
105101 * @return area of given triangle
106102 */
107- private static double surfaceAreaTriangle (double base , double height ) {
103+ public static double surfaceAreaTriangleRectangle (final double base , final double height ) {
104+ if (base <= 0 ) {
105+ throw new IllegalArgumentException (POSITIVE_BASE );
106+ }
107+ if (height <= 0 ) {
108+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
109+ }
108110 return base * height / 2 ;
109111 }
110112
111113 /**
112- * Calculate the area of a parallelogram
114+ * Calculate the area of a parallelogram.
113115 *
114- * @param base base of parallelogram
115- * @param height height of parallelogram
116+ * @param base base of a parallelogram
117+ * @param height height of a parallelogram
116118 * @return area of given parallelogram
117119 */
118- private static double surfaceAreaParallelogram (double base , double height ) {
120+ public static double surfaceAreaParallelogram (final double base , final double height ) {
121+ if (base <= 0 ) {
122+ throw new IllegalArgumentException (POSITIVE_BASE );
123+ }
124+ if (height <= 0 ) {
125+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
126+ }
119127 return base * height ;
120128 }
121129
122130 /**
123- * Calculate the area of a trapezium
131+ * Calculate the area of a trapezium.
124132 *
125- * @param base1 upper base of trapezium
126- * @param base2 bottom base of trapezium
133+ * @param base1 upper base of trapezium
134+ * @param base2 bottom base of trapezium
127135 * @param height height of trapezium
128136 * @return area of given trapezium
129137 */
130- private static double surfaceAreaTrapezium (
131- double base1 ,
132- double base2 ,
133- double height
134- ) {
138+ public static double surfaceAreaTrapezium (final double base1 , final double base2 , final double height ) {
139+ if (base1 <= 0 ) {
140+ throw new IllegalArgumentException (POSITIVE_BASE + 1 );
141+ }
142+ if (base2 <= 0 ) {
143+ throw new IllegalArgumentException (POSITIVE_BASE + 2 );
144+ }
145+ if (height <= 0 ) {
146+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
147+ }
135148 return (base1 + base2 ) * height / 2 ;
136149 }
137150
138151 /**
139- * Calculate the area of a circle
152+ * Calculate the area of a circle.
140153 *
141154 * @param radius radius of circle
142155 * @return area of given circle
143156 */
144- private static double surfaceAreaCircle (double radius ) {
157+ public static double surfaceAreaCircle (final double radius ) {
158+ if (radius <= 0 ) {
159+ throw new IllegalArgumentException (POSITIVE_RADIUS );
160+ }
145161 return Math .PI * radius * radius ;
146162 }
147163
@@ -151,7 +167,10 @@ private static double surfaceAreaCircle(double radius) {
151167 * @param radius radius of hemisphere
152168 * @return surface area of given hemisphere
153169 */
154- private static double surfaceAreaHemisphere (double radius ) {
170+ public static double surfaceAreaHemisphere (final double radius ) {
171+ if (radius <= 0 ) {
172+ throw new IllegalArgumentException (POSITIVE_RADIUS );
173+ }
155174 return 3 * Math .PI * radius * radius ;
156175 }
157176
@@ -162,11 +181,13 @@ private static double surfaceAreaHemisphere(double radius) {
162181 * @param height of cone.
163182 * @return surface area of given cone.
164183 */
165- private static double surfaceAreaCone (double radius , double height ) {
166- return (
167- Math .PI *
168- radius *
169- (radius + Math .pow ((height * height + radius * radius ), 0.5 ))
170- );
184+ public static double surfaceAreaCone (final double radius , final double height ) {
185+ if (radius <= 0 ) {
186+ throw new IllegalArgumentException (POSITIVE_RADIUS );
187+ }
188+ if (height <= 0 ) {
189+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
190+ }
191+ return Math .PI * radius * (radius + Math .pow (height * height + radius * radius , 0.5 ));
171192 }
172193}
0 commit comments