mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
26
src/main/java/com/thealgorithms/maths/AbsoluteValue.java
Normal file
26
src/main/java/com/thealgorithms/maths/AbsoluteValue.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class AbsoluteValue {
|
||||
|
||||
public static void main(String[] args) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If value is less than zero, make value positive.
|
||||
*
|
||||
* @param value a number
|
||||
* @return the absolute value of a number
|
||||
*/
|
||||
public static int absVal(int value) {
|
||||
return value < 0 ? -value : value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user