# CS 112 Homework 7 Due on Wednesday 11/3/2004 by 12:00 a.m.

### Part I: Queue class

Develop a Java class, a Java application and a Java applet to implement and test a queue data structure (for integers).
The Queue data structure has the basic queue operations In and Out, Full and Empty, plus a function Print for testing and Draw for visualizing. The queue implementation is circular, and the allocated size is EXACTLY 3 (to allow for easy testing). IN and OUT should print error messages when the queue is full (resp. empty).

The (application) testdriver should be menu-based, with one option for each of the five operations (IN, OUT, EMPTY, FULL and PRINT).

The applet should have buttons for all the above operations. In addition, it should correctly visualize the queue by representing (drawing) only the part of the allocated structure that is currently occupied.

### Part II. Halloween: waiting in line for pumpkins!!!

As an application of the Queue data structure, you will develop an applet to simulate a "real-life" situation where waiting in line is needed.

After graduation, Meggie Smith decides to start her own on-line business, Pumpkins.com. She has to decide whether she will need to hire any additional help during the busy October period. She is advised to build a computer model for her business, and - using simulation techniques - to infer whether she is likely to be able to handle the orders on her own, or not.
Your task is to build the computer model for Meggie. More precisely, you will design a Java applet simulating a business day for the pumpkin business, which works roughly like this:
• The customer orders arrive by email at random times. Each order specifies a pumpkin size. To simplify the matters, assume there are only a finite number of possible sizes. You will simulate this situation by using a random number generator for the arrival time and one for the desired pumpkin size (this is described in detail below).
• The orders are automatically placed in a queue, waiting for processing.
• An order of size k is processed in k units of time, and the orders are processed in the order in which they were received.

#### Technical specifications

• The applet should allow the user to play with the simulation parameters, by choosing:
• the average time between the arrival of two orders, in units of time, e.g. 5 units.
• a range for the possible pumpkin sizes, e.g. 2-10.
There should be default values in the corresponding fields, so the user doesn't have to explicitly enter them.
• The applet should have three buttons: Start, to start the simulation, Step, to advance it by one step, and Stop to stop it.
• When Start is pressed, the simulation starts. Its activity is visualized by drawing the queue of Pumpkin orders. This can be drawn as either a queue of integers (representing the pumpkin sizes), or, better (Extra credit) by drawing Pumpkins of respective sizes.
• There is an implicit timer, which is set in motion when the Start button is pressed, advanced when Step is pressed and stopped when the Stop button is pressed. The timer is (internally) an integer variable. You will also have to visualize the value of the timer on the applet, so that the user can see how many simulation steps have passed.
• At each time step, a random 0-1 value is generated. If 0, no order arrives in that time unit. If 1, an order has arrived. A second number generator is then invoked to generate the size of the ordered pumpkin. Then, a Pumpkin object is created and placed in the queue.
We will discuss in class what is the appropriate code to use for generating meaningful 0-1 arrival values.
• At each time step, the program checks whether the current pumpkin order is in the works. If there is no current pumpkin order, it takes one from the queue and makes it the current order. Then, at each time step, it decreases by one the time left for its processing. When that number reaches 0, the pumpkin order has been finished, and a new one can be processed, if there is one. Your applet should also visualize the time left for the current order (e.g. by printing the value of the internal variable on the applet).
• Each time an order arrives, a variable (of received orders) is incremented (and displayed).
• Each time an order was completed, a variable (of completed orders) is incremented (and displayed).
• Each time an order arrives, a time stamp is associated to it. When the order is removed from the queue, the waiting time for the order is recorded, and added to a total waiting time, which is displayed.
• When the Stop button is pressed, the statistics gathered during the simulation are displayed:
• Average waiting time: the total waiting time (of completed orders) divided by the number of completed orders.
• Unprocessed orders: the number of orders remaining in the queue, their total waiting time and their total remaining processing time.
• Assume that each pumpkin sold produces an income proportional to its size. At the end of the day (i.e. when the Stop button was pressed), compute the total income for the day, as well as the lost income due to unprocessed orders.

You must test your applet on several values of the simulation parameters, and, in each case, try to interpret the results: shall you hire some additional help? What would be a reasonable range of pumpkin sizes that your business could handle without additional help, and what would be the income it will generate? Be ready to demonstrate the homework in class, together with its usage and the interpretation of the data.

For extra credit, you may implement additional features. For instance, a realistic assumption for a real store, where customers arrive and wait in line to be served, is that a customer will leave if not served after a certain number of time units. Another realistic assumption is that the line cannot grow too big (the queue should be of a fixed size): a customer will leave if the line is too long (and the store will lose business, hence income, if this happens).

#### Grading guidelines (for the TA)

• Part I: compile and test on the following sequence: IN 1, Print, IN 2, Print, IN 3, Print, OUT, Print, IN 4, Print, IN 5, Print, OUT, Print, OUT, Print, IN 6, Print, OUT, Print, OUT, Print, OUT, Print, OUT, Print
• Part II: test the applet and comment on the availability and correct functioning of the required features.

Ileana Streinu