From ed77b0e10f183ae581f210e1a0a9964eb4f242d7 Mon Sep 17 00:00:00 2001 From: Yadunand Prem Date: Thu, 1 Sep 2022 10:13:37 +0800 Subject: [PATCH] feat: add lab2 params --- Lab2.java | 26 ++++++++++ Makefile | 5 +- Queue.java | 119 ++++++++++++++++++++++++++++++++++++++++++++ inputs/Lab2.1.in | 4 ++ inputs/Lab2.10.in | 6 +++ inputs/Lab2.2.in | 4 ++ inputs/Lab2.3.in | 7 +++ inputs/Lab2.4.in | 7 +++ inputs/Lab2.5.in | 5 ++ inputs/Lab2.6.in | 5 ++ inputs/Lab2.7.in | 4 ++ inputs/Lab2.8.in | 6 +++ inputs/Lab2.9.in | 6 +++ outputs/Lab2.1.out | 12 +++++ outputs/Lab2.10.out | 20 ++++++++ outputs/Lab2.2.out | 14 ++++++ outputs/Lab2.3.out | 23 +++++++++ outputs/Lab2.4.out | 26 ++++++++++ outputs/Lab2.5.out | 16 ++++++ outputs/Lab2.6.out | 16 ++++++ outputs/Lab2.7.out | 12 +++++ outputs/Lab2.8.out | 20 ++++++++ outputs/Lab2.9.out | 23 +++++++++ 23 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 Lab2.java create mode 100644 Queue.java create mode 100644 inputs/Lab2.1.in create mode 100644 inputs/Lab2.10.in create mode 100644 inputs/Lab2.2.in create mode 100644 inputs/Lab2.3.in create mode 100644 inputs/Lab2.4.in create mode 100644 inputs/Lab2.5.in create mode 100644 inputs/Lab2.6.in create mode 100644 inputs/Lab2.7.in create mode 100644 inputs/Lab2.8.in create mode 100644 inputs/Lab2.9.in create mode 100644 outputs/Lab2.1.out create mode 100644 outputs/Lab2.10.out create mode 100644 outputs/Lab2.2.out create mode 100644 outputs/Lab2.3.out create mode 100644 outputs/Lab2.4.out create mode 100644 outputs/Lab2.5.out create mode 100644 outputs/Lab2.6.out create mode 100644 outputs/Lab2.7.out create mode 100644 outputs/Lab2.8.out create mode 100644 outputs/Lab2.9.out diff --git a/Lab2.java b/Lab2.java new file mode 100644 index 0000000..390ecaf --- /dev/null +++ b/Lab2.java @@ -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(); + } +} diff --git a/Makefile b/Makefile index 71e4083..1fe54ab 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,12 @@ CLASSES := $(wildcard *.java) default: classes -Lab1: +lab1: java Lab1 +lab2: + java Lab2 + classes: $(CLASSES:.java=.class) %.class : %.java diff --git a/Queue.java b/Queue.java new file mode 100644 index 0000000..b2c28e4 --- /dev/null +++ b/Queue.java @@ -0,0 +1,119 @@ +/** + * 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 Wei Tsang + * @version CS2030S AY21/22 Semester 2 + */ +class Queue { + /** An array to store the items in the queue. */ + private Object[] 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; + this.items = new Object[size]; + 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(Object 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 Object deq() { + if (this.isEmpty()) { + return null; + } + Object 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 + "]"; + } +} diff --git a/inputs/Lab2.1.in b/inputs/Lab2.1.in new file mode 100644 index 0000000..631c2d5 --- /dev/null +++ b/inputs/Lab2.1.in @@ -0,0 +1,4 @@ +3 1 2 +1.0 1.0 +3.0 1.0 +5.0 1.0 diff --git a/inputs/Lab2.10.in b/inputs/Lab2.10.in new file mode 100644 index 0000000..6c1a21e --- /dev/null +++ b/inputs/Lab2.10.in @@ -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 diff --git a/inputs/Lab2.2.in b/inputs/Lab2.2.in new file mode 100644 index 0000000..b2f8444 --- /dev/null +++ b/inputs/Lab2.2.in @@ -0,0 +1,4 @@ +3 1 2 +1.1 2.0 +2.2 2.0 +3.3 2.0 diff --git a/inputs/Lab2.3.in b/inputs/Lab2.3.in new file mode 100644 index 0000000..2530a7f --- /dev/null +++ b/inputs/Lab2.3.in @@ -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 diff --git a/inputs/Lab2.4.in b/inputs/Lab2.4.in new file mode 100644 index 0000000..10cce57 --- /dev/null +++ b/inputs/Lab2.4.in @@ -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 diff --git a/inputs/Lab2.5.in b/inputs/Lab2.5.in new file mode 100644 index 0000000..0b6da3f --- /dev/null +++ b/inputs/Lab2.5.in @@ -0,0 +1,5 @@ +4 2 1 +1.0 1.0 +1.1 1.0 +2.2 1.0 +2.3 1.0 diff --git a/inputs/Lab2.6.in b/inputs/Lab2.6.in new file mode 100644 index 0000000..87fcdf3 --- /dev/null +++ b/inputs/Lab2.6.in @@ -0,0 +1,5 @@ +4 2 2 +1.0 1.0 +1.1 1.0 +2.2 1.0 +2.3 1.0 diff --git a/inputs/Lab2.7.in b/inputs/Lab2.7.in new file mode 100644 index 0000000..2fa03d0 --- /dev/null +++ b/inputs/Lab2.7.in @@ -0,0 +1,4 @@ +3 2 1 +1.0 4 +2.1 1 +4.2 1 diff --git a/inputs/Lab2.8.in b/inputs/Lab2.8.in new file mode 100644 index 0000000..eeae789 --- /dev/null +++ b/inputs/Lab2.8.in @@ -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 diff --git a/inputs/Lab2.9.in b/inputs/Lab2.9.in new file mode 100644 index 0000000..a85206c --- /dev/null +++ b/inputs/Lab2.9.in @@ -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 diff --git a/outputs/Lab2.1.out b/outputs/Lab2.1.out new file mode 100644 index 0000000..fb7a655 --- /dev/null +++ b/outputs/Lab2.1.out @@ -0,0 +1,12 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +2.000: C0 service done (by S0) +2.000: C0 departed +3.000: C1 arrived [ ] +3.000: C1 service begin (by S0) +4.000: C1 service done (by S0) +4.000: C1 departed +5.000: C2 arrived [ ] +5.000: C2 service begin (by S0) +6.000: C2 service done (by S0) +6.000: C2 departed diff --git a/outputs/Lab2.10.out b/outputs/Lab2.10.out new file mode 100644 index 0000000..7a4ed18 --- /dev/null +++ b/outputs/Lab2.10.out @@ -0,0 +1,20 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +1.200: C1 arrived [ ] +1.200: C1 service begin (by S1) +1.400: C2 arrived [ ] +1.400: C2 joined queue [ ] +1.600: C3 arrived [ C2 ] +1.600: C3 joined queue [ C2 ] +2.100: C4 arrived [ C2 C3 ] +2.100: C4 departed +2.200: C1 service done (by S1) +2.200: C1 departed +2.200: C2 service begin (by S1) +2.500: C0 service done (by S0) +2.500: C0 departed +2.500: C3 service begin (by S0) +3.200: C2 service done (by S1) +3.200: C2 departed +3.500: C3 service done (by S0) +3.500: C3 departed diff --git a/outputs/Lab2.2.out b/outputs/Lab2.2.out new file mode 100644 index 0000000..c6b9ccc --- /dev/null +++ b/outputs/Lab2.2.out @@ -0,0 +1,14 @@ +1.100: C0 arrived [ ] +1.100: C0 service begin (by S0) +2.200: C1 arrived [ ] +2.200: C1 joined queue [ ] +3.100: C0 service done (by S0) +3.100: C0 departed +3.100: C1 service begin (by S0) +3.300: C2 arrived [ ] +3.300: C2 joined queue [ ] +5.100: C1 service done (by S0) +5.100: C1 departed +5.100: C2 service begin (by S0) +7.100: C2 service done (by S0) +7.100: C2 departed diff --git a/outputs/Lab2.3.out b/outputs/Lab2.3.out new file mode 100644 index 0000000..f103cc9 --- /dev/null +++ b/outputs/Lab2.3.out @@ -0,0 +1,23 @@ +1.100: C0 arrived [ ] +1.100: C0 service begin (by S0) +1.200: C1 arrived [ ] +1.200: C1 joined queue [ ] +1.300: C2 arrived [ C1 ] +1.300: C2 joined queue [ C1 ] +1.400: C3 arrived [ C1 C2 ] +1.400: C3 departed +3.100: C0 service done (by S0) +3.100: C0 departed +3.100: C1 service begin (by S0) +4.000: C4 arrived [ C2 ] +4.000: C4 joined queue [ C2 ] +5.000: C5 arrived [ C2 C4 ] +5.000: C5 departed +5.100: C1 service done (by S0) +5.100: C1 departed +5.100: C2 service begin (by S0) +7.100: C2 service done (by S0) +7.100: C2 departed +7.100: C4 service begin (by S0) +9.100: C4 service done (by S0) +9.100: C4 departed diff --git a/outputs/Lab2.4.out b/outputs/Lab2.4.out new file mode 100644 index 0000000..ce7640a --- /dev/null +++ b/outputs/Lab2.4.out @@ -0,0 +1,26 @@ +1.100: C0 arrived [ ] +1.100: C0 service begin (by S0) +1.200: C1 arrived [ ] +1.200: C1 joined queue [ ] +1.300: C2 arrived [ C1 ] +1.300: C2 joined queue [ C1 ] +1.400: C3 arrived [ C1 C2 ] +1.400: C3 joined queue [ C1 C2 ] +3.100: C0 service done (by S0) +3.100: C0 departed +3.100: C1 service begin (by S0) +4.000: C4 arrived [ C2 C3 ] +4.000: C4 joined queue [ C2 C3 ] +5.000: C5 arrived [ C2 C3 C4 ] +5.000: C5 departed +5.100: C1 service done (by S0) +5.100: C1 departed +5.100: C2 service begin (by S0) +7.100: C2 service done (by S0) +7.100: C2 departed +7.100: C3 service begin (by S0) +9.100: C3 service done (by S0) +9.100: C3 departed +9.100: C4 service begin (by S0) +11.100: C4 service done (by S0) +11.100: C4 departed diff --git a/outputs/Lab2.5.out b/outputs/Lab2.5.out new file mode 100644 index 0000000..ac4cd58 --- /dev/null +++ b/outputs/Lab2.5.out @@ -0,0 +1,16 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +1.100: C1 arrived [ ] +1.100: C1 service begin (by S1) +2.000: C0 service done (by S0) +2.000: C0 departed +2.100: C1 service done (by S1) +2.100: C1 departed +2.200: C2 arrived [ ] +2.200: C2 service begin (by S0) +2.300: C3 arrived [ ] +2.300: C3 service begin (by S1) +3.200: C2 service done (by S0) +3.200: C2 departed +3.300: C3 service done (by S1) +3.300: C3 departed diff --git a/outputs/Lab2.6.out b/outputs/Lab2.6.out new file mode 100644 index 0000000..ac4cd58 --- /dev/null +++ b/outputs/Lab2.6.out @@ -0,0 +1,16 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +1.100: C1 arrived [ ] +1.100: C1 service begin (by S1) +2.000: C0 service done (by S0) +2.000: C0 departed +2.100: C1 service done (by S1) +2.100: C1 departed +2.200: C2 arrived [ ] +2.200: C2 service begin (by S0) +2.300: C3 arrived [ ] +2.300: C3 service begin (by S1) +3.200: C2 service done (by S0) +3.200: C2 departed +3.300: C3 service done (by S1) +3.300: C3 departed diff --git a/outputs/Lab2.7.out b/outputs/Lab2.7.out new file mode 100644 index 0000000..5185a39 --- /dev/null +++ b/outputs/Lab2.7.out @@ -0,0 +1,12 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +2.100: C1 arrived [ ] +2.100: C1 service begin (by S1) +3.100: C1 service done (by S1) +3.100: C1 departed +4.200: C2 arrived [ ] +4.200: C2 service begin (by S1) +5.000: C0 service done (by S0) +5.000: C0 departed +5.200: C2 service done (by S1) +5.200: C2 departed diff --git a/outputs/Lab2.8.out b/outputs/Lab2.8.out new file mode 100644 index 0000000..31788e4 --- /dev/null +++ b/outputs/Lab2.8.out @@ -0,0 +1,20 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +1.200: C1 arrived [ ] +1.200: C1 service begin (by S1) +1.400: C2 arrived [ ] +1.400: C2 joined queue [ ] +1.600: C3 arrived [ C2 ] +1.600: C3 departed +2.000: C0 service done (by S0) +2.000: C0 departed +2.000: C2 service begin (by S0) +2.100: C4 arrived [ ] +2.100: C4 joined queue [ ] +2.200: C1 service done (by S1) +2.200: C1 departed +2.200: C4 service begin (by S1) +3.000: C2 service done (by S0) +3.000: C2 departed +3.200: C4 service done (by S1) +3.200: C4 departed diff --git a/outputs/Lab2.9.out b/outputs/Lab2.9.out new file mode 100644 index 0000000..482cdb7 --- /dev/null +++ b/outputs/Lab2.9.out @@ -0,0 +1,23 @@ +1.000: C0 arrived [ ] +1.000: C0 service begin (by S0) +1.200: C1 arrived [ ] +1.200: C1 service begin (by S1) +1.400: C2 arrived [ ] +1.400: C2 joined queue [ ] +1.600: C3 arrived [ C2 ] +1.600: C3 joined queue [ C2 ] +2.000: C0 service done (by S0) +2.000: C0 departed +2.000: C2 service begin (by S0) +2.100: C4 arrived [ C3 ] +2.100: C4 joined queue [ C3 ] +2.200: C1 service done (by S1) +2.200: C1 departed +2.200: C3 service begin (by S1) +3.000: C2 service done (by S0) +3.000: C2 departed +3.000: C4 service begin (by S0) +3.200: C3 service done (by S1) +3.200: C3 departed +4.000: C4 service done (by S0) +4.000: C4 departed