Skip to content

Commit ea43d70

Browse files
authored
Merge pull request TheAlgorithms#1417 from rbshealy/master
Add PythagoreanTriple.java and Add assert statements
2 parents eea5158 + 9a31b96 commit ea43d70

6 files changed

Lines changed: 54 additions & 6 deletions

File tree

Maths/AbsoluteMax.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
public class AbsoluteMax {
1212
public static void main(String[] args) {
13-
int[] numbers = new int[]{3, -10, -2};
13+
int[] testnums = {-2, 0, 16};
14+
assert absMax(testnums) == 16;
15+
16+
int[] numbers = {3, -10, -2};
1417
System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
1518
}
1619

Maths/AbsoluteMin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
public class AbsoluteMin {
1212
public static void main(String[] args) {
13-
int[] numbers = new int[]{3, -10, -2};
13+
int[] testnums = {4, 0, 16};
14+
assert absMin(testnums) == 0;
15+
16+
int[] numbers = {3, -10, -2};
1417
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
1518
}
1619

Maths/AbsoluteValue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package Maths;
22

3-
/**
4-
* @author PatOnTheBack
5-
*/
6-
73
public class AbsoluteValue {
84

95
public static void main(String[] args) {
6+
assert absVal(-13) == 13;
7+
assert absVal(0) == 0;
8+
assert absVal(100) == 100;
9+
1010
int value = -34;
1111
System.out.println("The absolute value of " + value + " is " + absVal(value));
1212
}

Maths/MaxValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static int max(int a, int b) {
1717
}
1818

1919
public static void main(String[] args) {
20+
assert max(-3,3) == 3;
21+
assert max(-6,-20) == -6;
22+
assert max(100,32) == 100;
23+
assert max(13,13) == 13;
24+
2025
int a = 3;
2126
int b = 4;
2227
System.out.format("max:%d between %d and %d", max(a, b), a, b);

Maths/MinValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static int min(int a, int b) {
1717
}
1818

1919
public static void main(String[] args) {
20+
assert min(-3,3) == -3;
21+
assert min(-6,-20) == -20;
22+
assert min(100,32) == 32;
23+
assert min(13,13) == 13;
24+
2025
int a = 3;
2126
int b = 4;
2227
System.out.format("min:%d between %d and %d", min(a, b), a, b);

Maths/PythagoreanTriple.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Pythagorean_triple
5+
*
6+
*/
7+
public class PythagoreanTriple {
8+
public static void main(String[] args) {
9+
assert isPythagTriple(3,4,5);
10+
assert isPythagTriple(5,12,13);
11+
assert isPythagTriple(6,8,10);
12+
assert !isPythagTriple(10,20,30);
13+
assert !isPythagTriple(6,8,100);
14+
assert !isPythagTriple(-1,-1,1);
15+
}
16+
17+
/**
18+
* Check if a,b,c are a Pythagorean Triple
19+
*
20+
* @param a x/y component length of a right triangle
21+
* @param b y/x component length of a right triangle
22+
* @param c hypotenuse length of a right triangle
23+
* @return boolean <tt>true</tt> if a, b, c satisfy the Pythagorean theorem, otherwise <tt>false</tt>
24+
*/
25+
public static boolean isPythagTriple(int a, int b, int c) {
26+
if(a <= 0 || b <= 0 || c <= 0) {
27+
return false;
28+
} else {
29+
return (a * a) + (b * b) == (c * c);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)