diff --git a/Array.java b/Lab1/Array.java similarity index 100% rename from Array.java rename to Lab1/Array.java diff --git a/ArrayTest.java b/Lab1/ArrayTest.java similarity index 100% rename from ArrayTest.java rename to Lab1/ArrayTest.java diff --git a/ArrivalEvent.java b/Lab1/ArrivalEvent.java similarity index 100% rename from ArrivalEvent.java rename to Lab1/ArrivalEvent.java diff --git a/CS2030STest.java b/Lab1/CS2030STest.java similarity index 100% rename from CS2030STest.java rename to Lab1/CS2030STest.java diff --git a/Customer.java b/Lab1/Customer.java similarity index 100% rename from Customer.java rename to Lab1/Customer.java diff --git a/DepartureEvent.java b/Lab1/DepartureEvent.java similarity index 100% rename from DepartureEvent.java rename to Lab1/DepartureEvent.java diff --git a/Event.java b/Lab1/Event.java similarity index 100% rename from Event.java rename to Lab1/Event.java diff --git a/JoinCounterQueueEvent.java b/Lab1/JoinCounterQueueEvent.java similarity index 100% rename from JoinCounterQueueEvent.java rename to Lab1/JoinCounterQueueEvent.java diff --git a/JoinShopQueueEvent.java b/Lab1/JoinShopQueueEvent.java similarity index 100% rename from JoinShopQueueEvent.java rename to Lab1/JoinShopQueueEvent.java diff --git a/Lab1.java b/Lab1/Lab1.java similarity index 100% rename from Lab1.java rename to Lab1/Lab1.java diff --git a/Lab2.java b/Lab1/Lab2.java similarity index 100% rename from Lab2.java rename to Lab1/Lab2.java diff --git a/Lab3.java b/Lab1/Lab3.java similarity index 100% rename from Lab3.java rename to Lab1/Lab3.java diff --git a/Makefile b/Lab1/Makefile similarity index 100% rename from Makefile rename to Lab1/Makefile diff --git a/Queue.java b/Lab1/Queue.java similarity index 100% rename from Queue.java rename to Lab1/Queue.java diff --git a/QueueTest.java b/Lab1/QueueTest.java similarity index 100% rename from QueueTest.java rename to Lab1/QueueTest.java diff --git a/ServiceBeginEvent.java b/Lab1/ServiceBeginEvent.java similarity index 100% rename from ServiceBeginEvent.java rename to Lab1/ServiceBeginEvent.java diff --git a/ServiceCounter.java b/Lab1/ServiceCounter.java similarity index 100% rename from ServiceCounter.java rename to Lab1/ServiceCounter.java diff --git a/ServiceEndEvent.java b/Lab1/ServiceEndEvent.java similarity index 100% rename from ServiceEndEvent.java rename to Lab1/ServiceEndEvent.java diff --git a/Shop.java b/Lab1/Shop.java similarity index 100% rename from Shop.java rename to Lab1/Shop.java diff --git a/ShopSimulation.java b/Lab1/ShopSimulation.java similarity index 100% rename from ShopSimulation.java rename to Lab1/ShopSimulation.java diff --git a/Simulation.java b/Lab1/Simulation.java similarity index 100% rename from Simulation.java rename to Lab1/Simulation.java diff --git a/Simulator.java b/Lab1/Simulator.java similarity index 100% rename from Simulator.java rename to Lab1/Simulator.java diff --git a/inputs/Lab1.1.in b/Lab1/inputs/Lab1.1.in similarity index 100% rename from inputs/Lab1.1.in rename to Lab1/inputs/Lab1.1.in diff --git a/inputs/Lab1.2.in b/Lab1/inputs/Lab1.2.in similarity index 100% rename from inputs/Lab1.2.in rename to Lab1/inputs/Lab1.2.in diff --git a/inputs/Lab1.3.in b/Lab1/inputs/Lab1.3.in similarity index 100% rename from inputs/Lab1.3.in rename to Lab1/inputs/Lab1.3.in diff --git a/inputs/Lab1.4.in b/Lab1/inputs/Lab1.4.in similarity index 100% rename from inputs/Lab1.4.in rename to Lab1/inputs/Lab1.4.in diff --git a/inputs/Lab1.5.in b/Lab1/inputs/Lab1.5.in similarity index 100% rename from inputs/Lab1.5.in rename to Lab1/inputs/Lab1.5.in diff --git a/inputs/Lab2.1.in b/Lab1/inputs/Lab2.1.in similarity index 100% rename from inputs/Lab2.1.in rename to Lab1/inputs/Lab2.1.in diff --git a/inputs/Lab2.10.in b/Lab1/inputs/Lab2.10.in similarity index 100% rename from inputs/Lab2.10.in rename to Lab1/inputs/Lab2.10.in diff --git a/inputs/Lab2.2.in b/Lab1/inputs/Lab2.2.in similarity index 100% rename from inputs/Lab2.2.in rename to Lab1/inputs/Lab2.2.in diff --git a/inputs/Lab2.3.in b/Lab1/inputs/Lab2.3.in similarity index 100% rename from inputs/Lab2.3.in rename to Lab1/inputs/Lab2.3.in diff --git a/inputs/Lab2.4.in b/Lab1/inputs/Lab2.4.in similarity index 100% rename from inputs/Lab2.4.in rename to Lab1/inputs/Lab2.4.in diff --git a/inputs/Lab2.5.in b/Lab1/inputs/Lab2.5.in similarity index 100% rename from inputs/Lab2.5.in rename to Lab1/inputs/Lab2.5.in diff --git a/inputs/Lab2.6.in b/Lab1/inputs/Lab2.6.in similarity index 100% rename from inputs/Lab2.6.in rename to Lab1/inputs/Lab2.6.in diff --git a/inputs/Lab2.7.in b/Lab1/inputs/Lab2.7.in similarity index 100% rename from inputs/Lab2.7.in rename to Lab1/inputs/Lab2.7.in diff --git a/inputs/Lab2.8.in b/Lab1/inputs/Lab2.8.in similarity index 100% rename from inputs/Lab2.8.in rename to Lab1/inputs/Lab2.8.in diff --git a/inputs/Lab2.9.in b/Lab1/inputs/Lab2.9.in similarity index 100% rename from inputs/Lab2.9.in rename to Lab1/inputs/Lab2.9.in diff --git a/inputs/Lab3.1.in b/Lab1/inputs/Lab3.1.in similarity index 100% rename from inputs/Lab3.1.in rename to Lab1/inputs/Lab3.1.in diff --git a/inputs/Lab3.10.in b/Lab1/inputs/Lab3.10.in similarity index 100% rename from inputs/Lab3.10.in rename to Lab1/inputs/Lab3.10.in diff --git a/inputs/Lab3.11.in b/Lab1/inputs/Lab3.11.in similarity index 100% rename from inputs/Lab3.11.in rename to Lab1/inputs/Lab3.11.in diff --git a/inputs/Lab3.12.in b/Lab1/inputs/Lab3.12.in similarity index 100% rename from inputs/Lab3.12.in rename to Lab1/inputs/Lab3.12.in diff --git a/inputs/Lab3.13.in b/Lab1/inputs/Lab3.13.in similarity index 100% rename from inputs/Lab3.13.in rename to Lab1/inputs/Lab3.13.in diff --git a/inputs/Lab3.14.in b/Lab1/inputs/Lab3.14.in similarity index 100% rename from inputs/Lab3.14.in rename to Lab1/inputs/Lab3.14.in diff --git a/inputs/Lab3.15.in b/Lab1/inputs/Lab3.15.in similarity index 100% rename from inputs/Lab3.15.in rename to Lab1/inputs/Lab3.15.in diff --git a/inputs/Lab3.2.in b/Lab1/inputs/Lab3.2.in similarity index 100% rename from inputs/Lab3.2.in rename to Lab1/inputs/Lab3.2.in diff --git a/inputs/Lab3.3.in b/Lab1/inputs/Lab3.3.in similarity index 100% rename from inputs/Lab3.3.in rename to Lab1/inputs/Lab3.3.in diff --git a/inputs/Lab3.4.in b/Lab1/inputs/Lab3.4.in similarity index 100% rename from inputs/Lab3.4.in rename to Lab1/inputs/Lab3.4.in diff --git a/inputs/Lab3.5.in b/Lab1/inputs/Lab3.5.in similarity index 100% rename from inputs/Lab3.5.in rename to Lab1/inputs/Lab3.5.in diff --git a/inputs/Lab3.6.in b/Lab1/inputs/Lab3.6.in similarity index 100% rename from inputs/Lab3.6.in rename to Lab1/inputs/Lab3.6.in diff --git a/inputs/Lab3.7.in b/Lab1/inputs/Lab3.7.in similarity index 100% rename from inputs/Lab3.7.in rename to Lab1/inputs/Lab3.7.in diff --git a/inputs/Lab3.8.in b/Lab1/inputs/Lab3.8.in similarity index 100% rename from inputs/Lab3.8.in rename to Lab1/inputs/Lab3.8.in diff --git a/inputs/Lab3.9.in b/Lab1/inputs/Lab3.9.in similarity index 100% rename from inputs/Lab3.9.in rename to Lab1/inputs/Lab3.9.in diff --git a/outputs/ArrayTest.out b/Lab1/outputs/ArrayTest.out similarity index 100% rename from outputs/ArrayTest.out rename to Lab1/outputs/ArrayTest.out diff --git a/outputs/Lab1.1.out b/Lab1/outputs/Lab1.1.out similarity index 100% rename from outputs/Lab1.1.out rename to Lab1/outputs/Lab1.1.out diff --git a/outputs/Lab1.2.out b/Lab1/outputs/Lab1.2.out similarity index 100% rename from outputs/Lab1.2.out rename to Lab1/outputs/Lab1.2.out diff --git a/outputs/Lab1.3.out b/Lab1/outputs/Lab1.3.out similarity index 100% rename from outputs/Lab1.3.out rename to Lab1/outputs/Lab1.3.out diff --git a/outputs/Lab1.4.out b/Lab1/outputs/Lab1.4.out similarity index 100% rename from outputs/Lab1.4.out rename to Lab1/outputs/Lab1.4.out diff --git a/outputs/Lab1.5.out b/Lab1/outputs/Lab1.5.out similarity index 100% rename from outputs/Lab1.5.out rename to Lab1/outputs/Lab1.5.out diff --git a/outputs/Lab2.1.out b/Lab1/outputs/Lab2.1.out similarity index 100% rename from outputs/Lab2.1.out rename to Lab1/outputs/Lab2.1.out diff --git a/outputs/Lab2.10.out b/Lab1/outputs/Lab2.10.out similarity index 100% rename from outputs/Lab2.10.out rename to Lab1/outputs/Lab2.10.out diff --git a/outputs/Lab2.2.out b/Lab1/outputs/Lab2.2.out similarity index 100% rename from outputs/Lab2.2.out rename to Lab1/outputs/Lab2.2.out diff --git a/outputs/Lab2.3.out b/Lab1/outputs/Lab2.3.out similarity index 100% rename from outputs/Lab2.3.out rename to Lab1/outputs/Lab2.3.out diff --git a/outputs/Lab2.4.out b/Lab1/outputs/Lab2.4.out similarity index 100% rename from outputs/Lab2.4.out rename to Lab1/outputs/Lab2.4.out diff --git a/outputs/Lab2.5.out b/Lab1/outputs/Lab2.5.out similarity index 100% rename from outputs/Lab2.5.out rename to Lab1/outputs/Lab2.5.out diff --git a/outputs/Lab2.6.out b/Lab1/outputs/Lab2.6.out similarity index 100% rename from outputs/Lab2.6.out rename to Lab1/outputs/Lab2.6.out diff --git a/outputs/Lab2.7.out b/Lab1/outputs/Lab2.7.out similarity index 100% rename from outputs/Lab2.7.out rename to Lab1/outputs/Lab2.7.out diff --git a/outputs/Lab2.8.out b/Lab1/outputs/Lab2.8.out similarity index 100% rename from outputs/Lab2.8.out rename to Lab1/outputs/Lab2.8.out diff --git a/outputs/Lab2.9.out b/Lab1/outputs/Lab2.9.out similarity index 100% rename from outputs/Lab2.9.out rename to Lab1/outputs/Lab2.9.out diff --git a/outputs/Lab3.1.out b/Lab1/outputs/Lab3.1.out similarity index 100% rename from outputs/Lab3.1.out rename to Lab1/outputs/Lab3.1.out diff --git a/outputs/Lab3.10.out b/Lab1/outputs/Lab3.10.out similarity index 100% rename from outputs/Lab3.10.out rename to Lab1/outputs/Lab3.10.out diff --git a/outputs/Lab3.11.out b/Lab1/outputs/Lab3.11.out similarity index 100% rename from outputs/Lab3.11.out rename to Lab1/outputs/Lab3.11.out diff --git a/outputs/Lab3.12.out b/Lab1/outputs/Lab3.12.out similarity index 100% rename from outputs/Lab3.12.out rename to Lab1/outputs/Lab3.12.out diff --git a/outputs/Lab3.13.out b/Lab1/outputs/Lab3.13.out similarity index 100% rename from outputs/Lab3.13.out rename to Lab1/outputs/Lab3.13.out diff --git a/outputs/Lab3.14.out b/Lab1/outputs/Lab3.14.out similarity index 100% rename from outputs/Lab3.14.out rename to Lab1/outputs/Lab3.14.out diff --git a/outputs/Lab3.15.out b/Lab1/outputs/Lab3.15.out similarity index 100% rename from outputs/Lab3.15.out rename to Lab1/outputs/Lab3.15.out diff --git a/outputs/Lab3.2.out b/Lab1/outputs/Lab3.2.out similarity index 100% rename from outputs/Lab3.2.out rename to Lab1/outputs/Lab3.2.out diff --git a/outputs/Lab3.3.out b/Lab1/outputs/Lab3.3.out similarity index 100% rename from outputs/Lab3.3.out rename to Lab1/outputs/Lab3.3.out diff --git a/outputs/Lab3.4.out b/Lab1/outputs/Lab3.4.out similarity index 100% rename from outputs/Lab3.4.out rename to Lab1/outputs/Lab3.4.out diff --git a/outputs/Lab3.5.out b/Lab1/outputs/Lab3.5.out similarity index 100% rename from outputs/Lab3.5.out rename to Lab1/outputs/Lab3.5.out diff --git a/outputs/Lab3.6.out b/Lab1/outputs/Lab3.6.out similarity index 100% rename from outputs/Lab3.6.out rename to Lab1/outputs/Lab3.6.out diff --git a/outputs/Lab3.7.out b/Lab1/outputs/Lab3.7.out similarity index 100% rename from outputs/Lab3.7.out rename to Lab1/outputs/Lab3.7.out diff --git a/outputs/Lab3.8.out b/Lab1/outputs/Lab3.8.out similarity index 100% rename from outputs/Lab3.8.out rename to Lab1/outputs/Lab3.8.out diff --git a/outputs/Lab3.9.out b/Lab1/outputs/Lab3.9.out similarity index 100% rename from outputs/Lab3.9.out rename to Lab1/outputs/Lab3.9.out diff --git a/outputs/QueueTest.out b/Lab1/outputs/QueueTest.out similarity index 100% rename from outputs/QueueTest.out rename to Lab1/outputs/QueueTest.out diff --git a/test.sh b/Lab1/test.sh similarity index 87% rename from test.sh rename to Lab1/test.sh index f0aa4ff..6c8c075 100755 --- a/test.sh +++ b/Lab1/test.sh @@ -34,7 +34,7 @@ do else out=$(mktemp --suffix=$PROG) fi - java $PROG < inputs/$PROG.$i.in | head -c 50MB > $out + java $PROG < inputs/$PROG.$i.in > $out status="$?" if [ "$status" -ne "0" ] then @@ -72,5 +72,6 @@ then echo "$PROG: passed everything 🎉" fi # Run style checker -java -jar ~cs2030s/bin/checkstyle.jar -c ~cs2030s/bin/cs2030_checks.xml *.java +#java -jar ~cs2030s/bin/checkstyle.jar -c ~cs2030s/bin/cs2030_checks.xml *.java +java -jar ./checkstyle.jar -c ./cs2030_checks.xml *.java # vim:noexpandtab:sw=4:ts=4 diff --git a/Lab4/Action.java b/Lab4/Action.java new file mode 100644 index 0000000..9583f32 --- /dev/null +++ b/Lab4/Action.java @@ -0,0 +1,11 @@ +/** + * The Action interface that can be called + * on an object of type T to act. + * + * Contains a single abstract method call. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Actionable.java b/Lab4/Actionable.java new file mode 100644 index 0000000..a9e8046 --- /dev/null +++ b/Lab4/Actionable.java @@ -0,0 +1,11 @@ +/** + * The Actionable interface that can + * act when given an action. + * + * Contains a single abstract method act. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Applicable.java b/Lab4/Applicable.java new file mode 100644 index 0000000..36567f1 --- /dev/null +++ b/Lab4/Applicable.java @@ -0,0 +1,12 @@ +/** + * The Applicable interface that can probably + * transform if given something that is + * probably an Immutator. + * + * Contains a single abstract method apply. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/CS2030STest.java b/Lab4/CS2030STest.java new file mode 100644 index 0000000..b1d12ea --- /dev/null +++ b/Lab4/CS2030STest.java @@ -0,0 +1,75 @@ +import java.net.URI; +import java.util.List; +import javax.tools.DiagnosticCollector; +import javax.tools.SimpleJavaFileObject; +import javax.tools.ToolProvider; +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +class CS2030STest { + + private static final String ANSI_RESET = "\u001B[0m"; + private static final String ANSI_RED = "\u001B[31m"; + private static final String ANSI_GREEN = "\u001B[32m"; + + public void expect(String test, Object output, Object expect) { + System.out.print(test); + if ((expect == null && output == null) || output.equals(expect)) { + System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET); + } else { + System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET); + System.out.println(" expected: " + expect); + System.out.println(" got this: " + output); + } + } + + public static String clean(String txt) { + String res = ""; + for (int i=0; i(), null, null, + List.of(new JavaSourceFromString(statement))) + .call(); + + if (noError != success) { + System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET); + if (!success) { + System.out.println(" expected compilation error but it compiles fine."); + } else { + System.out.println(" expected the statement to compile without errors but it does not."); + } + } else { + System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET); + } + } +} diff --git a/Lab4/Immutator.java b/Lab4/Immutator.java new file mode 100644 index 0000000..e9f7439 --- /dev/null +++ b/Lab4/Immutator.java @@ -0,0 +1,11 @@ +/** + * The Immutator interface that can transform + * to type T2, an object of type T1. + * + * Contains a single abstract method invoke. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Immutatorable.java b/Lab4/Immutatorable.java new file mode 100644 index 0000000..84c17e0 --- /dev/null +++ b/Lab4/Immutatorable.java @@ -0,0 +1,12 @@ +/** + * The Immutatorable interface that can + * transform when given something that is + * Immutator. + * + * Contains a single abstract method transform. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Improbable.java b/Lab4/Improbable.java new file mode 100644 index 0000000..e1fbb21 --- /dev/null +++ b/Lab4/Improbable.java @@ -0,0 +1,9 @@ +/** + * A generic Immutator that takes in an object + * that is T and returns an object that is probably T. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/IsModEq.java b/Lab4/IsModEq.java new file mode 100644 index 0000000..0011860 --- /dev/null +++ b/Lab4/IsModEq.java @@ -0,0 +1,12 @@ +/** + * A non-generic Immutator with parameter + * div and mod that takes in an integer val + * and return the boolean value by checking + * if the given value is equal to mod when + * divided by div. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Lab4.java b/Lab4/Lab4.java new file mode 100644 index 0000000..e6cb03b --- /dev/null +++ b/Lab4/Lab4.java @@ -0,0 +1,247 @@ +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"); + } + } +} diff --git a/Lab4/Lab4.pdf b/Lab4/Lab4.pdf new file mode 100644 index 0000000..976400e Binary files /dev/null and b/Lab4/Lab4.pdf differ diff --git a/Lab4/Print.java b/Lab4/Print.java new file mode 100644 index 0000000..bbed090 --- /dev/null +++ b/Lab4/Print.java @@ -0,0 +1,9 @@ +/** + * A non-generic Action to print the String + * representation of the object. + * + * CS2030S Lab 4 + * AY22/23 Semester 1 + * + * @author Put Your Name (Lab Group) + */ diff --git a/Lab4/Probably.java b/Lab4/Probably.java new file mode 100644 index 0000000..c27887a --- /dev/null +++ b/Lab4/Probably.java @@ -0,0 +1,96 @@ +/** + * This class implements something that + * probably is just a value but may be nothing. + * We will never return null in this class, but + * we may return something that contains nothing + * where the nothing is a null. + * + * @author XXX + * @version CS2030S AY22/23 Semester 1 + */ +class Probably { + private final T value; + + private static final Probably NONE = new Probably<>(null); + + /** + * Private constructor, can only be invoked inside. + * This is called a factory method. We can only + * create this using the two public static method. + * + * @return The shared NOTHING. + */ + private Probably(T value) { + this.value = value; + } + + /** + * It is probably nothing, no value inside. + * + * @return The shared NOTHING. + */ + public static Probably none() { + @SuppressWarnings("unchecked") + Probably res = (Probably) NONE; + return res; + } + + /** + * It is probably just the given value. + * Unless the value is null, then nothing is + * given again. + * + * @param value Probably this is the value + * unless it is null then we say + * that there is no + * @return The given value or nothing but + * never null. + */ + public static Probably just(T value) { + if (value == null) { + return none(); + } + return (Probably) new Probably<>(value); + } + + /** + * Check for equality between something that + * is probably a value but maybe nothing. + * + * @param obj The other value to be compared. + * @return True if the two values are equal, + * false otherwise. + */ + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj instanceof Probably) { + Probably some = (Probably) obj; + if (this.value == some.value) { + return true; + } + if (this.value == null || some.value == null) { + return false; + } + return this.value.equals(some.value); + } + return false; + } + + /** + * String representation of something that + * is probably a value but maybe nothing. + * + * @return The string representation. + */ + @Override + public String toString() { + if (this.value == null) { + return "<>"; + } else { + return "<" + this.value.toString() + ">"; + } + } +} \ No newline at end of file diff --git a/Lab4/Test1.java b/Lab4/Test1.java new file mode 100644 index 0000000..5dd514b --- /dev/null +++ b/Lab4/Test1.java @@ -0,0 +1,35 @@ +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +class Test1 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + PrintStream old = System.out; + ByteArrayOutputStream baos; + PrintStream ps; + + baos = new ByteArrayOutputStream(); + ps = new PrintStream(baos); + System.setOut(ps); + + new Print().call(17); + we.expectPrint("new Print().call(17)", + "17", + baos, + old); + + baos = new ByteArrayOutputStream(); + ps = new PrintStream(baos); + System.setOut(ps); + + new Print().call("string"); + we.expectPrint("new Print().call(\"string\")", + "string", + baos, + old); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/Test2.java b/Lab4/Test2.java new file mode 100644 index 0000000..e1e22e7 --- /dev/null +++ b/Lab4/Test2.java @@ -0,0 +1,45 @@ +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +class Test2 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + PrintStream old = System.out; + ByteArrayOutputStream baos; + PrintStream ps; + + baos = new ByteArrayOutputStream(); + ps = new PrintStream(baos); + System.setOut(ps); + + Probably.just(4).act(new Print()); + we.expectPrint("Probably.just(4).act(new Print())", + "4", + baos, + old); + + baos = new ByteArrayOutputStream(); + ps = new PrintStream(baos); + System.setOut(ps); + + Probably.just("string").act(new Print()); + we.expectPrint("Probably.just(\"string\").act(new Print())", + "string", + baos, + old); + + baos = new ByteArrayOutputStream(); + ps = new PrintStream(baos); + System.setOut(ps); + + Probably.none().act(new Print()); + we.expectPrint("Probably.none().act(new Print())", + "", + baos, + old); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/Test3.java b/Lab4/Test3.java new file mode 100644 index 0000000..421fc63 --- /dev/null +++ b/Lab4/Test3.java @@ -0,0 +1,57 @@ +class Test3 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + class Incr implements Immutator { + public Integer invoke(Integer t1) { + return t1 + 1; + } + } + class Length implements Immutator { + public Integer invoke(String t1) { + return t1.length(); + } + } + + we.prefix("jshell> class Incr implements Immutator {"); + we.prefix(" ...> public Integer invoke(Integer t1) {"); + we.prefix(" ...> return t1 + 1;"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.prefix("jshell> class Length implements Immutator {"); + we.prefix(" ...> public Integer invoke(String t1) {"); + we.prefix(" ...> return t1.length();"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.expect("new Incr().invoke(4)", + new Incr().invoke(4).toString(), + "5"); + we.expect("new Incr().invoke(new Incr().invoke(4))", + new Incr().invoke(new Incr().invoke(4)).toString(), + "6"); + we.expect("new Length().invoke(\"string\")", + new Length().invoke("string").toString(), + "6"); + we.expect("new Incr().invoke(new Length().invoke(\"string\"))", + new Incr().invoke(new Length().invoke("string")).toString(), + "7"); + + we.expect("new Improbable<>().invoke(1)", + new Improbable().invoke(1).toString(), + "<1>"); + we.expect("new Improbable().invoke(null)", + new Improbable().invoke(null).toString(), + "<>"); + we.expect("new Improbable().invoke(1).transform(new Incr())", + new Improbable().invoke(1).transform(new Incr()).toString(), + "<2>"); + we.expect("new Improbable<>().invoke(new Improbable<>().invoke(1))", + new Improbable<>().invoke(new Improbable().invoke(1)).toString(), + "<<1>>"); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/Test4.java b/Lab4/Test4.java new file mode 100644 index 0000000..cde82dd --- /dev/null +++ b/Lab4/Test4.java @@ -0,0 +1,54 @@ +class Test4 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + class Incr implements Immutator { + public Integer invoke(Integer t1) { + return t1 + 1; + } + } + class Length implements Immutator { + public Integer invoke(String t1) { + return t1.length(); + } + } + + we.prefix("jshell> class Incr implements Immutator {"); + we.prefix(" ...> public Integer invoke(Integer t1) {"); + we.prefix(" ...> return t1 + 1;"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.prefix("jshell> class Length implements Immutator {"); + we.prefix(" ...> public Integer invoke(String t1) {"); + we.prefix(" ...> return t1.length();"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.expect("Probably.just(4).transform(new Incr())", + Probably.just(4).transform(new Incr()).toString(), + "<5>"); + we.expect("Probably.just(4).transform(new Incr()).transform(new Incr())", + Probably.just(4).transform(new Incr()).transform(new Incr()).toString(), + "<6>"); + we.expect("Probably.just(\"string\").transform(new Length())", + Probably.just("string").transform(new Length()).toString(), + "<6>"); + we.expect("Probably.just(\"string\").transform(new Length()).transform(new Incr())", + Probably.just("string").transform(new Length()).transform(new Incr()).toString(), + "<7>"); + + we.expect("Probably.none().transform(new Incr())", + Probably.none().transform(new Incr()).toString(), + "<>"); + we.expect("Probably.none().transform(new Length())", + Probably.none().transform(new Length()).toString(), + "<>"); + we.expect("Probably.just(null).transform(new Length()).transform(new Incr())", + Probably.just(null).transform(new Length()).transform(new Incr()).toString(), + "<>"); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/Test5.java b/Lab4/Test5.java new file mode 100644 index 0000000..244174f --- /dev/null +++ b/Lab4/Test5.java @@ -0,0 +1,48 @@ +class Test5 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + class Incr implements Immutator { + public Integer invoke(Integer t1) { + return t1 + 1; + } + } + class Length implements Immutator { + public Integer invoke(String t1) { + return t1.length(); + } + } + + we.prefix("jshell> class Incr implements Immutator {"); + we.prefix(" ...> public Integer invoke(Integer t1) {"); + we.prefix(" ...> return t1 + 1;"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.prefix("jshell> class Length implements Immutator {"); + we.prefix(" ...> public Integer invoke(String t1) {"); + we.prefix(" ...> return t1.length();"); + we.prefix(" ...> }"); + we.prefix(" ...> }"); + + we.expect("Probably.just(17).check(new IsModEq(3,2)) // 17 % 3 is equal to 2", + Probably.just(17).check(new IsModEq(3,2)).toString(), + "<17>"); + we.expect("Probably.just(18).check(new IsModEq(3,2)) // 18 % 3 is not equal to 2", + Probably.just(18).check(new IsModEq(3,2)).toString(), + "<>"); + + we.expect("Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)) // 17 % 3 is not equal to 2", + Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)).toString(), + "<17>"); + we.expect("Probably.just(\"string\").transform(new Length()).check(new IsModEq(3,2))", + Probably.just("string").transform(new Length()).transform(new Incr()).transform(new Incr()).check(new IsModEq(3,2)).toString(), + "<8>"); + we.expect("Probably.just(null).check(new IsModEq(0,2))", + Probably.just(null).check(new IsModEq(0,2)).toString(), + "<>"); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/Test6.java b/Lab4/Test6.java new file mode 100644 index 0000000..895cd6f --- /dev/null +++ b/Lab4/Test6.java @@ -0,0 +1,65 @@ +class Test6 { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + class Incr implements Immutator { + public Integer invoke(Integer t1) { + return t1 + 1; + } + } + class Length implements Immutator { + public Integer invoke(String t1) { + return t1.length(); + } + } + + Probably> justIncr = Probably.just(new Incr()); + Probably> justLength = Probably.just(new Length()); + Probably> noIncr = Probably.none(); + Probably> noLength = Probably.none(); + + we.prefix("jshell> class Incr implements Immutator {"); + we.prefix(" ...> public Integer invoke(Integer t1) {"); + we.prefix(" ...> return t1 + 1;"); + we.prefix(" ...> }"); + + we.prefix("jshell> class Length implements Immutator {"); + we.prefix(" ...> public Integer invoke(String t1) {"); + we.prefix(" ...> return t1.length();"); + we.prefix(" ...> }"); + + we.prefix("jshell> Probably> justIncr = Probably.just(new Incr());"); + we.prefix("jshell> Probably> justLength = Probably.just(new Length());"); + we.prefix("jshell> Probably> noIncr = Probably.none();"); + we.prefix("jshell> Probably> noLength = Probably.none();"); + + we.expect("Probably.just(17).apply(justIncr)", + Probably.just(17).apply(justIncr).toString(), + "<18>"); + we.expect("Probably.none().apply(justIncr)", + Probably.none().apply(justIncr).toString(), + "<>"); + we.expect("Probably.just(17).apply(noIncr)", + Probably.just(17).apply(noIncr).toString(), + "<>"); + we.expect("Probably.none().apply(noIncr)", + Probably.none().apply(noIncr).toString(), + "<>"); + + we.expect("Probably.just(\"string\").apply(justLength)", + Probably.just("string").apply(justLength).toString(), + "<6>"); + we.expect("Probably.none().apply(justLength)", + Probably.none().apply(justLength).toString(), + "<>"); + we.expect("Probably.just(\"string\").apply(noLength)", + Probably.just("string").apply(noLength).toString(), + "<>"); + we.expect("Probably.none().apply(noLength)", + Probably.none().apply(noLength).toString(), + "<>"); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/TestProbably.java b/Lab4/TestProbably.java new file mode 100644 index 0000000..a7481b5 --- /dev/null +++ b/Lab4/TestProbably.java @@ -0,0 +1,61 @@ +class TestProbably { + public static void main(String[] args) { + CS2030STest we = new CS2030STest(); + + we.expect("Probably.just(4)", + Probably.just(4).toString(), + "<4>"); + we.expect("Probably.just(Probably.just(0))", + Probably.just(Probably.just(0)).toString(), + "<<0>>"); + we.expect("Probably.just(Probably.just(Probably.just(\"null\")))", + Probably.just(Probably.just(Probably.just("null"))).toString(), + "<<>>"); + we.expect("Probably.just(Probably.just(Probably.none()))", + Probably.just(Probably.just(Probably.none())).toString(), + "<<<>>>"); + we.expect("Probably.just(Probably.just(null))", + Probably.just(Probably.just(null)).toString(), + "<<>>"); + + we.expect("Probably.just(4).equals(Probably.just(4))", + Probably.just(4).equals(Probably.just(4)), + true); + we.expect("Probably.just(4).equals(4)", + Probably.just(4).equals(4), + false); + we.expect("Probably.just(Probably.just(0)).equals(Probably.just(0))", + Probably.just(Probably.just(0)).equals(Probably.just(0)), + false); + we.expect("Probably.just(Probably.just(0)).equals(Probably.just(Probably.just(0)))", + Probably.just(Probably.just(0)).equals(Probably.just(Probably.just(0))), + true); + + we.expect("Probably.just(\"string\")", + Probably.just("string").toString(), + ""); + + we.expect("Probably.just(\"string\").equals(Probably.just(4))", + Probably.just("string").equals(Probably.just(4)), + false); + we.expect("Probably.just(\"string\").equals(Probably.just(\"null\"))", + Probably.just("string").equals(Probably.just("null")), + false); + + we.expect("Probably.just(null)", + Probably.just(null).toString(), + "<>"); + we.expect("Probably.none()", + Probably.none().toString(), + "<>"); + we.expect("Probably.none().equals(Probably.just(null))", + Probably.none().equals(Probably.just(null)), + true); + we.expect("Probably.none() == Probably.just(null)", + Probably.none() == Probably.just(null), + true); + + we.showStat(); + we.print(); + } +} \ No newline at end of file diff --git a/Lab4/inputs/Lab4.1.in b/Lab4/inputs/Lab4.1.in new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Lab4/inputs/Lab4.1.in @@ -0,0 +1 @@ +1 diff --git a/Lab4/inputs/Lab4.2.in b/Lab4/inputs/Lab4.2.in new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/Lab4/inputs/Lab4.2.in @@ -0,0 +1 @@ +2 diff --git a/Lab4/inputs/Lab4.3.in b/Lab4/inputs/Lab4.3.in new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/Lab4/inputs/Lab4.3.in @@ -0,0 +1 @@ +3 diff --git a/Lab4/inputs/Lab4.4.in b/Lab4/inputs/Lab4.4.in new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/Lab4/inputs/Lab4.4.in @@ -0,0 +1 @@ +4 diff --git a/Lab4/inputs/Lab4.5.in b/Lab4/inputs/Lab4.5.in new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/Lab4/inputs/Lab4.5.in @@ -0,0 +1 @@ +5 diff --git a/Lab4/inputs/Lab4.6.in b/Lab4/inputs/Lab4.6.in new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/Lab4/inputs/Lab4.6.in @@ -0,0 +1 @@ +6 diff --git a/Lab4/outputs/Lab4.1.out b/Lab4/outputs/Lab4.1.out new file mode 100644 index 0000000..a8f6cff --- /dev/null +++ b/Lab4/outputs/Lab4.1.out @@ -0,0 +1,2 @@ +17 +string diff --git a/Lab4/outputs/Lab4.2.out b/Lab4/outputs/Lab4.2.out new file mode 100644 index 0000000..b6f2c16 --- /dev/null +++ b/Lab4/outputs/Lab4.2.out @@ -0,0 +1,3 @@ +4 +string + diff --git a/Lab4/outputs/Lab4.3.out b/Lab4/outputs/Lab4.3.out new file mode 100644 index 0000000..d999cc6 --- /dev/null +++ b/Lab4/outputs/Lab4.3.out @@ -0,0 +1,8 @@ +5 +6 +6 +7 +<1> +<> +<2> +<<1>> diff --git a/Lab4/outputs/Lab4.4.out b/Lab4/outputs/Lab4.4.out new file mode 100644 index 0000000..b71df69 --- /dev/null +++ b/Lab4/outputs/Lab4.4.out @@ -0,0 +1,7 @@ +<5> +<6> +<6> +<7> +<> +<> +<> diff --git a/Lab4/outputs/Lab4.5.out b/Lab4/outputs/Lab4.5.out new file mode 100644 index 0000000..96d818e --- /dev/null +++ b/Lab4/outputs/Lab4.5.out @@ -0,0 +1,5 @@ +<17> +<> +<17> +<8> +<> diff --git a/Lab4/outputs/Lab4.6.out b/Lab4/outputs/Lab4.6.out new file mode 100644 index 0000000..7b7ea2e --- /dev/null +++ b/Lab4/outputs/Lab4.6.out @@ -0,0 +1,8 @@ +<18> +<> +<> +<> +<6> +<> +<> +<>