Skip to content

Commit a9c141f

Browse files
Merge pull request TheAlgorithms#1415 from shellhub/dev
Add more math algorithm
2 parents 1eb9dcb + affddc5 commit a9c141f

4 files changed

Lines changed: 147 additions & 22 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
* [AbsoluteMin](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteMin.java)
116116
* [AbsoluteValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteValue.java)
117117
* [AmicableNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/AmicableNumber.java)
118-
* [CountDigit](https://github.com/TheAlgorithms/Java/blob/master/Maths/CountDigit.java)
118+
* [Area](https://github.com/TheAlgorithms/Java/blob/master/Maths/Area.java)
119+
* [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java)
119120
* [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java)
120121
* [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java)
121122
* [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java)

Maths/Area.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package Maths;
2+
3+
/**
4+
* Find the area of various geometric shapes
5+
*/
6+
public class Area {
7+
public static void main(String[] args) {
8+
/* test cube */
9+
assert surfaceAreaCube(1) == 6;
10+
11+
/* test sphere */
12+
assert surfaceAreaSphere(5) == 314.1592653589793;
13+
assert surfaceAreaSphere(1) == 12.566370614359172;
14+
15+
/* test rectangle */
16+
assert surfaceAreaRectangle(10, 20) == 200;
17+
18+
/* test square */
19+
assert surfaceAreaSquare(10) == 100;
20+
21+
/* test triangle */
22+
assert surfaceAreaTriangle(10, 10) == 50;
23+
24+
/* test parallelogram */
25+
assert surfaceAreaParallelogram(10, 20) == 200;
26+
27+
/* test trapezium */
28+
assert surfaceAreaTrapezium(10, 20, 30) == 450;
29+
30+
/* test circle */
31+
assert surfaceAreaCircle(20) == 1256.6370614359173;
32+
33+
}
34+
35+
/**
36+
* Calculate the surface area of a cube.
37+
*
38+
* @param sideLength side length of cube
39+
* @return surface area of given cube
40+
*/
41+
public static double surfaceAreaCube(double sideLength) {
42+
return 6 * sideLength * sideLength;
43+
}
44+
45+
/**
46+
* Calculate the surface area of a sphere.
47+
*
48+
* @param radius radius of sphere
49+
* @return surface area of given sphere
50+
*/
51+
public static double surfaceAreaSphere(double radius) {
52+
return 4 * Math.PI * radius * radius;
53+
}
54+
55+
/**
56+
* Calculate the area of a rectangle
57+
*
58+
* @param length length of rectangle
59+
* @param width width of rectangle
60+
* @return area of given rectangle
61+
*/
62+
public static double surfaceAreaRectangle(double length, double width) {
63+
return length * width;
64+
}
65+
66+
/**
67+
* Calculate the area of a square
68+
*
69+
* @param sideLength side length of square
70+
* @return area of given square
71+
*/
72+
public static double surfaceAreaSquare(double sideLength) {
73+
return sideLength * sideLength;
74+
}
75+
76+
/**
77+
* Calculate the area of a triangle
78+
*
79+
* @param base base of triangle
80+
* @param height height of triangle
81+
* @return area of given triangle
82+
*/
83+
public static double surfaceAreaTriangle(double base, double height) {
84+
return base * height / 2;
85+
}
86+
87+
/**
88+
* Calculate the area of a parallelogram
89+
*
90+
* @param base base of parallelogram
91+
* @param height height of parallelogram
92+
* @return area of given parallelogram
93+
*/
94+
public static double surfaceAreaParallelogram(double base, double height) {
95+
return base * height;
96+
}
97+
98+
/**
99+
* Calculate the area of a trapezium
100+
*
101+
* @param base1 upper base of trapezium
102+
* @param base2 bottom base of trapezium
103+
* @param height height of trapezium
104+
* @return area of given trapezium
105+
*/
106+
public static double surfaceAreaTrapezium(double base1, double base2, double height) {
107+
return (base1 + base2) * height / 2;
108+
}
109+
110+
/**
111+
* Calculate the area of a circle
112+
*
113+
* @param radius radius of circle
114+
* @return area of given circle
115+
*/
116+
public static double surfaceAreaCircle(double radius) {
117+
return Math.PI * radius * radius;
118+
}
119+
}

Maths/Average.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Maths;
2+
3+
/**
4+
* Calculate average of a list of numbers
5+
*/
6+
public class Average {
7+
public static void main(String[] args) {
8+
assert average(new double[]{3, 6, 9, 12, 15, 18, 21}) == 12;
9+
assert average(new double[]{5, 10, 15, 20, 25, 30, 35}) == 20;
10+
assert average(new double[]{1, 2, 3, 4, 5, 6, 7, 8}) == 4.5;
11+
}
12+
13+
/**
14+
* Calculate average of a list of numbers
15+
*
16+
* @param numbers array to store numbers
17+
* @return mean of given numbers
18+
*/
19+
public static double average(double[] numbers) {
20+
double sum = 0;
21+
for (double number : numbers) {
22+
sum += number;
23+
}
24+
return sum / numbers.length;
25+
}
26+
}

Maths/CountDigit.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)