feat: add Lab3 base

This commit is contained in:
Yadunand Prem 2022-09-08 01:59:09 +08:00
parent f1826044a7
commit a300c1f0b5
6 changed files with 156 additions and 6 deletions

2
.gitignore vendored
View File

@ -3,4 +3,4 @@ Lab1.pdf
*.class
checkstyle.jar
cs2030_checks.xml
cs2030_checks.xml

37
Array.java Executable file
View File

@ -0,0 +1,37 @@
/**
* The Array<T> for CS2030S
*
* @author XXX
* @version CS2030S AY21/22 Semester 2
*/
class Array<T> { // TODO: Change to bounded type parameter
private T[] array;
Array(int size) {
// TODO
}
public void set(int index, T item) {
// TODO
}
public T get(int index) {
// TODO
}
public T min() {
// TODO
}
@Override
public String toString() {
StringBuilder s = new StringBuilder("[ ");
for (int i = 0; i < array.length; i++) {
s.append(i + ":" + array[i]);
if (i != array.length - 1) {
s.append(", ");
}
}
return s.append(" ]").toString();
}
}

64
ArrayTest.java Executable file
View File

@ -0,0 +1,64 @@
class ArrayTest {
public static void main(String[] args) {
CS2030STest i = new CS2030STest();
Array<Integer> a = new Array<Integer>(4);
i.expect("initializing an empty array",
a.toString(), "[ 0:null, 1:null, 2:null, 3:null ]");
a.set(0, 3);
i.expect("set element 0 to 3",
a.toString(), "[ 0:3, 1:null, 2:null, 3:null ]");
a.set(1, 4);
i.expect("set element 1 to 4",
a.toString(), "[ 0:3, 1:4, 2:null, 3:null ]");
a.set(2, 1);
i.expect("set element 2 to 1",
a.toString(), "[ 0:3, 1:4, 2:1, 3:null ]");
a.set(3, 6);
i.expect("set element 3 to 6",
a.toString(), "[ 0:3, 1:4, 2:1, 3:6 ]");
i.expect("get element 0",
a.get(0), 3);
i.expect("get element 1",
a.get(1), 4);
i.expect("get element 2",
a.get(2), 1);
i.expect("get element 3",
a.get(3), 6);
i.expect("smallest element is 1",
a.min(), 1);
i.expect("a.min() does not change the array",
a.toString(), "[ 0:3, 1:4, 2:1, 3:6 ]");
a.set(2, 9);
i.expect("update element 2 to 9",
a.toString(), "[ 0:3, 1:4, 2:9, 3:6 ]");
i.expect("smallest element is now 3",
a.min(), 3);
i.expectCompile("cannot put a non-integer into an array of integer",
"new Array<Integer>(4).set(0, false)", false);
i.expectCompile("cannot get a non-integer from an array of integer",
"String s = new Array<Integer>(4).get(0)", false);
i.expectCompile("cannot create an array of non-comparable element",
"class A {} Array<A> a;", false);
i.expectCompile("cannot create an array of comparable element (but not to itself)",
"class A implements Comparable<Long> {" +
" public int compareTo(Long i) {" +
" return 0; " +
" }" +
"}" +
"Array<A> a;", false);
i.expectCompile("can create an array of comparable element (to itself)",
"class A implements Comparable<A> {" +
" public int compareTo(A i) {" +
" return 0; " +
" }" +
"}" +
"Array<A> a;", true);
}
}

26
Lab3.java Executable file
View File

@ -0,0 +1,26 @@
import java.util.Scanner;
/**
* The main class for CS2030S Lab 3.
*
* @author Wei Tsang
* @version CS2030S AY21/22 Semester 2
*/
class Lab3 {
public static void main(String[] args) {
// Create a scanner to read from standard input.
Scanner sc = new Scanner(System.in);
// Create a simulation. The ShopSimulation
// constructor will read the simulation parameters
// and initial events using the scanner.
Simulation simulation = new ShopSimulation(sc);
// Create a new simulator and run the simulation
new Simulator(simulation).run();
// Clean up the scanner.
sc.close();
}
}

23
QueueTest.java Executable file
View File

@ -0,0 +1,23 @@
class QueueTest {
public static void main(String[] args) {
CS2030STest i = new CS2030STest();
Queue<Integer> q = new Queue<Integer>(2);
i.expect("insert 4 into a queue of integer",
q.enq(4), true);
i.expect("insert 8 into a queue of integer",
q.enq(8), true);
i.expect("insert 0 into a full queue",
q.enq(0), false);
i.expect("remove 4 from queue",
q.deq(), 4);
i.expect("remove 8 from queue",
q.deq(), 8);
i.expect("cannot deque anymore",
q.deq(), null);
i.expectCompile("cannot deque a non-integer from a queue of integer",
"String s = new Queue<Integer>(3).deq();", false);
i.expectCompile("cannot insert a non-integer into a queue of integer",
"new Queue<Integer>(3).enqueue(false);", false);
}
}

10
test.sh Normal file → Executable file
View File

@ -1,4 +1,5 @@
#!/bin/bash
set -o nounset
function control_c() {
if [ -e $out ]
then
@ -33,7 +34,7 @@ do
else
out=$(mktemp --suffix=$PROG)
fi
java $PROG < inputs/$PROG.$i.in > $out
java $PROG < inputs/$PROG.$i.in | head -c 50MB > $out
status="$?"
if [ "$status" -ne "0" ]
then
@ -45,11 +46,11 @@ do
then
if [ `diff -bB $out outputs/$PROG.$i.out | wc -l` -ne 0 ]
then
echo "test $i: failed"
echo "$PROG test $i: failed"
#cat inputs/$PROG.$i.in
num_failed=$((num_failed + 1))
else
echo "test $i: passed"
echo "$PROG test $i: passed"
fi
rm -f $out
else
@ -70,7 +71,6 @@ elif [ $num_failed -eq 0 ]
then
echo "$PROG: passed everything 🎉"
fi
# Run style checker
java -jar ./checkstyle.jar -c ./cs2030_checks.xml *.java
java -jar ~cs2030s/bin/checkstyle.jar -c ~cs2030s/bin/cs2030_checks.xml *.java
# vim:noexpandtab:sw=4:ts=4