Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 8

Laboratory 8 - Simulation: Queues, Arrays, and Preparation for Project 3

Goals:
To understand using queues. To prepare for project 3: A bank simulation using queues, randomization and gathering statistics.
Getting started:
The Steps of the Lab:
  1. Open the SimpleQueue project. You will be creating a Queue class from scratch.
    Do

    Create an empty Queue class by opening the class editor (double-click the Queue class diagram) and create the class shell:

        import java.util.ArrayList;
        
        /**
         * Queue (waiting line).
         *
         * @author <Your Name Goes Here>
         * @version <Today's Date Goes Here>
         */
        public class Queue {
        
        }
                       
    

    Add the private fields: ArrayList entries and int maxLengthEver

    Create the class constructor: It will create an instance of entries and set maxLengthEver to 0. maxLengthEver represents the largest the queue ever becomes during its lifetime.

    Write the methods enqueue and dequeue: Customer objects will be added to and removed from the queue. The customer class here has only one field, a customerID. This is different from the customer object you will use in project 3, which you will see in class. Open the Customer class in an editor window. Take a few minutes to examine its methods before continuing to the next step.

    enqueue()
    is a method with one parameter, newCustomer, of type Customer. Enqueue adds newCustomer to the end of the queue and checks the size of the resulting queue to see if maxLengthEver needs to be updated.
    dequeue()
    is a method with no parameters. It removes the customer from the front of the queue and returns that customer.

    Write the method size: size() simply returns the size of the queue as an int.

    Test

    Now test enqueue and dequeue as follows: Create three Customer objects, one with customerID = 1, another with customerID = 2, and the third with customerID = 3. Next create an instance of your Queue class and envoke enqueue three times, adding a different customer object each time. When the dialog box appears to request the customer to enqueue, just left-click once on the customer you want to add. Check the size of the queue. Now dequeue and enqueue as you wish, checking the size of the queue each time to make sure the number of items in the queue is correct. When you are satisfied that things are working correctly, proceed to the next step.

    Do

    Add the method isEmpty: isEmpty is a boolean method that returns true if the queue has no elements and false otherwise. Test the method before going on to the next step.

    Write two display methods, printStatistics and display:

    printStatistics()
    is a method that displays the number of customers in the queue and the maximum number of customers that were ever in the queue. The display should look like
        Current number of customers in the queue: <the number>
        Maximum number of customers in the queue: <the number>
                           
    
    display()
    is a method that displays, from front to back, the customerID of each customer in the queue. You may simply display one customerID per line.
Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University , Computer Science Department , Spring 2005
Maintained by Brian Howard ( bhoward@depauw.edu ). Last updated