feat: add lab2 params

This commit is contained in:
Yadunand Prem 2022-09-01 10:13:37 +08:00
parent 638c644326
commit ed77b0e10f
23 changed files with 385 additions and 1 deletions

26
Lab2.java Normal file
View 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();
}
}

View File

@ -2,9 +2,12 @@ CLASSES := $(wildcard *.java)
default: classes
Lab1:
lab1:
java Lab1
lab2:
java Lab2
classes: $(CLASSES:.java=.class)
%.class : %.java

119
Queue.java Normal file
View File

@ -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 + "]";
}
}

4
inputs/Lab2.1.in Normal file
View File

@ -0,0 +1,4 @@
3 1 2
1.0 1.0
3.0 1.0
5.0 1.0

6
inputs/Lab2.10.in Normal file
View 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
inputs/Lab2.2.in Normal file
View File

@ -0,0 +1,4 @@
3 1 2
1.1 2.0
2.2 2.0
3.3 2.0

7
inputs/Lab2.3.in Normal file
View 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
inputs/Lab2.4.in Normal file
View 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
inputs/Lab2.5.in Normal file
View 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
inputs/Lab2.6.in Normal file
View 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
inputs/Lab2.7.in Normal file
View File

@ -0,0 +1,4 @@
3 2 1
1.0 4
2.1 1
4.2 1

6
inputs/Lab2.8.in Normal file
View 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
inputs/Lab2.9.in Normal file
View 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

12
outputs/Lab2.1.out Normal file
View File

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

20
outputs/Lab2.10.out Normal file
View File

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

14
outputs/Lab2.2.out Normal file
View File

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

23
outputs/Lab2.3.out Normal file
View File

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

26
outputs/Lab2.4.out Normal file
View File

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

16
outputs/Lab2.5.out Normal file
View File

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

16
outputs/Lab2.6.out Normal file
View File

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

12
outputs/Lab2.7.out Normal file
View File

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

20
outputs/Lab2.8.out Normal file
View File

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

23
outputs/Lab2.9.out Normal file
View File

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