File tree Expand file tree Collapse file tree 2 files changed +23
-17
lines changed
main/java/com/thealgorithms/maths
test/java/com/thealgorithms/maths Expand file tree Collapse file tree 2 files changed +23
-17
lines changed Original file line number Diff line number Diff line change 11package com .thealgorithms .maths ;
22
3- import java .util .Random ;
4-
53public class AbsoluteValue {
64
7- public static void main (String [] args ) {
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- }
15- }
16-
175 /**
18- * If value is less than zero, make value positive .
6+ * Returns the absolute value of a number .
197 *
20- * @param value a number
21- * @return the absolute value of a number
8+ * @param number The number to be transformed
9+ * @return The absolute value of the {@code number}
2210 */
23- public static int absVal (int value ) {
24- return value < 0 ? -value : value ;
11+ public static int getAbsValue (int number ) {
12+ return number < 0 ? -number : number ;
2513 }
2614}
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .maths ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .concurrent .ThreadLocalRandom ;
6+ import java .util .stream .Stream ;
7+
8+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9+
10+ public class AbsoluteValueTest {
11+
12+ @ Test
13+ void testGetAbsValue () {
14+ Stream .generate (() -> ThreadLocalRandom .current ().nextInt ())
15+ .limit (1000 )
16+ .forEach (number -> assertEquals (Math .abs (number ), AbsoluteValue .getAbsValue (number )));
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments