- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathArea.java
195 lines (180 loc) · 5.77 KB
/
Area.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
packagecom.thealgorithms.maths;
/**
* Find the area of various geometric shapes
*/
publicfinalclassArea {
privateArea() {
}
/**
* String of IllegalArgumentException for radius
*/
privatestaticfinalStringPOSITIVE_RADIUS = "Must be a positive radius";
/**
* String of IllegalArgumentException for height
*/
privatestaticfinalStringPOSITIVE_HEIGHT = "Must be a positive height";
/**
* String of IllegalArgumentException for base
*/
privatestaticfinalStringPOSITIVE_BASE = "Must be a positive base";
/**
* Calculate the surface area of a cube.
*
* @param sideLength side length of cube
* @return surface area of given cube
*/
publicstaticdoublesurfaceAreaCube(finaldoublesideLength) {
if (sideLength <= 0) {
thrownewIllegalArgumentException("Must be a positive sideLength");
}
return6 * sideLength * sideLength;
}
/**
* Calculate the surface area of a sphere.
*
* @param radius radius of sphere
* @return surface area of given sphere
*/
publicstaticdoublesurfaceAreaSphere(finaldoubleradius) {
if (radius <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
return4 * Math.PI * radius * radius;
}
/**
* Calculate the area of a rectangle.
*
* @param length length of a rectangle
* @param width width of a rectangle
* @return area of given rectangle
*/
publicstaticdoublesurfaceAreaRectangle(finaldoublelength, finaldoublewidth) {
if (length <= 0) {
thrownewIllegalArgumentException("Must be a positive length");
}
if (width <= 0) {
thrownewIllegalArgumentException("Must be a positive width");
}
returnlength * width;
}
/**
* Calculate surface area of a cylinder.
*
* @param radius radius of the floor
* @param height height of the cylinder.
* @return volume of given cylinder
*/
publicstaticdoublesurfaceAreaCylinder(finaldoubleradius, finaldoubleheight) {
if (radius <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
if (height <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
return2 * (Math.PI * radius * radius + Math.PI * radius * height);
}
/**
* Calculate the area of a square.
*
* @param sideLength side length of square
* @return area of given square
*/
publicstaticdoublesurfaceAreaSquare(finaldoublesideLength) {
if (sideLength <= 0) {
thrownewIllegalArgumentException("Must be a positive sideLength");
}
returnsideLength * sideLength;
}
/**
* Calculate the area of a triangle.
*
* @param base base of triangle
* @param height height of triangle
* @return area of given triangle
*/
publicstaticdoublesurfaceAreaTriangle(finaldoublebase, finaldoubleheight) {
if (base <= 0) {
thrownewIllegalArgumentException(POSITIVE_BASE);
}
if (height <= 0) {
thrownewIllegalArgumentException(POSITIVE_HEIGHT);
}
returnbase * height / 2;
}
/**
* Calculate the area of a parallelogram.
*
* @param base base of a parallelogram
* @param height height of a parallelogram
* @return area of given parallelogram
*/
publicstaticdoublesurfaceAreaParallelogram(finaldoublebase, finaldoubleheight) {
if (base <= 0) {
thrownewIllegalArgumentException(POSITIVE_BASE);
}
if (height <= 0) {
thrownewIllegalArgumentException(POSITIVE_HEIGHT);
}
returnbase * height;
}
/**
* Calculate the area of a trapezium.
*
* @param base1 upper base of trapezium
* @param base2 bottom base of trapezium
* @param height height of trapezium
* @return area of given trapezium
*/
publicstaticdoublesurfaceAreaTrapezium(finaldoublebase1, finaldoublebase2, finaldoubleheight) {
if (base1 <= 0) {
thrownewIllegalArgumentException(POSITIVE_BASE + 1);
}
if (base2 <= 0) {
thrownewIllegalArgumentException(POSITIVE_BASE + 2);
}
if (height <= 0) {
thrownewIllegalArgumentException(POSITIVE_HEIGHT);
}
return (base1 + base2) * height / 2;
}
/**
* Calculate the area of a circle.
*
* @param radius radius of circle
* @return area of given circle
*/
publicstaticdoublesurfaceAreaCircle(finaldoubleradius) {
if (radius <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
returnMath.PI * radius * radius;
}
/**
* Calculate the surface area of a hemisphere.
*
* @param radius radius of hemisphere
* @return surface area of given hemisphere
*/
publicstaticdoublesurfaceAreaHemisphere(finaldoubleradius) {
if (radius <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
return3 * Math.PI * radius * radius;
}
/**
* Calculate the surface area of a cone.
*
* @param radius radius of cone.
* @param height of cone.
* @return surface area of given cone.
*/
publicstaticdoublesurfaceAreaCone(finaldoubleradius, finaldoubleheight) {
if (radius <= 0) {
thrownewIllegalArgumentException(POSITIVE_RADIUS);
}
if (height <= 0) {
thrownewIllegalArgumentException(POSITIVE_HEIGHT);
}
returnMath.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
}
}