Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/main/java/com/thealgorithms/maths/Area.java at master · datpp/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
datpp
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
Area.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
235 lines (218 loc) · 7.33 KB
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
Area.java
Copy path
Top
File metadata and controls
Code
Blame
235 lines (218 loc) · 7.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package
com
.
thealgorithms
.
maths
;
/**
* Find the area of various geometric shapes
*/
public
final
class
Area
{
private
Area
() {
}
/**
* String of IllegalArgumentException for radius
*/
private
static
final
String
POSITIVE_RADIUS
=
"Radius must be greater than 0"
;
/**
* String of IllegalArgumentException for height
*/
private
static
final
String
POSITIVE_HEIGHT
=
"Height must be greater than 0"
;
/**
* String of IllegalArgumentException for base
*/
private
static
final
String
POSITIVE_BASE
=
"Base must be greater than 0"
;
/**
* Calculate the surface area of a cube.
*
* @param sideLength side length of cube
* @return surface area of given cube
*/
public
static
double
surfaceAreaCube
(
final
double
sideLength
) {
if
(
sideLength
<=
0
) {
throw
new
IllegalArgumentException
(
"Side length must be greater than 0"
);
}
return
6
*
sideLength
*
sideLength
;
}
/**
* Calculate the surface area of a cuboid.
*
* @param length length of the cuboid
* @param width width of the cuboid
* @param height height of the cuboid
* @return surface area of given cuboid
*/
public
static
double
surfaceAreaCuboid
(
final
double
length
,
double
width
,
double
height
) {
if
(
length
<=
0
) {
throw
new
IllegalArgumentException
(
"Length must be greater than 0"
);
}
if
(
width
<=
0
) {
throw
new
IllegalArgumentException
(
"Width must be greater than 0"
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
"Height must be greater than 0"
);
}
return
2
* (
length
*
width
+
length
*
height
+
width
*
height
);
}
/**
* Calculate the surface area of a sphere.
*
* @param radius radius of sphere
* @return surface area of given sphere
*/
public
static
double
surfaceAreaSphere
(
final
double
radius
) {
if
(
radius
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_RADIUS
);
}
return
4
*
Math
.
PI
*
radius
*
radius
;
}
/**
* Calculate the surface area of a pyramid with a square base.
*
* @param sideLength side length of the square base
* @param slantHeight slant height of the pyramid
* @return surface area of the given pyramid
*/
public
static
double
surfaceAreaPyramid
(
final
double
sideLength
,
final
double
slantHeight
) {
if
(
sideLength
<=
0
) {
throw
new
IllegalArgumentException
(
""
);
}
if
(
slantHeight
<=
0
) {
throw
new
IllegalArgumentException
(
"slant height must be greater than 0"
);
}
double
baseArea
=
sideLength
*
sideLength
;
double
lateralSurfaceArea
=
2
*
sideLength
*
slantHeight
;
return
baseArea
+
lateralSurfaceArea
;
}
/**
* Calculate the area of a rectangle.
*
* @param length length of a rectangle
* @param width width of a rectangle
* @return area of given rectangle
*/
public
static
double
surfaceAreaRectangle
(
final
double
length
,
final
double
width
) {
if
(
length
<=
0
) {
throw
new
IllegalArgumentException
(
"Length must be greater than 0"
);
}
if
(
width
<=
0
) {
throw
new
IllegalArgumentException
(
"Width must be greater than 0"
);
}
return
length
*
width
;
}
/**
* Calculate surface area of a cylinder.
*
* @param radius radius of the floor
* @param height height of the cylinder.
* @return volume of given cylinder
*/
public
static
double
surfaceAreaCylinder
(
final
double
radius
,
final
double
height
) {
if
(
radius
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_RADIUS
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_HEIGHT
);
}
return
2
* (
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
*/
public
static
double
surfaceAreaSquare
(
final
double
sideLength
) {
if
(
sideLength
<=
0
) {
throw
new
IllegalArgumentException
(
"Side Length must be greater than 0"
);
}
return
sideLength
*
sideLength
;
}
/**
* Calculate the area of a triangle.
*
* @param base base of triangle
* @param height height of triangle
* @return area of given triangle
*/
public
static
double
surfaceAreaTriangle
(
final
double
baseLength
,
final
double
height
) {
if
(
baseLength
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_BASE
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_HEIGHT
);
}
return
baseLength
*
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
*/
public
static
double
surfaceAreaParallelogram
(
final
double
baseLength
,
final
double
height
) {
if
(
baseLength
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_BASE
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_HEIGHT
);
}
return
baseLength
*
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
*/
public
static
double
surfaceAreaTrapezium
(
final
double
baseLength1
,
final
double
baseLength2
,
final
double
height
) {
if
(
baseLength1
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_BASE
+
1
);
}
if
(
baseLength2
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_BASE
+
2
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_HEIGHT
);
}
return
(
baseLength1
+
baseLength2
) *
height
/
2
;
}
/**
* Calculate the area of a circle.
*
* @param radius radius of circle
* @return area of given circle
*/
public
static
double
surfaceAreaCircle
(
final
double
radius
) {
if
(
radius
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_RADIUS
);
}
return
Math
.
PI
*
radius
*
radius
;
}
/**
* Calculate the surface area of a hemisphere.
*
* @param radius radius of hemisphere
* @return surface area of given hemisphere
*/
public
static
double
surfaceAreaHemisphere
(
final
double
radius
) {
if
(
radius
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_RADIUS
);
}
return
3
*
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.
*/
public
static
double
surfaceAreaCone
(
final
double
radius
,
final
double
height
) {
if
(
radius
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_RADIUS
);
}
if
(
height
<=
0
) {
throw
new
IllegalArgumentException
(
POSITIVE_HEIGHT
);
}
return
Math
.
PI
*
radius
* (
radius
+
Math
.
pow
(
height
*
height
+
radius
*
radius
,
0.5
));
}
}
You can’t perform that action at this time.