/************************************************** * Author: Morrison * Date created: 3 Sep 2020 * Date last modified: 3 Sep 2020 **************************************************/ import java.util.ArrayList; public class TestConditional { //static service class private TestConditional() { } //absolute value function public static int abs(int x) { return x >= 0? x: -x; } //returns 0 if x is 0, 1 if x > 0, -1 if x < 0 public static int sgn(int x) { if(x > 0) { return 1; } if(x < 0) { return -1; } return 0; } //returns true if item resides at an even index in list. //this is a generic function. They can be very cool. public static boolean isAtEvenIndex(ArrayList list, T item) { for(int k = 0; k < item.size(); k += 2) { if(list.get(k).equals(item)) { return true; } } return false; } public static void main(String[] args) { System.out.println("Testing abs********"); int x = 5; int expected = 5; System.out.printf("Case %s: %s\n", x, abs(x) == expected); x = -5; expected = 5; System.out.printf("Case %s: %s\n", x, abs(x) == expected); x = 0; expected = 0; System.out.printf("Case %s: %s\n", x, abs(x) == expected); System.out.println("Testing sgn********"); x = 5; expected = 1; System.out.printf("Case %s: %s\n", x, sgn(x) == expected); x = -5; expected = -1; System.out.printf("Case %s: %s\n", x, sgn(x) == expected); x = 0; expected = 0; System.out.printf("Case %s: %s\n", x, sgn(x) == expected); ArrayList foo = new ArrayList<>(); foo.add("cat"); foo.add("dog"); foo.add("elephant"); //String s = "cat"; //System.out.printf("Case %s: %s\n", s, isAtEvenIndex(foo, s)); //String s = "dog"; //System.out.printf("Case %s: %s\n", s, !isAtEvenIndex(foo, s)); //String s = "heffalump"; //System.out.printf("Case %s: %s\n", s, !isAtEvenIndex(foo, s)); } }