Skip to content

Commit c771089

Browse files
test using rand numbers
1 parent d6947d0 commit c771089

9 files changed

+128
-65
lines changed

Maths/AbsoluteValue.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package Maths;
22

3+
import java.util.Random;
4+
35
public class AbsoluteValue {
46

57
public static void main(String[] args) {
6-
assert absVal(-13) == 13;
7-
assert absVal(0) == 0;
8-
assert absVal(100) == 100;
9-
10-
int value = -34;
11-
System.out.println("The absolute value of " + value + " is " + absVal(value));
8+
Random random = new Random();
9+
10+
/* test 1000 random numbers */
11+
for (int i = 1; i <= 1000; ++i) {
12+
int randomNumber = random.nextInt();
13+
assert absVal(randomNumber) == Math.abs(randomNumber);
14+
}
1215
}
1316

1417
/**

Maths/Ceil.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package Maths;
22

3+
import java.util.Random;
4+
35
public class Ceil {
46
public static void main(String[] args) {
5-
assert ceil(10) == Math.ceil(10);
6-
assert ceil(-10) == Math.ceil(-10);
7-
assert ceil(10.0) == Math.ceil(10.0);
8-
assert ceil(-10.0) == Math.ceil(-10.0);
9-
assert ceil(10.1) == Math.ceil(10.1);
10-
assert ceil(-10.1) == Math.ceil(-10.1);
11-
assert ceil(0) == Math.ceil(0);
12-
assert ceil(-0) == Math.ceil(-0);
13-
assert ceil(0.0) == Math.ceil(0.0);
14-
assert ceil(-0.0) == Math.ceil(-0.0);
7+
Random random = new Random();
8+
for (int i = 1; i <= 1000; ++i) {
9+
double randomNumber = random.nextDouble();
10+
assert ceil(randomNumber) == Math.ceil(randomNumber);
11+
}
1512
}
1613

1714
/**

Maths/FindMax.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package Maths;
22

3+
import java.util.Arrays;
4+
import java.util.Random;
5+
36
public class FindMax {
47

5-
//Driver
8+
/**
9+
* Driver Code
10+
*/
611
public static void main(String[] args) {
7-
int[] array = {2, 4, 9, 7, 19, 94, 5};
8-
assert findMax(array) == 94;
12+
Random random = new Random();
13+
14+
/* random size */
15+
int size = random.nextInt(100) + 1;
16+
int[] array = new int[size];
17+
18+
/* init array with random numbers */
19+
for (int i = 0; i < size; i++) {
20+
array[i] = random.nextInt() % 100;
21+
}
22+
23+
assert Arrays.stream(array).max().getAsInt() == findMax(array);
924
}
1025

1126
/**
1227
* find max of array
1328
*
1429
* @param array the array contains element
15-
* @return max value
30+
* @return max value of given array
1631
*/
1732
public static int findMax(int[] array) {
1833
int max = array[0];

Maths/FindMaxRecursion.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package Maths;
22

3+
import java.util.Arrays;
4+
import java.util.Random;
5+
36
public class FindMaxRecursion {
47
public static void main(String[] args) {
5-
int[] array = {2, 4, 9, 7, 19, 94, 5};
6-
int low = 0;
7-
int high = array.length - 1;
8+
Random rand = new Random();
9+
10+
/* rand size */
11+
int size = rand.nextInt(100) + 1;
12+
int[] array = new int[size];
13+
14+
/* init array with rand numbers */
15+
for (int i = 0; i < size; i++) {
16+
array[i] = rand.nextInt() % 100;
17+
}
818

9-
assert max(array, low, high) == 94;
10-
assert max(array, array.length) == 94;
19+
assert max(array, array.length) == Arrays.stream(array).max().getAsInt();
20+
assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt();
1121
}
1222

1323
/**

Maths/FindMin.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package Maths;
22

3+
import java.util.Arrays;
4+
import java.util.Random;
5+
36
public class FindMin {
47

5-
//Driver
8+
/**
9+
* Driver Code
10+
*/
611
public static void main(String[] args) {
7-
int[] array = {2, 4, 9, 7, 19, 94, 5};
8-
assert findMin(array) == 2;
12+
Random random = new Random();
13+
14+
/* random size */
15+
int size = random.nextInt(100) + 1;
16+
int[] array = new int[size];
17+
18+
/* init array with random numbers */
19+
for (int i = 0; i < size; i++) {
20+
array[i] = random.nextInt() % 100;
21+
}
22+
23+
assert Arrays.stream(array).min().getAsInt() == findMin(array);
924
}
1025

1126
/**

Maths/FindMinRecursion.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package Maths;
22

3+
import java.util.Arrays;
4+
import java.util.Random;
5+
36
public class FindMinRecursion {
7+
8+
/**
9+
* Driver Code
10+
*/
411
public static void main(String[] args) {
5-
int[] array = {2, 4, 9, -7, 19, 94, 5};
6-
int low = 0;
7-
int high = array.length - 1;
12+
Random rand = new Random();
13+
14+
/* rand size */
15+
int size = rand.nextInt(100) + 1;
16+
int[] array = new int[size];
17+
18+
/* init array with rand numbers */
19+
for (int i = 0; i < size; i++) {
20+
array[i] = rand.nextInt() % 100;
21+
}
822

9-
assert min(array, low, high) == -7;
10-
assert min(array, array.length) == -7;
23+
assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
24+
assert min(array, array.length) == Arrays.stream(array).min().getAsInt();
1125
}
1226

1327
/**

Maths/Floor.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package Maths;
22

3+
import java.util.Random;
4+
35
public class Floor {
46
public static void main(String[] args) {
5-
assert floor(10) == Math.floor(10);
6-
assert floor(-10) == Math.floor(-10);
7-
assert floor(10.0) == Math.floor(10.0);
8-
assert floor(-10.0) == Math.floor(-10.0);
9-
assert floor(10.1) == Math.floor(10.1);
10-
assert floor(-10.1) == Math.floor(-10.1);
11-
assert floor(0) == Math.floor(0);
12-
assert floor(-0) == Math.floor(-0);
13-
assert floor(0.0) == Math.floor(0.0);
14-
assert floor(-0.0) == Math.floor(-0.0);
7+
Random random = new Random();
8+
for (int i = 1; i <= 1000; ++i) {
9+
double randomNumber = random.nextDouble();
10+
assert floor(randomNumber) == Math.floor(randomNumber);
11+
}
1512
}
1613

1714
/**

Maths/MaxValue.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
package Maths;
22

3+
import java.util.Random;
4+
35
public class MaxValue {
46

7+
/**
8+
* Driver Code
9+
*/
10+
public static void main(String[] args) {
11+
Random rand = new Random();
12+
13+
/* test 100 times using rand numbers */
14+
for (int i = 1; i <= 100; ++i) {
15+
/* generate number from -50 to 49 */
16+
int a = rand.nextInt(100) - 50;
17+
int b = rand.nextInt(100) - 50;
18+
assert max(a, b) == Math.max(a, b);
19+
}
20+
}
21+
522
/**
623
* Returns the greater of two {@code int} values. That is, the
724
* result is the argument closer to the value of
@@ -15,15 +32,4 @@ public class MaxValue {
1532
public static int max(int a, int b) {
1633
return a >= b ? a : b;
1734
}
18-
19-
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-
25-
int a = 3;
26-
int b = 4;
27-
System.out.format("max:%d between %d and %d", max(a, b), a, b);
28-
}
2935
}

Maths/MinValue.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
package Maths;
22

3+
import java.util.Random;
4+
35
public class MinValue {
46

7+
/**
8+
* Driver Code
9+
*/
10+
public static void main(String[] args) {
11+
Random rand = new Random();
12+
13+
/* test 100 times using rand numbers */
14+
for (int i = 1; i <= 100; ++i) {
15+
/* generate number from -50 to 49 */
16+
int a = rand.nextInt(100) - 50;
17+
int b = rand.nextInt(100) - 50;
18+
assert min(a, b) == Math.min(a, b);
19+
}
20+
}
21+
522
/**
623
* Returns the smaller of two {@code int} values. That is,
724
* the result the argument closer to the value of
@@ -15,15 +32,4 @@ public class MinValue {
1532
public static int min(int a, int b) {
1633
return a <= b ? a : b;
1734
}
18-
19-
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-
25-
int a = 3;
26-
int b = 4;
27-
System.out.format("min:%d between %d and %d", min(a, b), a, b);
28-
}
2935
}

0 commit comments

Comments
 (0)