Skip to content

Commit 4ab0548

Browse files
* Added arithmetic series
* Fixed compare two double * Fiexed documentation
1 parent ddd98a9 commit 4ab0548

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

Maths/Area.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
*/
66
public class Area {
77
public static void main(String[] args) {
8+
89
/* test cube */
9-
assert surfaceAreaCube(1) == 6;
10+
assert Double.compare(surfaceAreaCube(1), 6.0) == 0;
1011

1112
/* test sphere */
12-
assert surfaceAreaSphere(5) == 314.1592653589793;
13-
assert surfaceAreaSphere(1) == 12.566370614359172;
13+
assert Double.compare(surfaceAreaSphere(5), 314.1592653589793) == 0;
14+
assert Double.compare(surfaceAreaSphere(1), 12.566370614359172) == 0;
1415

1516
/* test rectangle */
16-
assert surfaceAreaRectangle(10, 20) == 200;
17+
assert Double.compare(surfaceAreaRectangle(10, 20), 200.0) == 0;
1718

1819
/* test square */
19-
assert surfaceAreaSquare(10) == 100;
20+
assert Double.compare(surfaceAreaSquare(10), 100.0) == 0;
2021

2122
/* test triangle */
22-
assert surfaceAreaTriangle(10, 10) == 50;
23+
assert Double.compare(surfaceAreaTriangle(10, 10), 50.0) == 0;
2324

2425
/* test parallelogram */
25-
assert surfaceAreaParallelogram(10, 20) == 200;
26+
assert Double.compare(surfaceAreaParallelogram(10, 20), 200.0) == 0;
2627

2728
/* test trapezium */
28-
assert surfaceAreaTrapezium(10, 20, 30) == 450;
29+
assert Double.compare(surfaceAreaTrapezium(10, 20, 30), 450.0) == 0;
2930

3031
/* test circle */
31-
assert surfaceAreaCircle(20) == 1256.6370614359173;
32-
32+
assert Double.compare(surfaceAreaCircle(20), 1256.6370614359173) == 0;
3333
}
3434

3535
/**
@@ -38,7 +38,7 @@ public static void main(String[] args) {
3838
* @param sideLength side length of cube
3939
* @return surface area of given cube
4040
*/
41-
public static double surfaceAreaCube(double sideLength) {
41+
private static double surfaceAreaCube(double sideLength) {
4242
return 6 * sideLength * sideLength;
4343
}
4444

@@ -48,7 +48,7 @@ public static double surfaceAreaCube(double sideLength) {
4848
* @param radius radius of sphere
4949
* @return surface area of given sphere
5050
*/
51-
public static double surfaceAreaSphere(double radius) {
51+
private static double surfaceAreaSphere(double radius) {
5252
return 4 * Math.PI * radius * radius;
5353
}
5454

@@ -59,7 +59,7 @@ public static double surfaceAreaSphere(double radius) {
5959
* @param width width of rectangle
6060
* @return area of given rectangle
6161
*/
62-
public static double surfaceAreaRectangle(double length, double width) {
62+
private static double surfaceAreaRectangle(double length, double width) {
6363
return length * width;
6464
}
6565

@@ -69,7 +69,7 @@ public static double surfaceAreaRectangle(double length, double width) {
6969
* @param sideLength side length of square
7070
* @return area of given square
7171
*/
72-
public static double surfaceAreaSquare(double sideLength) {
72+
private static double surfaceAreaSquare(double sideLength) {
7373
return sideLength * sideLength;
7474
}
7575

@@ -80,7 +80,7 @@ public static double surfaceAreaSquare(double sideLength) {
8080
* @param height height of triangle
8181
* @return area of given triangle
8282
*/
83-
public static double surfaceAreaTriangle(double base, double height) {
83+
private static double surfaceAreaTriangle(double base, double height) {
8484
return base * height / 2;
8585
}
8686

@@ -91,7 +91,7 @@ public static double surfaceAreaTriangle(double base, double height) {
9191
* @param height height of parallelogram
9292
* @return area of given parallelogram
9393
*/
94-
public static double surfaceAreaParallelogram(double base, double height) {
94+
private static double surfaceAreaParallelogram(double base, double height) {
9595
return base * height;
9696
}
9797

@@ -103,7 +103,7 @@ public static double surfaceAreaParallelogram(double base, double height) {
103103
* @param height height of trapezium
104104
* @return area of given trapezium
105105
*/
106-
public static double surfaceAreaTrapezium(double base1, double base2, double height) {
106+
private static double surfaceAreaTrapezium(double base1, double base2, double height) {
107107
return (base1 + base2) * height / 2;
108108
}
109109

@@ -113,7 +113,7 @@ public static double surfaceAreaTrapezium(double base1, double base2, double hei
113113
* @param radius radius of circle
114114
* @return area of given circle
115115
*/
116-
public static double surfaceAreaCircle(double radius) {
116+
private static double surfaceAreaCircle(double radius) {
117117
return Math.PI * radius * radius;
118118
}
119-
}
119+
}

Maths/PowRecursion.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
public class PowRecursion {
44
public static void main(String[] args) {
5-
assert pow(2, 0) == Math.pow(2, 0);
6-
assert pow(0, 2) == Math.pow(0, 2);
7-
assert pow(2, 10) == Math.pow(2, 10);
8-
assert pow(10, 2) == Math.pow(10, 2);
5+
assert Double.compare(pow(2, 0), Math.pow(2, 0)) == 0;
6+
assert Double.compare(pow(0, 2), Math.pow(0, 2)) == 0;
7+
assert Double.compare(pow(2, 10), Math.pow(2, 10)) == 0;
8+
assert Double.compare(pow(10, 2), Math.pow(10, 2)) == 0;
99
}
1010

1111
/**
@@ -17,10 +17,6 @@ public static void main(String[] args) {
1717
* @return the value {@code a}<sup>{@code b}</sup>.
1818
*/
1919
public static long pow(int a, int b) {
20-
if (b == 0) {
21-
return 1;
22-
} else {
23-
return a * pow(a, b - 1);
24-
}
20+
return b == 0 ? 1 : a * pow(a, b - 1);
2521
}
26-
}
22+
}

Maths/SumOfArithmeticSeries.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Maths;
2+
3+
/**
4+
* <p>
5+
* In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the
6+
* difference between the consecutive terms is constant. Difference here means the second minus the first.
7+
* For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with common difference of 2.
8+
* <p>
9+
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
10+
*/
11+
public class SumOfArithmeticSeries {
12+
public static void main(String[] args) {
13+
14+
/* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 */
15+
assert Double.compare(55.0, sumOfSeries(1, 1, 10)) == 0;
16+
17+
/* 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 */
18+
assert Double.compare(100.0, sumOfSeries(1, 2, 10)) == 0;
19+
20+
/* 1 + 11 + 21 + 31 + 41 + 51 + 61 + 71 + 81 + 91 */
21+
assert Double.compare(460.0, sumOfSeries(1, 10, 10)) == 0;
22+
23+
/* 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 0.6 + 0.7 + 0.8 + 0.9 + 1.0 */
24+
assert Double.compare(5.5, sumOfSeries(0.1, 0.1, 10)) == 0;
25+
26+
assert Double.compare(49600.0, sumOfSeries(1, 10, 100)) == 0;
27+
}
28+
29+
/**
30+
* Calculate sum of arithmetic series
31+
*
32+
* @param firstTerm the initial term of an arithmetic series
33+
* @param commonDiff the common difference of an arithmetic series
34+
* @param numOfTerms the total terms of an arithmetic series
35+
* @return sum of given arithmetic series
36+
*/
37+
private static double sumOfSeries(double firstTerm, double commonDiff, int numOfTerms) {
38+
return numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff);
39+
}
40+
}

0 commit comments

Comments
 (0)