Merge pull request #1430 from shellhub/master

Tested using rand numbers
This commit is contained in:
Du Yuanchao 2020-08-23 12:48:39 +08:00 committed by GitHub
commit 01e2c10605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 67 deletions

View File

@ -1,14 +1,17 @@
package Maths;
import java.util.Random;
public class AbsoluteValue {
public static void main(String[] args) {
assert absVal(-13) == 13;
assert absVal(0) == 0;
assert absVal(100) == 100;
int value = -34;
System.out.println("The absolute value of " + value + " is " + absVal(value));
Random random = new Random();
/* test 1000 random numbers */
for (int i = 1; i <= 1000; ++i) {
int randomNumber = random.nextInt();
assert absVal(randomNumber) == Math.abs(randomNumber);
}
}
/**

View File

@ -1,17 +1,14 @@
package Maths;
import java.util.Random;
public class Ceil {
public static void main(String[] args) {
assert ceil(10) == Math.ceil(10);
assert ceil(-10) == Math.ceil(-10);
assert ceil(10.0) == Math.ceil(10.0);
assert ceil(-10.0) == Math.ceil(-10.0);
assert ceil(10.1) == Math.ceil(10.1);
assert ceil(-10.1) == Math.ceil(-10.1);
assert ceil(0) == Math.ceil(0);
assert ceil(-0) == Math.ceil(-0);
assert ceil(0.0) == Math.ceil(0.0);
assert ceil(-0.0) == Math.ceil(-0.0);
Random random = new Random();
for (int i = 1; i <= 1000; ++i) {
double randomNumber = random.nextDouble();
assert ceil(randomNumber) == Math.ceil(randomNumber);
}
}
/**

View File

@ -1,18 +1,33 @@
package Maths;
import java.util.Arrays;
import java.util.Random;
public class FindMax {
//Driver
/**
* Driver Code
*/
public static void main(String[] args) {
int[] array = {2, 4, 9, 7, 19, 94, 5};
assert findMax(array) == 94;
Random random = new Random();
/* random size */
int size = random.nextInt(100) + 1;
int[] array = new int[size];
/* init array with random numbers */
for (int i = 0; i < size; i++) {
array[i] = random.nextInt() % 100;
}
assert Arrays.stream(array).max().getAsInt() == findMax(array);
}
/**
* find max of array
*
* @param array the array contains element
* @return max value
* @return max value of given array
*/
public static int findMax(int[] array) {
int max = array[0];

View File

@ -1,13 +1,23 @@
package Maths;
import java.util.Arrays;
import java.util.Random;
public class FindMaxRecursion {
public static void main(String[] args) {
int[] array = {2, 4, 9, 7, 19, 94, 5};
int low = 0;
int high = array.length - 1;
Random rand = new Random();
assert max(array, low, high) == 94;
assert max(array, array.length) == 94;
/* rand size */
int size = rand.nextInt(100) + 1;
int[] array = new int[size];
/* init array with rand numbers */
for (int i = 0; i < size; i++) {
array[i] = rand.nextInt() % 100;
}
assert max(array, array.length) == Arrays.stream(array).max().getAsInt();
assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt();
}
/**

View File

@ -1,11 +1,26 @@
package Maths;
import java.util.Arrays;
import java.util.Random;
public class FindMin {
//Driver
/**
* Driver Code
*/
public static void main(String[] args) {
int[] array = {2, 4, 9, 7, 19, 94, 5};
assert findMin(array) == 2;
Random random = new Random();
/* random size */
int size = random.nextInt(100) + 1;
int[] array = new int[size];
/* init array with random numbers */
for (int i = 0; i < size; i++) {
array[i] = random.nextInt() % 100;
}
assert Arrays.stream(array).min().getAsInt() == findMin(array);
}
/**

View File

@ -1,13 +1,27 @@
package Maths;
public class FindMinRecursion {
public static void main(String[] args) {
int[] array = {2, 4, 9, -7, 19, 94, 5};
int low = 0;
int high = array.length - 1;
import java.util.Arrays;
import java.util.Random;
assert min(array, low, high) == -7;
assert min(array, array.length) == -7;
public class FindMinRecursion {
/**
* Driver Code
*/
public static void main(String[] args) {
Random rand = new Random();
/* rand size */
int size = rand.nextInt(100) + 1;
int[] array = new int[size];
/* init array with rand numbers */
for (int i = 0; i < size; i++) {
array[i] = rand.nextInt() % 100;
}
assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
assert min(array, array.length) == Arrays.stream(array).min().getAsInt();
}
/**

View File

@ -1,17 +1,14 @@
package Maths;
import java.util.Random;
public class Floor {
public static void main(String[] args) {
assert floor(10) == Math.floor(10);
assert floor(-10) == Math.floor(-10);
assert floor(10.0) == Math.floor(10.0);
assert floor(-10.0) == Math.floor(-10.0);
assert floor(10.1) == Math.floor(10.1);
assert floor(-10.1) == Math.floor(-10.1);
assert floor(0) == Math.floor(0);
assert floor(-0) == Math.floor(-0);
assert floor(0.0) == Math.floor(0.0);
assert floor(-0.0) == Math.floor(-0.0);
Random random = new Random();
for (int i = 1; i <= 1000; ++i) {
double randomNumber = random.nextDouble();
assert floor(randomNumber) == Math.floor(randomNumber);
}
}
/**

View File

@ -1,7 +1,24 @@
package Maths;
import java.util.Random;
public class MaxValue {
/**
* Driver Code
*/
public static void main(String[] args) {
Random rand = new Random();
/* test 100 times using rand numbers */
for (int i = 1; i <= 100; ++i) {
/* generate number from -50 to 49 */
int a = rand.nextInt(100) - 50;
int b = rand.nextInt(100) - 50;
assert max(a, b) == Math.max(a, b);
}
}
/**
* Returns the greater of two {@code int} values. That is, the
* result is the argument closer to the value of
@ -15,15 +32,4 @@ public class MaxValue {
public static int max(int a, int b) {
return a >= b ? a : b;
}
public static void main(String[] args) {
assert max(-3,3) == 3;
assert max(-6,-20) == -6;
assert max(100,32) == 100;
assert max(13,13) == 13;
int a = 3;
int b = 4;
System.out.format("max:%d between %d and %d", max(a, b), a, b);
}
}

View File

@ -1,7 +1,24 @@
package Maths;
import java.util.Random;
public class MinValue {
/**
* Driver Code
*/
public static void main(String[] args) {
Random rand = new Random();
/* test 100 times using rand numbers */
for (int i = 1; i <= 100; ++i) {
/* generate number from -50 to 49 */
int a = rand.nextInt(100) - 50;
int b = rand.nextInt(100) - 50;
assert min(a, b) == Math.min(a, b);
}
}
/**
* Returns the smaller of two {@code int} values. That is,
* the result the argument closer to the value of
@ -15,15 +32,4 @@ public class MinValue {
public static int min(int a, int b) {
return a <= b ? a : b;
}
public static void main(String[] args) {
assert min(-3,3) == -3;
assert min(-6,-20) == -20;
assert min(100,32) == 32;
assert min(13,13) == 13;
int a = 3;
int b = 4;
System.out.format("min:%d between %d and %d", min(a, b), a, b);
}
}