JavaAlgorithms/Maths/AbsoluteValue.java

25 lines
555 B
Java
Raw Normal View History

package Maths;
2019-08-15 09:12:33 +08:00
public class AbsoluteValue {
public static void main(String[] args) {
2020-08-16 22:46:39 +08:00
assert absVal(-13) == 13;
assert absVal(0) == 0;
assert absVal(100) == 100;
int value = -34;
2019-08-15 09:12:33 +08:00
System.out.println("The absolute value of " + value + " is " + absVal(value));
}
2019-08-15 09:12:33 +08:00
/**
* 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) {
2019-09-28 10:19:39 +08:00
return value < 0 ? -value : value;
}
2019-08-15 09:12:33 +08:00
}