Lab 9: Queues
Due Date and Submission Requirements
- Due Date: Tuesday, April 1st at 11:59 p.m.
- Partner Information: This is an individual assignment. You are allowed to collaborate with other students, but each student must submit their individual, independent solution.
- Submission Instructions: You will submit your CarQueue.java and Car.Java files to the appropriate D2L dropbox
- If you are in Andras's section, please also copy and paste your code into the D2L submission text box
The goal of this lab is:
- Gain experience using a Queue data structure
- Implement a Queue in Java using a linked list
Directions
You will be writing a Java program that simulates cars using a Car Wash station. Using Lab9Demo.java as a starting point, you will define the CarQueue class and fill in the missing methods for the queue data structure.
Since you are writing a queue that represents a Car wash, each car object will have:
- The make/model of the car which is a String (for example "Nissan Rogue")
- The owner of the car, which is a String (for example "Sasha")
- Whether or not they are a VIP car wash member, which is a boolean (true/false). Car washes only cost $6.00 for VIP members, and $10.00 for non-VIP members
You will need to define the Car class, and add any necessary methods. At the very least, you will need to override the toString() method so that it returns information about the customer/car. The string that should be returned should follow the format: <Car Type> (<Car Owner>)
Next, you will define the CarQueue class, which will be your queue. You should use a LinkedList. The queue has a finite size, and that size is specified as an argument to the CarQueue constructor. Your queue will need to keep track of the current size of the queue and the capacity of the queue.
You will also need to keep track of the number of cars serviced by the car wash, and how much money the car wash has made.
There is a special priority that this car wash must follow. VIP customers are allowed to cut in front of non-VIP customers, but they cannot cut in front of other VIP members. Please see the "hints" section below to understand how to implement this priority. This queue is a hybrid of a priority queue and a vanilla queue.
You are NOT allowed to modify the Lab9Demo class. After creating the CarQueue class, you must define the following methods:
- enqueue(Car newCar)- The enqueue method adds a new car to the back of the queue. If the queue is full, an "Error" message should be printed out, and the car should not be added to the queue. Remember that if a VIP member is enqueued, they should be placed ahead of normal customers, but behind other VIP customers.
- dequeue()- the dequeue removes and prints out the car at the front of this queue. This method doesn't need to return anything. When a car is dequeued, that means that car has been "serviced". This should increase the number of cars serviced variables by 1, and increase the total earning by $6 (if they are a VIP) or by $10 (if they are not a VIP)
- peek()- the peek() method returns the car that is currently at the front of the queue
- isEmpty()- This method returns true if the queue is empty, false if the queue is not empty
- printQueue()- Prints out all the cars that are currently in the queue. You should print the queue out as a numbered list, where the first thing printed out is the car in the front of the queue, and the last thing printed out is the last thing in the queue (see sample output)
- printStats() - This method prints out how many cars the car wash has serviced, and the total earnings (remember these should be instance fields of the CarQueue class).
Starting Code
Output
When you run your program, your output should look exactly like this screenshot
Restrictions
You CANNOT import java.util.Queue; (i know this is silly)
Hints
Implementing the priority logic for handling VIP customers is probably the trickiest part. I strongly encourage you to follow this hint:
For your CarQueue class, consider having two queues (two LinkedList), one for VIP customers (vip_queue), and one for normal Customers (normal_queue). When adding a new car, check their VIP status. If they are a VIP, add them to the back of vip_queue. If they are a normal customer, add them to the back of normal_queue.
When dequeueing and peeking, always prioritize the vip_queue. If vip_queue is empty, then use the element at the front of the normal_queue.
When printing the queue, you first print out the vip_queue (if it's not empty), and then print out the normal_queue (if it's not empty)
Grading (10 points)
- 2 points- enqueue() is correct
- 2 points- dequeue() is correct
- 1 point- peek() is correct
- 1 point- isEmpty() is correct
- 1 point- printQueue() is correct
- 1 point- printStats() is correct
- 1 point- The Car class is correctly defined
- 1 point- The CarQueue class is correctly defined and uses a linked list or array