feat: add Lab4

This commit is contained in:
Yadunand Prem 2022-09-15 11:21:56 +08:00
parent c4d857ed02
commit 46d4fa5e9d
116 changed files with 912 additions and 2 deletions

View File

@ -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

11
Lab4/Action.java Normal file
View File

@ -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)
*/

11
Lab4/Actionable.java Normal file
View File

@ -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)
*/

12
Lab4/Applicable.java Normal file
View File

@ -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)
*/

75
Lab4/CS2030STest.java Normal file
View File

@ -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<txt.length(); i++) {
if (txt.charAt(i) != '\r' && txt.charAt(i) != '\n') {
res += txt.charAt(i);
}
}
return res;
}
public void expectPrint(String test, Object expect, ByteArrayOutputStream baos, PrintStream old) {
System.out.flush();
System.setOut(old);
expect(test, CS2030STest.clean(baos.toString()), expect);
}
public void expectCompile(String test, String statement, boolean success) {
System.out.print(test);
class JavaSourceFromString extends SimpleJavaFileObject {
final String code;
JavaSourceFromString(String code) {
super(URI.create("string:///TempClass.java"), Kind.SOURCE);
this.code = "class TempClass {void foo(){" + code + ";}}";
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
boolean noError = ToolProvider
.getSystemJavaCompiler()
.getTask(null, null, new DiagnosticCollector<>(), 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);
}
}
}

11
Lab4/Immutator.java Normal file
View File

@ -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)
*/

12
Lab4/Immutatorable.java Normal file
View File

@ -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)
*/

9
Lab4/Improbable.java Normal file
View File

@ -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)
*/

12
Lab4/IsModEq.java Normal file
View File

@ -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)
*/

247
Lab4/Lab4.java Normal file
View File

@ -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<Integer,Integer> {
public Integer invoke(Integer t1) {
return t1 + 1;
}
}
/**
* Inner class for testing.
*/
static class Length implements Immutator<Integer,String> {
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<txt.length(); i++) {
if (txt.charAt(i) != '\r' && txt.charAt(i) != '\n') {
res += txt.charAt(i);
}
}
return res;
}
public static void main(String[] args) {
// Create a scanner to read from standard input.
Scanner sc = new Scanner(System.in);
// Read a single integer from the test file
// and then run the appropriate test case
switch (sc.nextInt()) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test3();
break;
case 4:
test4();
break;
case 5:
test5();
break;
case 6:
test6();
break;
}
// Clean up the scanner.
sc.close();
}
/**
* Test #1.
*/
public static void test1() {
PrintStream old = System.out;
ByteArrayOutputStream baos;
PrintStream ps;
try {
baos = new ByteArrayOutputStream();
ps = new PrintStream(baos);
System.setOut(ps);
new Print().call(17);
System.out.flush();
System.setOut(old);
System.out.println(Lab4.clean(baos.toString()));
} catch(Exception e) {
System.out.println("Error occurred");
}
try {
baos = new ByteArrayOutputStream();
ps = new PrintStream(baos);
System.setOut(ps);
new Print().call("string");
System.out.flush();
System.setOut(old);
System.out.println(Lab4.clean(baos.toString()));
} catch(Exception e) {
System.out.println("Error occurred");
}
}
/**
* Test #2.
*/
public static void test2() {
PrintStream old = System.out;
ByteArrayOutputStream baos;
PrintStream ps;
try {
baos = new ByteArrayOutputStream();
ps = new PrintStream(baos);
System.setOut(ps);
Probably.just(4).act(new Print());
System.out.flush();
System.setOut(old);
System.out.println(Lab4.clean(baos.toString()));
} catch(Exception e) {
System.out.println("Error occurred");
}
try {
baos = new ByteArrayOutputStream();
ps = new PrintStream(baos);
System.setOut(ps);
Probably.just("string").act(new Print());
System.out.flush();
System.setOut(old);
System.out.println(Lab4.clean(baos.toString()));
} catch(Exception e) {
System.out.println("Error occurred");
}
try {
baos = new ByteArrayOutputStream();
ps = new PrintStream(baos);
System.setOut(ps);
Probably.none().act(new Print());
System.out.flush();
System.setOut(old);
System.out.println(Lab4.clean(baos.toString()));
} catch(Exception e) {
System.out.println("Error occurred");
}
}
/**
* Test #3.
*/
public static void test3() {
try {
System.out.println(new Incr().invoke(4).toString());
System.out.println(new Incr().invoke(new Incr().invoke(4)).toString());
System.out.println(new Length().invoke("string").toString());
System.out.println(new Incr().invoke(new Length().invoke("string")).toString());
System.out.println(new Improbable<Integer>().invoke(1).toString());
System.out.println(new Improbable<String>().invoke(null).toString());
System.out.println(new Improbable<Integer>().invoke(1).transform(new Incr()).toString());
System.out.println(new Improbable<>().invoke(new Improbable<Integer>().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.<Integer>none().transform(new Incr()).toString());
System.out.println(Probably.<String>none().transform(new Length()).toString());
System.out.println(Probably.<String>just(null).transform(new Length()).transform(new Incr()).toString());
} catch(Exception e) {
System.out.println("Error occurred");
}
}
/**
* Test #5.
*/
public static void test5() {
Probably<Immutator<Integer,Integer>> justIncr = Probably.just(new Incr());
Probably<Immutator<Integer,String>> justLength = Probably.just(new Length());
Probably<Immutator<Integer,Integer>> noIncr = Probably.none();
Probably<Immutator<Integer,String>> 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.<Integer>just(null).check(new IsModEq(0,2)).toString());
} catch(Exception e) {
System.out.println("Error occurred");
}
}
/**
* Test #6.
*/
public static void test6() {
Probably<Immutator<Integer,Integer>> justIncr = Probably.just(new Incr());
Probably<Immutator<Integer,String>> justLength = Probably.just(new Length());
Probably<Immutator<Integer,Integer>> noIncr = Probably.none();
Probably<Immutator<Integer,String>> noLength = Probably.none();
try {
System.out.println(Probably.just(17).apply(justIncr).toString());
System.out.println(Probably.<Integer>none().apply(justIncr).toString());
System.out.println(Probably.just(17).apply(noIncr).toString());
System.out.println(Probably.<Integer>none().apply(noIncr).toString());
System.out.println(Probably.just("string").apply(justLength).toString());
System.out.println(Probably.<String>none().apply(justLength).toString());
System.out.println(Probably.just("string").apply(noLength).toString());
System.out.println(Probably.<String>none().apply(noLength).toString());
} catch(Exception e) {
System.out.println("Error occurred");
}
}
}

BIN
Lab4/Lab4.pdf Normal file

Binary file not shown.

9
Lab4/Print.java Normal file
View File

@ -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)
*/

96
Lab4/Probably.java Normal file
View File

@ -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<T> {
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 <T> Probably<T> none() {
@SuppressWarnings("unchecked")
Probably<T> res = (Probably<T>) 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 <T> Probably<T> just(T value) {
if (value == null) {
return none();
}
return (Probably<T>) 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() + ">";
}
}
}

35
Lab4/Test1.java Normal file
View File

@ -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();
}
}

45
Lab4/Test2.java Normal file
View File

@ -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();
}
}

57
Lab4/Test3.java Normal file
View File

@ -0,0 +1,57 @@
class Test3 {
public static void main(String[] args) {
CS2030STest we = new CS2030STest();
class Incr implements Immutator<Integer,Integer> {
public Integer invoke(Integer t1) {
return t1 + 1;
}
}
class Length implements Immutator<Integer,String> {
public Integer invoke(String t1) {
return t1.length();
}
}
we.prefix("jshell> class Incr implements Immutator<Integer,Integer> {");
we.prefix(" ...> public Integer invoke(Integer t1) {");
we.prefix(" ...> return t1 + 1;");
we.prefix(" ...> }");
we.prefix(" ...> }");
we.prefix("jshell> class Length implements Immutator<Integer,String> {");
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<Integer>().invoke(1).toString(),
"<1>");
we.expect("new Improbable<String>().invoke(null)",
new Improbable<String>().invoke(null).toString(),
"<>");
we.expect("new Improbable<Integer>().invoke(1).transform(new Incr())",
new Improbable<Integer>().invoke(1).transform(new Incr()).toString(),
"<2>");
we.expect("new Improbable<>().invoke(new Improbable<>().invoke(1))",
new Improbable<>().invoke(new Improbable<Integer>().invoke(1)).toString(),
"<<1>>");
we.showStat();
we.print();
}
}

Some files were not shown because too many files have changed in this diff Show More