feat: update structure
This commit is contained in:
53
cs2030s/labs/Lab1/Array.java
Executable file
53
cs2030s/labs/Lab1/Array.java
Executable file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* The Array for CS2030S
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY21/22 Semester 2
|
||||
*/
|
||||
|
||||
class Array<T extends Comparable<T>> {
|
||||
private T[] array;
|
||||
|
||||
public Array(int size) {
|
||||
// The only way to add values to `array` is via set(), and we can only put
|
||||
// objects of type T via that method. Thus, it is safe to cast Comparable[]
|
||||
// to T[].
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] temp = (T[]) new Comparable[size];
|
||||
this.array = temp;
|
||||
}
|
||||
|
||||
public void set(int index, T item) {
|
||||
this.array[index] = item;
|
||||
}
|
||||
|
||||
public T get(int index) {
|
||||
return this.array[index];
|
||||
}
|
||||
|
||||
public T min() {
|
||||
T result = this.array[0];
|
||||
for (T i : this.array) {
|
||||
if (i.compareTo(result) < 0) {
|
||||
result = i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return this.array.length;
|
||||
}
|
||||
|
||||
@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
cs2030s/labs/Lab1/ArrayTest.java
Executable file
64
cs2030s/labs/Lab1/ArrayTest.java
Executable 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);
|
||||
}
|
||||
}
|
||||
50
cs2030s/labs/Lab1/ArrivalEvent.java
Normal file
50
cs2030s/labs/Lab1/ArrivalEvent.java
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
/**
|
||||
* The ArrivalEvent is an Event which handles the arrival of a customer
|
||||
* It decides whether a customer should go into queue or go to a counter
|
||||
* It also allocates a counter to the customer. This event thus returns either a
|
||||
* DepartureEvent or a ServiceBeginEvent
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class ArrivalEvent extends Event {
|
||||
|
||||
private Customer customer;
|
||||
private Shop shop;
|
||||
|
||||
public ArrivalEvent(Customer customer, Shop shop) {
|
||||
super(customer.getArrivalTIme());
|
||||
this.customer = customer;
|
||||
this.shop = shop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
ServiceCounter availableCounter = this.shop.getAvailableCounter();
|
||||
// check if counters are available. If available, start service for that
|
||||
// customer
|
||||
if (availableCounter != null) {
|
||||
return new Event[] {
|
||||
new ServiceBeginEvent(this.getTime(), customer, shop, availableCounter) };
|
||||
}
|
||||
// if no counters available, check if queue slots avialable in counters
|
||||
availableCounter = this.shop.findCounterWithQueue();
|
||||
if (availableCounter != null) {
|
||||
return new Event[] {
|
||||
new JoinCounterQueueEvent(this.getTime(), this.customer, availableCounter)
|
||||
};
|
||||
}
|
||||
// if shop queue isn't empty, join shop queue
|
||||
if (!this.shop.isQueueFull()) {
|
||||
return new Event[] { new JoinShopQueueEvent(customer, shop) };
|
||||
}
|
||||
return new Event[] { new DepartureEvent(this.getTime(), customer) };
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s arrived %s", super.toString(), this.customer, this.shop);
|
||||
}
|
||||
}
|
||||
77
cs2030s/labs/Lab1/CS2030STest.java
Executable file
77
cs2030s/labs/Lab1/CS2030STest.java
Executable file
@@ -0,0 +1,77 @@
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import javax.tools.DiagnosticCollector;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
class CS2030STest {
|
||||
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
public static final String ANSI_RED = "\u001B[31m";
|
||||
public static final String ANSI_GREEN = "\u001B[32m";
|
||||
|
||||
/**
|
||||
* Test if two objects are equals.
|
||||
*
|
||||
* @param test A description of the test.
|
||||
* @param output The output from an expression.
|
||||
* @param expect The expected output from that expression.
|
||||
* @return this object.
|
||||
*/
|
||||
public CS2030STest 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);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an expression compiles with/without error.
|
||||
*
|
||||
* @param test A description of the test.
|
||||
* @param statement The java statement to compile
|
||||
* @param success Whether the statement is expected to compile or not
|
||||
* (true if yes; false otherwise)
|
||||
* @return this object.
|
||||
*/
|
||||
public CS2030STest 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);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
34
cs2030s/labs/Lab1/Customer.java
Normal file
34
cs2030s/labs/Lab1/Customer.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* This is a data class which holds information about the Customer.
|
||||
* It has a incrementing counter for the customer id;
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
public class Customer {
|
||||
private static int lastId = 0;
|
||||
|
||||
private final int id;
|
||||
private final double serviceTime;
|
||||
private final double arrivalTIme;
|
||||
|
||||
public Customer(double serviceTime, double arrivalTime) {
|
||||
this.id = lastId++;
|
||||
this.serviceTime = serviceTime;
|
||||
this.arrivalTIme = arrivalTime;
|
||||
}
|
||||
|
||||
public double getServiceTime() {
|
||||
return this.serviceTime;
|
||||
}
|
||||
|
||||
public double getArrivalTIme() {
|
||||
return arrivalTIme;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "C" + id;
|
||||
}
|
||||
|
||||
}
|
||||
28
cs2030s/labs/Lab1/DepartureEvent.java
Normal file
28
cs2030s/labs/Lab1/DepartureEvent.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* The DepartureEvent is an Event which handles the end of a service.
|
||||
* It handles allocating customers in queue to a counter.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class DepartureEvent extends Event {
|
||||
|
||||
private Customer customer;
|
||||
|
||||
public DepartureEvent(double time, Customer customer) {
|
||||
super(time);
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s departed", super.toString(), this.customer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
// when customer departs, check if there are customers in queue
|
||||
return new Event[] {};
|
||||
}
|
||||
|
||||
}
|
||||
72
cs2030s/labs/Lab1/Event.java
Normal file
72
cs2030s/labs/Lab1/Event.java
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* The Event class is an abstract class that encapsulates a
|
||||
* discrete event to be simulated. An event encapsulates the
|
||||
* time the event occurs. A subclass of event _must_ override
|
||||
* the simulate() method to implement the logic of the
|
||||
* simulation when this event is simulated. The simulate method
|
||||
* returns an array of events, which the simulator will then
|
||||
* add to the event queue. Note that an event also implements
|
||||
* the Comparable interface so that a PriorityQueue can
|
||||
* arrange the events in the order of event time.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
abstract class Event implements Comparable<Event> {
|
||||
/** The time this event occurs in the simulation. */
|
||||
private final double time;
|
||||
|
||||
/**
|
||||
* Creates an event that occurs at the given time.
|
||||
*
|
||||
* @param time The time the event occurs.
|
||||
*/
|
||||
public Event(double time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter to return the time of this event.
|
||||
*
|
||||
* @return The time this event occurs.
|
||||
*/
|
||||
public double getTime() {
|
||||
return this.time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare this event with a given event e.
|
||||
*
|
||||
* @param e The other event to compare to.
|
||||
* @return 1 if this event occurs later than e;
|
||||
* 0 if they occur the same time;
|
||||
* -1 if this event occurs earlier.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Event e) {
|
||||
if (this.time > e.time) {
|
||||
return 1;
|
||||
} else if (this.time == e.time) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the string representation this event.
|
||||
*
|
||||
* @return A string consists of the time this event occurs.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%.3f", this.time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate this event.
|
||||
*
|
||||
* @return An array of new events to be scheduled by the simulator.
|
||||
*/
|
||||
public abstract Event[] simulate();
|
||||
}
|
||||
23
cs2030s/labs/Lab1/JoinCounterQueueEvent.java
Normal file
23
cs2030s/labs/Lab1/JoinCounterQueueEvent.java
Normal file
@@ -0,0 +1,23 @@
|
||||
public class JoinCounterQueueEvent extends Event {
|
||||
private Customer customer;
|
||||
private ServiceCounter counter;
|
||||
|
||||
public JoinCounterQueueEvent(double time, Customer customer, ServiceCounter counter) {
|
||||
super(time);
|
||||
this.customer = customer;
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
this.counter.joinQueue(customer);
|
||||
return new Event[] {};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s joined counter queue (at %s)",
|
||||
super.toString(),
|
||||
this.customer, this.counter);
|
||||
}
|
||||
}
|
||||
24
cs2030s/labs/Lab1/JoinShopQueueEvent.java
Normal file
24
cs2030s/labs/Lab1/JoinShopQueueEvent.java
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
class JoinShopQueueEvent extends Event {
|
||||
private Customer customer;
|
||||
private Shop shop;
|
||||
|
||||
public JoinShopQueueEvent(Customer customer, Shop shop) {
|
||||
super(customer.getArrivalTIme());
|
||||
this.customer = customer;
|
||||
this.shop = shop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
this.shop.joinQueue(customer);
|
||||
return new Event[] {};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s joined shop queue %s",
|
||||
super.toString(),
|
||||
this.customer, this.shop);
|
||||
}
|
||||
}
|
||||
26
cs2030s/labs/Lab1/Lab1.java
Normal file
26
cs2030s/labs/Lab1/Lab1.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* The main class for CS2030S Lab 1.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class Lab1 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
26
cs2030s/labs/Lab1/Lab2.java
Normal file
26
cs2030s/labs/Lab1/Lab2.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* The main class for CS2030S Lab 1.
|
||||
*
|
||||
* @author Wei Tsang
|
||||
* @version CS2030S AY20/21 Semester 2
|
||||
*/
|
||||
class Lab2 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
26
cs2030s/labs/Lab1/Lab3.java
Executable file
26
cs2030s/labs/Lab1/Lab3.java
Executable 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();
|
||||
}
|
||||
}
|
||||
17
cs2030s/labs/Lab1/Makefile
Normal file
17
cs2030s/labs/Lab1/Makefile
Normal file
@@ -0,0 +1,17 @@
|
||||
CLASSES := $(wildcard *.java)
|
||||
|
||||
default: classes
|
||||
|
||||
lab1:
|
||||
java Lab1
|
||||
|
||||
lab2:
|
||||
java Lab2
|
||||
|
||||
classes: $(CLASSES:.java=.class)
|
||||
|
||||
%.class : %.java
|
||||
javac "$<"
|
||||
|
||||
clean:
|
||||
$(RM) *.class
|
||||
125
cs2030s/labs/Lab1/Queue.java
Normal file
125
cs2030s/labs/Lab1/Queue.java
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* The Queue class implements a simple FIFO data structure
|
||||
* with limited capacity that can store any Object instances.
|
||||
* Not to be confused with java.util.Queue.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY21/22 Semester 2
|
||||
*/
|
||||
class Queue<T> {
|
||||
/** An array to store the items in the queue. */
|
||||
private T[] items;
|
||||
|
||||
/** Index of the first element in the queue. */
|
||||
private int first;
|
||||
|
||||
/** Index of the last element in the queue. */
|
||||
private int last;
|
||||
|
||||
/** Maximum size of the queue. */
|
||||
private int maxSize;
|
||||
|
||||
/** Number of elements in the queue. */
|
||||
private int len;
|
||||
|
||||
/**
|
||||
* Constructor for a queue.
|
||||
*
|
||||
* @param size The maximum num of elements we can put in the queue.
|
||||
*/
|
||||
public Queue(int size) {
|
||||
this.maxSize = size;
|
||||
// The only way to add values to `items` is via enq(), and we can only put
|
||||
// objects of type T via that method. Thus, it is safe to cast Comparable[]
|
||||
// to T[].
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] temp = (T[]) new Object[size];
|
||||
this.items = temp;
|
||||
this.first = -1;
|
||||
this.last = -1;
|
||||
this.len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the object e into the queue.
|
||||
*
|
||||
* @param e The item to put in the queue.
|
||||
* @return false if the queue is full; true if e is added successfully.
|
||||
*/
|
||||
public boolean enq(T e) {
|
||||
if (this.isFull()) {
|
||||
return false;
|
||||
}
|
||||
if (this.isEmpty()) {
|
||||
this.first = 0;
|
||||
this.last = 0;
|
||||
} else {
|
||||
this.last = (this.last + 1) % this.maxSize;
|
||||
}
|
||||
this.items[last] = e;
|
||||
this.len += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the object from the queue.
|
||||
*
|
||||
* @return null if the queue is empty; the object removed from the queue
|
||||
* otherwise.
|
||||
*/
|
||||
public T deq() {
|
||||
if (this.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
T item = this.items[this.first];
|
||||
this.first = (this.first + 1) % this.maxSize;
|
||||
this.len -= 1;
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the queue is full.
|
||||
*
|
||||
* @return true if the queue is full; false otherwise.
|
||||
*/
|
||||
boolean isFull() {
|
||||
return (this.len == this.maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the queue is empty.
|
||||
*
|
||||
* @return true if the queue is empty; false otherwise.
|
||||
*/
|
||||
boolean isEmpty() {
|
||||
return (this.len == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of elements in the queue.
|
||||
*
|
||||
* @return The number of elements in the queue.
|
||||
*/
|
||||
public int length() {
|
||||
return this.len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the queue.
|
||||
*
|
||||
* @return A string consisting of the string representation of
|
||||
* every object in the queue.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = "[ ";
|
||||
int i = this.first;
|
||||
int count = 0;
|
||||
while (count < this.len) {
|
||||
str += this.items[i] + " ";
|
||||
i = (i + 1) % this.maxSize;
|
||||
count++;
|
||||
}
|
||||
return str + "]";
|
||||
}
|
||||
}
|
||||
23
cs2030s/labs/Lab1/QueueTest.java
Executable file
23
cs2030s/labs/Lab1/QueueTest.java
Executable 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);
|
||||
}
|
||||
}
|
||||
34
cs2030s/labs/Lab1/ServiceBeginEvent.java
Normal file
34
cs2030s/labs/Lab1/ServiceBeginEvent.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* The ServiceBeginEvent is an Event which handles the starting of a service.
|
||||
* It handles occupying a counter and also generating a serviceEndEvent
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class ServiceBeginEvent extends Event {
|
||||
|
||||
private ServiceCounter counter;
|
||||
private Customer customer;
|
||||
private Shop shop;
|
||||
|
||||
public ServiceBeginEvent(double time, Customer customer, Shop shop, ServiceCounter counter) {
|
||||
super(time);
|
||||
this.customer = customer;
|
||||
this.shop = shop;
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString()
|
||||
+ String.format(": %s service begin (by %s)", this.customer, this.counter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
this.counter.occupy();
|
||||
double endTime = this.getTime() + this.customer.getServiceTime();
|
||||
return new Event[] {
|
||||
new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) };
|
||||
}
|
||||
}
|
||||
64
cs2030s/labs/Lab1/ServiceCounter.java
Normal file
64
cs2030s/labs/Lab1/ServiceCounter.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* This is a data class which holds information about a counter. It has a
|
||||
* incrementing counter for the conuter Id.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
public class ServiceCounter implements Comparable<ServiceCounter> {
|
||||
private static int lastId;
|
||||
|
||||
private final int id;
|
||||
private boolean available;
|
||||
private Queue<Customer> queue;
|
||||
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
public void occupy() {
|
||||
this.available = false;
|
||||
}
|
||||
|
||||
public void free() {
|
||||
this.available = true;
|
||||
}
|
||||
|
||||
public boolean isQueueFull() {
|
||||
return this.queue.isFull();
|
||||
}
|
||||
|
||||
public boolean isQueueEmpty() {
|
||||
return this.queue.isEmpty();
|
||||
}
|
||||
|
||||
public void joinQueue(Customer customer) {
|
||||
this.queue.enq(customer);
|
||||
}
|
||||
|
||||
public Customer leaveQueue() {
|
||||
return this.queue.deq();
|
||||
}
|
||||
|
||||
public ServiceCounter(int queueSize) {
|
||||
this.id = lastId++;
|
||||
this.available = true;
|
||||
this.queue = new Queue<Customer>(queueSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("S%s %s", id, this.queue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ServiceCounter o) {
|
||||
if (this.queue.length() < o.queue.length()) {
|
||||
return -1;
|
||||
}
|
||||
if (this.id < o.id) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
58
cs2030s/labs/Lab1/ServiceEndEvent.java
Normal file
58
cs2030s/labs/Lab1/ServiceEndEvent.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* The ServiceEndEvent is an Event which handles the end of a service.
|
||||
* It handles freeing the counter and also generating a departure event
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class ServiceEndEvent extends Event {
|
||||
|
||||
private ServiceCounter counter;
|
||||
private Customer customer;
|
||||
private Shop shop;
|
||||
|
||||
public ServiceEndEvent(double time, Customer customer, Shop shop, ServiceCounter counter) {
|
||||
super(time);
|
||||
this.customer = customer;
|
||||
this.shop = shop;
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString()
|
||||
+ String.format(": %s service done (by %s)", this.customer, this.counter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event[] simulate() {
|
||||
// if there are customers in the counter queue, they will be serviced next.
|
||||
// Customers in the shop queue will then be added to the counter queue
|
||||
if (!this.counter.isQueueEmpty()) {
|
||||
Customer serviceCustomer = this.counter.leaveQueue();
|
||||
|
||||
if (!this.shop.isQueueEmpty()) {
|
||||
return new Event[] {
|
||||
new DepartureEvent(this.getTime(), this.customer),
|
||||
new ServiceBeginEvent(this.getTime(), serviceCustomer, this.shop, this.counter),
|
||||
new JoinCounterQueueEvent(this.getTime(), this.shop.leaveQueue(), counter)
|
||||
};
|
||||
}
|
||||
return new Event[] {
|
||||
new DepartureEvent(this.getTime(), this.customer),
|
||||
new ServiceBeginEvent(this.getTime(), serviceCustomer, this.shop, this.counter),
|
||||
};
|
||||
}
|
||||
// There can also be the case where the counter queue may be empty but there are
|
||||
// customers in the shop queue.
|
||||
if (!this.shop.isQueueEmpty()) {
|
||||
return new Event[] {
|
||||
new DepartureEvent(this.getTime(), this.customer),
|
||||
new ServiceBeginEvent(this.getTime(), this.shop.leaveQueue(), this.shop, this.counter),
|
||||
};
|
||||
}
|
||||
// else there are no more customers in the queue, and the counter can be freed
|
||||
this.counter.free();
|
||||
return new Event[] { new DepartureEvent(this.getTime(), this.customer) };
|
||||
}
|
||||
}
|
||||
67
cs2030s/labs/Lab1/Shop.java
Normal file
67
cs2030s/labs/Lab1/Shop.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Shop is a data class which holds information about a shop.
|
||||
* It stores the list of counters and the queue in which new customers can join.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
public class Shop {
|
||||
private Array<ServiceCounter> counters;
|
||||
private Queue<Customer> queue;
|
||||
|
||||
public Shop(int numOfCounters, int shopQueueSize, int counterQueueSize) {
|
||||
this.counters = new Array<ServiceCounter>(numOfCounters);
|
||||
for (int i = 0; i < numOfCounters; i++) {
|
||||
this.counters.set(i, new ServiceCounter(counterQueueSize));
|
||||
}
|
||||
|
||||
this.queue = new Queue<Customer>(shopQueueSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* getAvailableCounter returns the first available counter it finds.
|
||||
* If there are none, returns null
|
||||
*
|
||||
* @return the available counter or null if none found
|
||||
*/
|
||||
|
||||
public ServiceCounter getAvailableCounter() {
|
||||
for (int i = 0; i < this.counters.length(); i++) {
|
||||
ServiceCounter counter = this.counters.get(i);
|
||||
if (counter.isAvailable()) {
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isQueueFull() {
|
||||
return this.queue.isFull();
|
||||
}
|
||||
|
||||
public boolean isQueueEmpty() {
|
||||
return this.queue.isEmpty();
|
||||
}
|
||||
|
||||
public void joinQueue(Customer customer) {
|
||||
this.queue.enq(customer);
|
||||
}
|
||||
|
||||
public Customer leaveQueue() {
|
||||
return this.queue.deq();
|
||||
}
|
||||
|
||||
public ServiceCounter findCounterWithQueue() {
|
||||
ServiceCounter minCounter = this.counters.min();
|
||||
if (!minCounter.isQueueFull()) {
|
||||
return minCounter;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.queue.toString();
|
||||
}
|
||||
|
||||
}
|
||||
53
cs2030s/labs/Lab1/ShopSimulation.java
Normal file
53
cs2030s/labs/Lab1/ShopSimulation.java
Normal file
@@ -0,0 +1,53 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class implements a shop simulation.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
class ShopSimulation extends Simulation {
|
||||
|
||||
/**
|
||||
* The list of customer arrival events to populate
|
||||
* the simulation with.
|
||||
*/
|
||||
private Event[] initEvents;
|
||||
|
||||
/**
|
||||
* Constructor for a shop simulation.
|
||||
*
|
||||
* @param sc A scanner to read the parameters from. The first
|
||||
* integer scanned is the number of customers; followed
|
||||
* by the number of service counters. Next is a
|
||||
* sequence of (arrival time, service time) pair, each
|
||||
* pair represents a customer.
|
||||
*/
|
||||
public ShopSimulation(Scanner sc) {
|
||||
initEvents = new Event[sc.nextInt()];
|
||||
int numOfCounters = sc.nextInt();
|
||||
int maxCounterQueueSize = sc.nextInt();
|
||||
int maxShopQueueSize = sc.nextInt();
|
||||
|
||||
Shop shop = new Shop(numOfCounters, maxShopQueueSize, maxCounterQueueSize);
|
||||
|
||||
int id = 0;
|
||||
while (sc.hasNextDouble()) {
|
||||
double arrivalTime = sc.nextDouble();
|
||||
double serviceTime = sc.nextDouble();
|
||||
initEvents[id] = new ArrivalEvent(new Customer(serviceTime, arrivalTime), shop);
|
||||
id += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an array of events to populate the
|
||||
* simulator with.
|
||||
*
|
||||
* @return An array of events for the simulator.
|
||||
*/
|
||||
@Override
|
||||
public Event[] getInitialEvents() {
|
||||
return initEvents;
|
||||
}
|
||||
}
|
||||
20
cs2030s/labs/Lab1/Simulation.java
Normal file
20
cs2030s/labs/Lab1/Simulation.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* This class is a general abstract class that
|
||||
* encapsulates a simulation. To implement a
|
||||
* simulation, inherit from this class and implement
|
||||
* the `getInitialEvents` method.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
abstract class Simulation {
|
||||
/**
|
||||
* An abstract method to return an array of events
|
||||
* used to initialize the simulation.
|
||||
*
|
||||
* @return An array of initial events that the
|
||||
* simulator can use to kick-start the
|
||||
* simulation.
|
||||
*/
|
||||
public abstract Event[] getInitialEvents();
|
||||
}
|
||||
51
cs2030s/labs/Lab1/Simulator.java
Normal file
51
cs2030s/labs/Lab1/Simulator.java
Normal file
@@ -0,0 +1,51 @@
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* This class implements a discrete event simulator.
|
||||
* The simulator maintains a priority queue of events.
|
||||
* It runs through the events and simulates each one until
|
||||
* the queue is empty.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY22/23 Semester 2
|
||||
*/
|
||||
public class Simulator {
|
||||
/** The event queue. */
|
||||
private final PriorityQueue<Event> events;
|
||||
|
||||
/**
|
||||
* The constructor for a simulator. It takes in
|
||||
* a simulation as an argument, and calls the
|
||||
* getInitialEvents method of that simulation to
|
||||
* initialize the event queue.
|
||||
*
|
||||
* @param simulation The simulation to simulate.
|
||||
*/
|
||||
public Simulator(Simulation simulation) {
|
||||
this.events = new PriorityQueue<Event>();
|
||||
for (Event e : simulation.getInitialEvents()) {
|
||||
this.events.add(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the simulation until no more events is in
|
||||
* the queue. For each event in the queue (in
|
||||
* increasing order of time), print out its string
|
||||
* representation, then simulate it. If the
|
||||
* simulation returns one or more events, add them
|
||||
* to the queue, and repeat.
|
||||
*/
|
||||
public void run() {
|
||||
Event event = this.events.poll();
|
||||
while (event != null) {
|
||||
System.out.println(event);
|
||||
Event[] newEvents = event.simulate();
|
||||
for (Event e : newEvents) {
|
||||
this.events.add(e);
|
||||
}
|
||||
event = this.events.poll();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
4
cs2030s/labs/Lab1/inputs/Lab1.1.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab1.1.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 1
|
||||
1.0 1.0
|
||||
3.0 1.0
|
||||
5.0 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab1.2.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab1.2.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 1
|
||||
1.1 2.0
|
||||
2.2 2.0
|
||||
3.3 2.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab1.3.in
Normal file
6
cs2030s/labs/Lab1/inputs/Lab1.3.in
Normal file
@@ -0,0 +1,6 @@
|
||||
5 2
|
||||
1.0 1.0
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
5
cs2030s/labs/Lab1/inputs/Lab1.4.in
Normal file
5
cs2030s/labs/Lab1/inputs/Lab1.4.in
Normal file
@@ -0,0 +1,5 @@
|
||||
4 2
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
2.2 1.0
|
||||
2.3 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab1.5.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab1.5.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 2
|
||||
1.0 4
|
||||
2.1 1
|
||||
4.2 1
|
||||
4
cs2030s/labs/Lab1/inputs/Lab2.1.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab2.1.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 1 2
|
||||
1.0 1.0
|
||||
3.0 1.0
|
||||
5.0 1.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab2.10.in
Normal file
6
cs2030s/labs/Lab1/inputs/Lab2.10.in
Normal file
@@ -0,0 +1,6 @@
|
||||
5 2 2
|
||||
1.0 1.5
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab2.2.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab2.2.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 1 2
|
||||
1.1 2.0
|
||||
2.2 2.0
|
||||
3.3 2.0
|
||||
7
cs2030s/labs/Lab1/inputs/Lab2.3.in
Normal file
7
cs2030s/labs/Lab1/inputs/Lab2.3.in
Normal file
@@ -0,0 +1,7 @@
|
||||
6 1 2
|
||||
1.1 2
|
||||
1.2 2
|
||||
1.3 2
|
||||
1.4 2
|
||||
4.0 2
|
||||
5.0 2
|
||||
7
cs2030s/labs/Lab1/inputs/Lab2.4.in
Normal file
7
cs2030s/labs/Lab1/inputs/Lab2.4.in
Normal file
@@ -0,0 +1,7 @@
|
||||
6 1 3
|
||||
1.1 2
|
||||
1.2 2
|
||||
1.3 2
|
||||
1.4 2
|
||||
4.0 2
|
||||
5.0 2
|
||||
5
cs2030s/labs/Lab1/inputs/Lab2.5.in
Normal file
5
cs2030s/labs/Lab1/inputs/Lab2.5.in
Normal file
@@ -0,0 +1,5 @@
|
||||
4 2 1
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
2.2 1.0
|
||||
2.3 1.0
|
||||
5
cs2030s/labs/Lab1/inputs/Lab2.6.in
Normal file
5
cs2030s/labs/Lab1/inputs/Lab2.6.in
Normal file
@@ -0,0 +1,5 @@
|
||||
4 2 2
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
2.2 1.0
|
||||
2.3 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab2.7.in
Normal file
4
cs2030s/labs/Lab1/inputs/Lab2.7.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 2 1
|
||||
1.0 4
|
||||
2.1 1
|
||||
4.2 1
|
||||
6
cs2030s/labs/Lab1/inputs/Lab2.8.in
Normal file
6
cs2030s/labs/Lab1/inputs/Lab2.8.in
Normal file
@@ -0,0 +1,6 @@
|
||||
5 2 1
|
||||
1.0 1.0
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab2.9.in
Normal file
6
cs2030s/labs/Lab1/inputs/Lab2.9.in
Normal file
@@ -0,0 +1,6 @@
|
||||
5 2 2
|
||||
1.0 1.0
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab3.1.in
Executable file
4
cs2030s/labs/Lab1/inputs/Lab3.1.in
Executable file
@@ -0,0 +1,4 @@
|
||||
3 1 0 2
|
||||
1.0 1.0
|
||||
3.0 1.0
|
||||
5.0 1.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab3.10.in
Executable file
6
cs2030s/labs/Lab1/inputs/Lab3.10.in
Executable file
@@ -0,0 +1,6 @@
|
||||
5 2 0 2
|
||||
1.0 1.5
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab3.11.in
Executable file
6
cs2030s/labs/Lab1/inputs/Lab3.11.in
Executable file
@@ -0,0 +1,6 @@
|
||||
5 1 2 0
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
1.2 1.0
|
||||
1.3 1.0
|
||||
1.4 1.0
|
||||
12
cs2030s/labs/Lab1/inputs/Lab3.12.in
Executable file
12
cs2030s/labs/Lab1/inputs/Lab3.12.in
Executable file
@@ -0,0 +1,12 @@
|
||||
11 3 3 0
|
||||
1.0 2
|
||||
1.1 1
|
||||
1.2 1
|
||||
1.3 1
|
||||
1.4 1
|
||||
1.5 2
|
||||
1.6 2
|
||||
1.7 2
|
||||
1.8 2
|
||||
1.9 2
|
||||
2.15 1
|
||||
12
cs2030s/labs/Lab1/inputs/Lab3.13.in
Executable file
12
cs2030s/labs/Lab1/inputs/Lab3.13.in
Executable file
@@ -0,0 +1,12 @@
|
||||
11 3 3 0
|
||||
1.0 2
|
||||
1.1 1
|
||||
1.2 1
|
||||
1.3 1
|
||||
1.4 1
|
||||
1.5 2
|
||||
1.6 2
|
||||
1.7 2
|
||||
1.8 2
|
||||
1.9 2
|
||||
2.25 1
|
||||
13
cs2030s/labs/Lab1/inputs/Lab3.14.in
Executable file
13
cs2030s/labs/Lab1/inputs/Lab3.14.in
Executable file
@@ -0,0 +1,13 @@
|
||||
12 3 2 2
|
||||
1.0 2
|
||||
1.1 2
|
||||
1.2 2
|
||||
1.3 2
|
||||
1.4 2
|
||||
1.5 2
|
||||
1.6 2
|
||||
1.7 2
|
||||
1.8 2
|
||||
1.9 2
|
||||
2.0 2
|
||||
2.1 2
|
||||
13
cs2030s/labs/Lab1/inputs/Lab3.15.in
Executable file
13
cs2030s/labs/Lab1/inputs/Lab3.15.in
Executable file
@@ -0,0 +1,13 @@
|
||||
12 3 2 3
|
||||
1.0 2
|
||||
1.1 3
|
||||
1.2 2
|
||||
1.3 3
|
||||
1.4 2
|
||||
1.5 3
|
||||
1.6 2
|
||||
1.7 3
|
||||
1.8 2
|
||||
1.9 3
|
||||
2.0 2
|
||||
2.1 3
|
||||
4
cs2030s/labs/Lab1/inputs/Lab3.2.in
Executable file
4
cs2030s/labs/Lab1/inputs/Lab3.2.in
Executable file
@@ -0,0 +1,4 @@
|
||||
3 1 0 2
|
||||
1.1 2.0
|
||||
2.2 2.0
|
||||
3.3 2.0
|
||||
7
cs2030s/labs/Lab1/inputs/Lab3.3.in
Executable file
7
cs2030s/labs/Lab1/inputs/Lab3.3.in
Executable file
@@ -0,0 +1,7 @@
|
||||
6 1 0 2
|
||||
1.1 2
|
||||
1.2 2
|
||||
1.3 2
|
||||
1.4 2
|
||||
4.0 2
|
||||
5.0 2
|
||||
7
cs2030s/labs/Lab1/inputs/Lab3.4.in
Executable file
7
cs2030s/labs/Lab1/inputs/Lab3.4.in
Executable file
@@ -0,0 +1,7 @@
|
||||
6 1 0 3
|
||||
1.1 2
|
||||
1.2 2
|
||||
1.3 2
|
||||
1.4 2
|
||||
4.0 2
|
||||
5.0 2
|
||||
5
cs2030s/labs/Lab1/inputs/Lab3.5.in
Executable file
5
cs2030s/labs/Lab1/inputs/Lab3.5.in
Executable file
@@ -0,0 +1,5 @@
|
||||
4 2 0 1
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
2.2 1.0
|
||||
2.3 1.0
|
||||
5
cs2030s/labs/Lab1/inputs/Lab3.6.in
Executable file
5
cs2030s/labs/Lab1/inputs/Lab3.6.in
Executable file
@@ -0,0 +1,5 @@
|
||||
4 2 0 2
|
||||
1.0 1.0
|
||||
1.1 1.0
|
||||
2.2 1.0
|
||||
2.3 1.0
|
||||
4
cs2030s/labs/Lab1/inputs/Lab3.7.in
Executable file
4
cs2030s/labs/Lab1/inputs/Lab3.7.in
Executable file
@@ -0,0 +1,4 @@
|
||||
3 2 0 1
|
||||
1.0 4
|
||||
2.1 1
|
||||
4.2 1
|
||||
6
cs2030s/labs/Lab1/inputs/Lab3.8.in
Executable file
6
cs2030s/labs/Lab1/inputs/Lab3.8.in
Executable file
@@ -0,0 +1,6 @@
|
||||
5 2 0 1
|
||||
1.0 1.0
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
6
cs2030s/labs/Lab1/inputs/Lab3.9.in
Executable file
6
cs2030s/labs/Lab1/inputs/Lab3.9.in
Executable file
@@ -0,0 +1,6 @@
|
||||
5 2 0 2
|
||||
1.0 1.0
|
||||
1.2 1.0
|
||||
1.4 1.0
|
||||
1.6 1.0
|
||||
2.1 1.0
|
||||
77
cs2030s/labs/Lab1/test.sh
Executable file
77
cs2030s/labs/Lab1/test.sh
Executable file
@@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
set -o nounset
|
||||
function control_c() {
|
||||
if [ -e $out ]
|
||||
then
|
||||
rm -f $out
|
||||
fi
|
||||
}
|
||||
|
||||
trap control_c INT
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "usage: $0 <main class>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PROG=$1
|
||||
if [ ! -e $PROG.class ]
|
||||
then
|
||||
echo "$PROG.class does not exist. Have you compiled it with make or javac?"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
num_failed=0
|
||||
i=1
|
||||
while true
|
||||
do
|
||||
if [ -e inputs/$PROG.$i.in ]
|
||||
then
|
||||
if [ $(uname) == "Darwin" ]
|
||||
then
|
||||
out=$(mktemp -t $PROG)
|
||||
else
|
||||
out=$(mktemp --suffix=$PROG)
|
||||
fi
|
||||
java $PROG < inputs/$PROG.$i.in > $out
|
||||
status="$?"
|
||||
if [ "$status" -ne "0" ]
|
||||
then
|
||||
echo "$PROG: return non-zero status $status for test case $i"
|
||||
# cat inputs/$PROG.$i.in
|
||||
num_failed=$((num_failed + 1))
|
||||
else
|
||||
if [ -e $out ]
|
||||
then
|
||||
if [ `diff -bB $out outputs/$PROG.$i.out | wc -l` -ne 0 ]
|
||||
then
|
||||
echo "$PROG test $i: failed"
|
||||
#cat inputs/$PROG.$i.in
|
||||
num_failed=$((num_failed + 1))
|
||||
else
|
||||
echo "$PROG test $i: passed"
|
||||
fi
|
||||
rm -f $out
|
||||
else
|
||||
echo "$PROG: cannot find output file. Execution interrupted?"
|
||||
num_failed=$((num_failed + 1))
|
||||
fi
|
||||
fi
|
||||
i=$((i + 1))
|
||||
else
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $i -eq 1 ]
|
||||
then
|
||||
echo "$PROG: no test cases found 🤷"
|
||||
elif [ $num_failed -eq 0 ]
|
||||
then
|
||||
echo "$PROG: passed everything 🎉"
|
||||
fi
|
||||
# Run style checker
|
||||
#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
|
||||
Reference in New Issue
Block a user