import java.util.Scanner; import java.io.PrintStream; import java.io.ByteArrayOutputStream; /** * The main class for CS2030S Lab 4. * * @author Wei Tsang * @version CS2030S AY21/22 Semester 2 */ class Lab4 { /** * Inner class for testing. */ static class Incr implements Immutator { public Integer invoke(Integer t1) { return t1 + 1; } } /** * Inner class for testing. */ static class Length implements Immutator { public Integer invoke(String t1) { return t1.length(); } } /** * Helper method to clean a string from * any newline. * * @param txt Input string. * @return The cleaned string. */ public static String clean(String txt) { String res = ""; for (int i=0; i().invoke(1).toString()); System.out.println(new Improbable().invoke(null).toString()); System.out.println(new Improbable().invoke(1).transform(new Incr()).toString()); System.out.println(new Improbable<>().invoke(new Improbable().invoke(1)).toString()); } catch(Exception e) { System.out.println("Error occurred"); } } /** * Test #4. */ public static void test4() { try { System.out.println(Probably.just(4).transform(new Incr()).toString()); System.out.println(Probably.just(4).transform(new Incr()).transform(new Incr()).toString()); System.out.println(Probably.just("string").transform(new Length()).toString()); System.out.println(Probably.just("string").transform(new Length()).transform(new Incr()).toString()); System.out.println(Probably.none().transform(new Incr()).toString()); System.out.println(Probably.none().transform(new Length()).toString()); System.out.println(Probably.just(null).transform(new Length()).transform(new Incr()).toString()); } catch(Exception e) { System.out.println("Error occurred"); } } /** * Test #5. */ public static void test5() { Probably> justIncr = Probably.just(new Incr()); Probably> justLength = Probably.just(new Length()); Probably> noIncr = Probably.none(); Probably> noLength = Probably.none(); try { System.out.println(Probably.just(17).check(new IsModEq(3,2)).toString()); System.out.println(Probably.just(18).check(new IsModEq(3,2)).toString()); System.out.println(Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)).toString()); System.out.println(Probably.just("string").transform(new Length()).transform(new Incr()).transform(new Incr()).check(new IsModEq(3,2)).toString()); System.out.println(Probably.just(null).check(new IsModEq(0,2)).toString()); } catch(Exception e) { System.out.println("Error occurred"); } } /** * Test #6. */ public static void test6() { Probably> justIncr = Probably.just(new Incr()); Probably> justLength = Probably.just(new Length()); Probably> noIncr = Probably.none(); Probably> noLength = Probably.none(); try { System.out.println(Probably.just(17).apply(justIncr).toString()); System.out.println(Probably.none().apply(justIncr).toString()); System.out.println(Probably.just(17).apply(noIncr).toString()); System.out.println(Probably.none().apply(noIncr).toString()); System.out.println(Probably.just("string").apply(justLength).toString()); System.out.println(Probably.none().apply(justLength).toString()); System.out.println(Probably.just("string").apply(noLength).toString()); System.out.println(Probably.none().apply(noLength).toString()); } catch(Exception e) { System.out.println("Error occurred"); } } }