public void launch() { // this script attempts to use polymorphism to get the // maximum of 2 different numbers provided as a double, string or integer // the class assumes input of a double so that we don't get any loss of precision // the alternative was to use an integer and cast the double to an integer but wouldn't have // been able to compare non whole numbers in that case Comparator compare = new Comparator(); System.out.println("Max =" + compare.getMax(4.26, 4.33)); // compare doubles System.out.println("Max =" + compare.getMax(4, 3)); // compare integers System.out.println("Max =" + compare.getMax("4.89", "4.88")); // compare strings }
@Test public void testGetMaxString() { String test1 = "1"; String test2 = "2"; String output = test.getMax(test1, test2); assertEquals("2", output); }
@Test public void testGetMaxInt() { int test1 = 1; int test2 = 2; int output = test.getMax(test1, test2); assertEquals(2, output); }
@Test public void testGetMaxDouble() { double test1 = 1; double test2 = 2; double output = test.getMax(test1, test2); assertEquals(2.0, output, 1); }
public static void main(String[] args) { Comparator<String> c = new Comparator<String>(); String a = "22"; String b = "23"; System.out.println(c.getMax(a, b)); }