Added a magnitiude addition helper function.

This commit is contained in:
Harrison Deng 2020-04-25 17:49:13 -05:00
parent 629660c8fc
commit 1e0d63e562
2 changed files with 16 additions and 0 deletions

View File

@ -18,4 +18,11 @@ public class Utilities {
} }
return res; return res;
} }
public static int addMagnitude(int value, int add) {
boolean negative = false;
if (value < 0) negative = true;
int res = Math.abs(value) + add;
return negative ? - res : res;
}
} }

View File

@ -39,4 +39,13 @@ public class UtilitiesTest {
assertEquals(first, expectedFirst); assertEquals(first, expectedFirst);
assertEquals(second, expectedSecond); assertEquals(second, expectedSecond);
} }
@Test
public void testMagnitudeAddPositive() {
assertEquals(2, Utilities.addMagnitude(1, 1));
}
@Test
public void testMagnitudeAddNegative() {
assertEquals(-2, Utilities.addMagnitude(-1, 1));
}
} }