* Added arithmetic series
* Fixed compare two double * Fiexed documentation
This commit is contained in:
parent
ddd98a9a4c
commit
4ab05480e2
@ -5,31 +5,31 @@ package Maths;
|
|||||||
*/
|
*/
|
||||||
public class Area {
|
public class Area {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
/* test cube */
|
/* test cube */
|
||||||
assert surfaceAreaCube(1) == 6;
|
assert Double.compare(surfaceAreaCube(1), 6.0) == 0;
|
||||||
|
|
||||||
/* test sphere */
|
/* test sphere */
|
||||||
assert surfaceAreaSphere(5) == 314.1592653589793;
|
assert Double.compare(surfaceAreaSphere(5), 314.1592653589793) == 0;
|
||||||
assert surfaceAreaSphere(1) == 12.566370614359172;
|
assert Double.compare(surfaceAreaSphere(1), 12.566370614359172) == 0;
|
||||||
|
|
||||||
/* test rectangle */
|
/* test rectangle */
|
||||||
assert surfaceAreaRectangle(10, 20) == 200;
|
assert Double.compare(surfaceAreaRectangle(10, 20), 200.0) == 0;
|
||||||
|
|
||||||
/* test square */
|
/* test square */
|
||||||
assert surfaceAreaSquare(10) == 100;
|
assert Double.compare(surfaceAreaSquare(10), 100.0) == 0;
|
||||||
|
|
||||||
/* test triangle */
|
/* test triangle */
|
||||||
assert surfaceAreaTriangle(10, 10) == 50;
|
assert Double.compare(surfaceAreaTriangle(10, 10), 50.0) == 0;
|
||||||
|
|
||||||
/* test parallelogram */
|
/* test parallelogram */
|
||||||
assert surfaceAreaParallelogram(10, 20) == 200;
|
assert Double.compare(surfaceAreaParallelogram(10, 20), 200.0) == 0;
|
||||||
|
|
||||||
/* test trapezium */
|
/* test trapezium */
|
||||||
assert surfaceAreaTrapezium(10, 20, 30) == 450;
|
assert Double.compare(surfaceAreaTrapezium(10, 20, 30), 450.0) == 0;
|
||||||
|
|
||||||
/* test circle */
|
/* test circle */
|
||||||
assert surfaceAreaCircle(20) == 1256.6370614359173;
|
assert Double.compare(surfaceAreaCircle(20), 1256.6370614359173) == 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +38,7 @@ public class Area {
|
|||||||
* @param sideLength side length of cube
|
* @param sideLength side length of cube
|
||||||
* @return surface area of given cube
|
* @return surface area of given cube
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaCube(double sideLength) {
|
private static double surfaceAreaCube(double sideLength) {
|
||||||
return 6 * sideLength * sideLength;
|
return 6 * sideLength * sideLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ public class Area {
|
|||||||
* @param radius radius of sphere
|
* @param radius radius of sphere
|
||||||
* @return surface area of given sphere
|
* @return surface area of given sphere
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaSphere(double radius) {
|
private static double surfaceAreaSphere(double radius) {
|
||||||
return 4 * Math.PI * radius * radius;
|
return 4 * Math.PI * radius * radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ public class Area {
|
|||||||
* @param width width of rectangle
|
* @param width width of rectangle
|
||||||
* @return area of given rectangle
|
* @return area of given rectangle
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaRectangle(double length, double width) {
|
private static double surfaceAreaRectangle(double length, double width) {
|
||||||
return length * width;
|
return length * width;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ public class Area {
|
|||||||
* @param sideLength side length of square
|
* @param sideLength side length of square
|
||||||
* @return area of given square
|
* @return area of given square
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaSquare(double sideLength) {
|
private static double surfaceAreaSquare(double sideLength) {
|
||||||
return sideLength * sideLength;
|
return sideLength * sideLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ public class Area {
|
|||||||
* @param height height of triangle
|
* @param height height of triangle
|
||||||
* @return area of given triangle
|
* @return area of given triangle
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaTriangle(double base, double height) {
|
private static double surfaceAreaTriangle(double base, double height) {
|
||||||
return base * height / 2;
|
return base * height / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ public class Area {
|
|||||||
* @param height height of parallelogram
|
* @param height height of parallelogram
|
||||||
* @return area of given parallelogram
|
* @return area of given parallelogram
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaParallelogram(double base, double height) {
|
private static double surfaceAreaParallelogram(double base, double height) {
|
||||||
return base * height;
|
return base * height;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ public class Area {
|
|||||||
* @param height height of trapezium
|
* @param height height of trapezium
|
||||||
* @return area of given trapezium
|
* @return area of given trapezium
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaTrapezium(double base1, double base2, double height) {
|
private static double surfaceAreaTrapezium(double base1, double base2, double height) {
|
||||||
return (base1 + base2) * height / 2;
|
return (base1 + base2) * height / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ public class Area {
|
|||||||
* @param radius radius of circle
|
* @param radius radius of circle
|
||||||
* @return area of given circle
|
* @return area of given circle
|
||||||
*/
|
*/
|
||||||
public static double surfaceAreaCircle(double radius) {
|
private static double surfaceAreaCircle(double radius) {
|
||||||
return Math.PI * radius * radius;
|
return Math.PI * radius * radius;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,10 +2,10 @@ package Maths;
|
|||||||
|
|
||||||
public class PowRecursion {
|
public class PowRecursion {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
assert pow(2, 0) == Math.pow(2, 0);
|
assert Double.compare(pow(2, 0), Math.pow(2, 0)) == 0;
|
||||||
assert pow(0, 2) == Math.pow(0, 2);
|
assert Double.compare(pow(0, 2), Math.pow(0, 2)) == 0;
|
||||||
assert pow(2, 10) == Math.pow(2, 10);
|
assert Double.compare(pow(2, 10), Math.pow(2, 10)) == 0;
|
||||||
assert pow(10, 2) == Math.pow(10, 2);
|
assert Double.compare(pow(10, 2), Math.pow(10, 2)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,10 +17,6 @@ public class PowRecursion {
|
|||||||
* @return the value {@code a}<sup>{@code b}</sup>.
|
* @return the value {@code a}<sup>{@code b}</sup>.
|
||||||
*/
|
*/
|
||||||
public static long pow(int a, int b) {
|
public static long pow(int a, int b) {
|
||||||
if (b == 0) {
|
return b == 0 ? 1 : a * pow(a, b - 1);
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return a * pow(a, b - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
40
Maths/SumOfArithmeticSeries.java
Normal file
40
Maths/SumOfArithmeticSeries.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package Maths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the
|
||||||
|
* difference between the consecutive terms is constant. Difference here means the second minus the first.
|
||||||
|
* For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with common difference of 2.
|
||||||
|
* <p>
|
||||||
|
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
|
||||||
|
*/
|
||||||
|
public class SumOfArithmeticSeries {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
/* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 */
|
||||||
|
assert Double.compare(55.0, sumOfSeries(1, 1, 10)) == 0;
|
||||||
|
|
||||||
|
/* 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 */
|
||||||
|
assert Double.compare(100.0, sumOfSeries(1, 2, 10)) == 0;
|
||||||
|
|
||||||
|
/* 1 + 11 + 21 + 31 + 41 + 51 + 61 + 71 + 81 + 91 */
|
||||||
|
assert Double.compare(460.0, sumOfSeries(1, 10, 10)) == 0;
|
||||||
|
|
||||||
|
/* 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 0.6 + 0.7 + 0.8 + 0.9 + 1.0 */
|
||||||
|
assert Double.compare(5.5, sumOfSeries(0.1, 0.1, 10)) == 0;
|
||||||
|
|
||||||
|
assert Double.compare(49600.0, sumOfSeries(1, 10, 100)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate sum of arithmetic series
|
||||||
|
*
|
||||||
|
* @param firstTerm the initial term of an arithmetic series
|
||||||
|
* @param commonDiff the common difference of an arithmetic series
|
||||||
|
* @param numOfTerms the total terms of an arithmetic series
|
||||||
|
* @return sum of given arithmetic series
|
||||||
|
*/
|
||||||
|
private static double sumOfSeries(double firstTerm, double commonDiff, int numOfTerms) {
|
||||||
|
return numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user