0% found this document useful (0 votes)
8 views2 pages

Seniors First

The document outlines a simulation of a school lunch line where students are served based on their academic class priority: Seniors, Juniors, Sophomores, and Freshmen. It describes the use of a Priority Queue to manage the serving order and includes statistics on wait times and the number of students served for each class. Additionally, it provides hints for modifying the Customer class and implementing the algorithm for the simulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Seniors First

The document outlines a simulation of a school lunch line where students are served based on their academic class priority: Seniors, Juniors, Sophomores, and Freshmen. It describes the use of a Priority Queue to manage the serving order and includes statistics on wait times and the number of students served for each class. Additionally, it provides hints for modifying the Customer class and implementing the algorithm for the simulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Seniors First

A certain school gives privileges to its students according to their


academic class. One privilege is getting served in the lunch line,
So Ju F Se Ju Fr
where students join the queue at random, but get served according r
to the priority of their academic class, in the order Seniors, Juniors,
Sophomores, and Freshmen. A complication is that, if an Se Ju F Ju Fr
underclassman is already getting served, then an arriving Senior r
cannot take cuts but has to wait in the queue. After the underclassman is served and departs, then the
highest priority student rises to the front of the queue. The data structure to model this situation is, of
course, a Priority Queue. What method decides who rises to the front of the priority queue? ____________

Below is a sample run showing who is at the front getting served and the state of the Priority Queue. (Be
aware that the Priority Queue you see is printed in its heap order, which does not display in strict order of
precedence.)

For each of the four academic classes, we will count the number who were served, the longest wait time, and
the average wait time. Here is the output file of one 15-minute simulation:
1 minute 1. Serving null. Priority queue []
2 minute 2. Serving null. Priority queue []
3 minute 3. Serving 4-Se:3. Priority queue [4-Se:3]
4 minute 4. Serving 3-Se:3. Priority queue [3-Se:3, 4-Fr:4]
5 minute 5. Serving 2-Se:3. Priority queue [2-Se:3, 4-Fr:4, 2-So:5]
6 minute 6. Serving 1-Se:3. Priority queue [1-Se:3, 3-So:6, 2-So:5, 4-Fr:4]
7 Depart 0-Se:3, wait time was 4 minutes. Total time so far: 4
8
9 minute 7. Serving 2-Se:7. Priority queue [2-Se:7, 3-So:6, 2-So:5, 4-Fr:4]
10 minute 8. Serving 1-Se:7. Priority queue [1-Se:7, 5-Ju:8, 2-So:5, 4-Fr:4, 3-So:6]
11 Depart 0-Se:7, wait time was 2 minutes. Total time so far: 6
12
13 minute 9. Serving 5-Ju:8. Priority queue [5-Ju:8, 3-So:6, 2-So:5, 4-Fr:4, 3-Fr:9]
14 minute 10. Serving 4-Ju:8. Priority queue [4-Ju:8, 3-So:6, 2-So:5, 4-Fr:4, 3-Fr:9]
15 minute 11. Serving 3-Ju:8. Priority queue [3-Ju:8, 3-So:6, 2-So:5, 4-Fr:4, 3-Fr:9]
16 minute 12. Serving 2-Ju:8. Priority queue [2-Ju:8, 3-So:6, 2-So:5, 4-Fr:4, 3-Fr:9]
17 minute 13. Serving 1-Ju:8. Priority queue [1-Ju:8, 3-So:6, 2-So:5, 4-Fr:4, 3-Fr:9, 5-So:13]
18 Depart 0-Ju:8, wait time was 6 minutes. Total time so far: 6
19
20 minute 14. Serving 2-So:5. Priority queue [2-So:5, 3-So:6, 5-So:13, 4-Fr:4, 3-Fr:9]
21 minute 16. Serving 1-So:5. Priority queue [1-So:5, 3-So:6, 5-So:13, 4-Fr:4, 3-Fr:9]
22 Depart 0-So:5, wait time was 12 minutes. Total time so far: 12
23
24 minute 17. Serving 3-So:6. Priority queue [3-So:6, 4-Fr:4, 5-So:13, 3-Fr:9]
25 minute 18. Serving 2-So:6. Priority queue [2-So:6, 4-Fr:4, 5-So:13, 3-Fr:9]
26 minute 19. Serving 1-So:6. Priority queue [1-So:6, 4-Fr:4, 5-So:13, 3-Fr:9]
27 Depart 0-So:6, wait time was 14 minutes. Total time so far: 26
28
29 minute 20. Serving 5-So:13. Priority queue [5-So:13, 4-Fr:4, 3-Fr:9]
30 minute 21. Serving 4-So:13. Priority queue [4-So:13, 4-Fr:4, 3-Fr:9]
31 minute 22. Serving 3-So:13. Priority queue [3-So:13, 4-Fr:4, 3-Fr:9]
32 minute 23. Serving 2-So:13. Priority queue [2-So:13, 4-Fr:4, 3-Fr:9]
33 minute 24. Serving 1-So:13. Priority queue [1-So:13, 4-Fr:4, 3-Fr:9]
34 Depart 0-So:13, wait time was 12 minutes. Total time so far: 38
35
36 minute 25. Serving 4-Fr:4. Priority queue [4-Fr:4, 3-Fr:9]
37 minute 26. Serving 3-Fr:4. Priority queue [3-Fr:4, 3-Fr:9]
38 minute 27. Serving 2-Fr:4. Priority queue [2-Fr:4, 3-Fr:9]
39 minute 28. Serving 1-Fr:4. Priority queue [1-Fr:4, 3-Fr:9]
40 Depart 0-Fr:4, wait time was 25 minutes. Total time so far: 25
41
42 minute 29. Serving 3-Fr:9. Priority queue [3-Fr:9]
43 minute 30. Serving 2-Fr:9. Priority queue [2-Fr:9]
44 minute 31. Serving 1-Fr:9. Priority queue [1-Fr:9]
45 Depart 0-Fr:9, wait time was 23 minutes. Total time so far: 48
46
47 minute 32. Serving null. Priority queue []
48 Customer Total Longest Average Wait
49 Senior 2 4 3.0
50 Junior 1 6 6.0
51 Sophomor 3 14 12.66
52 Freshman 2 25 24.0
Hints on the Customer Class
You will need to modify the previous Customer class so that it works in a priority queue. How?

1. You will need a new int field in Customer to store its academic class. (Alternatively, some
students make 4 subclasses to model each kind of Customer.) Use 0 for Seniors, 1 for Juniors, 2
for Sophomores, and 3 for Freshman. You will these use integers to make compareTo work
properly.

2. You will need a new boolean field atFront in Customer to store whether this customer is at
the front, getting served. The atFront field will be used in the compareTo method to keep
Seniors from cutting in front of an underclassman who already at the front, being served.

3. The Customer class uses a 1-arg constructor to store the time of arrival. Both the time spent
getting their order (int)(Math.random() * 5 + 2); and the customer’s academic class
(int)(Math.random() * 4); are set at random.

4. The compareTo method in Customer compares this customer to another and returns either a
negative integer, 0, or a positive integer. Remember that in priority queues, the higher priority
returns the lower number. In this lab, the customer at the front has the highest priority, then
Seniors, then Juniors, then Sophomores, and last Freshmen. Finally, if there are two or more
students of the same class in the queue, the one who arrived earliest gets served first.

5. The toString method needs to return the time spent getting served, the academic class, and the
time of arrival. For example, a sophomore who arrived at minute 8 and will spend 6 minutes at the
front getting served displays as 6-So:8

Hints on the Algorithm


6. SeniorsFirst.java is a modification of CustomersInQueue.java in which Queue of
Customer is replaced by a PriorityQueue of Customer.

7. It will be convenient to store the statistics in three parallel arrays of int.


String[] classes = new String[]{"Senior", "Junior", "Sophomor", "Freshman"};
int[] served = new int[]{0,0,0,0};
int[] longestWait = new int[]{0,0,0,0};
int[] totalWait = new int[]{0,0,0,0};

8. The driver prompts the user to enter the duration of the simulation. Customers will enter and join
the priority queue to be served. After the duration is finished, no more customers are allowed in,
but all those already in the priority queue will be served. Then outfile the statistics.

Extension
Make a class MyPriorityQueue which implements a priority queue. It's your choice as to how you
will implement the priority queue.

Test your resource class with SeniorsFirst as the driver. You should not have to make any other
changes in SeniorsFirst.

You might also like