Queue Problems

  1. Write a client that will let you add Strings to the Queue, delete from the Queue or print who is next in line. The client should be menu driven.

    The delaration can be :
    private Queue<String> myQueue= new LinkedList<String>();

  2. Write a new Queue class that adds the feature of printing the queue. Declare a local Queue (private to the class). After you print the queue will be empty.

    You can declare the Queue with :
    private Queue<String> myQueue= new LinkedList<String>();

    add member function
    public void print()


    public void add(String s)

  3. Add a member function that returns the count of the elements in the queue. To do this you must declare a local queue and as you process the original queue, add them to the new queue. When you are done you can set your original queue reference to the newly formed queue.

  4. Using your new count function write a member function print2 that will print out the elements in the queue leaving the elements in the queue undisturbed.

  5. Write a function to copy q1 to q2 and leave q1 unchanged. Use the queue member functions to access the queues. Return q2 to the client. (You should use the count function you wrote above).

  6. Write a function to count and return the number of integers in a queue which are less than zero. Leave the queue unchanged.

  7. Assume you have two queues q1 and q2. Write a function called match that will count how many elements in the same position of the two queues are equal. The queues are of the same length. Do not modify the queues.

  8. Write a function that will reverse a queue. You should use a local ArrayList to help you.
    public void reverseQ(Queue q)

  9. Do the same function above using no other data structure to help you.

  10. Write a function that will add a Random Integer to your queue on the average of every 4 minutes (for every minute generate a random int between 1 and 4 - if it is 1 then add a random Integer to your queue).. Every 4th minute remove a number from a queue (if there is any number there).
    At the end of 120 minutes how many numbers are in your queue.
    Run the simulation 100 times and record the results.
    What is the average of numbers in the queue?

  11. The Car Wash

    The owners of a car wash want to know the average wait time for cars in their car wash. It take 4 minutes to wash a car and on the average a car arrives every 4 minutes.

    To simulate this :

    A car object has a wait time field (int).

    For every minute of the day (8 hour work day), there is a 25% chance a car will arrive.

    Hints to Solve Problem

    1. To see if a car arrives generate a random number between 1 and 4 and if it is 1 then assume a new car had arrived.
    2. If a car arrives and another car is being washed then put that car into the queue. You will have to keep track of how long it remains in queue. You will also have to keep track of how long the current car is being washed. When a car is finished being washed then increment your car count. Also add on to your total waiting for all cars the waiting time for that processed car (the car may have had a waiting time of 0).
    3. For every minute of the simulation see if a new car arrived. Increment waiting times of all cars in queue if there are any. Increment wash time of current car (if there is one).
    4. As an end result we want to see what the average waiting time is for all cars.
    5. Any cars left in the queue when time runs out will not be processed.

      Create a CarWash class. It should have one public function - getTime. It should receive how long it takes to wash a car and what the average time for a new car to arrive.
      The CarWash class should use a Queue of Car objects.
      private LinkedList myQueue= new LinkedList(); // use LinkedList because we have to keep processing through the Queue // changing waiting time for each car // could have recorded time when entered and then processed time // as we got out and used a real queue

      The call from the client would be :

      CarWash c = new CarWash(4,4);
      System.out.println("Average wait was "+c.getTime());

    6. Simulate the problem above 1000 times and see what the average waiting time was for a car.

    7. The Car Wash put an advertisement in the paper. The cars are arriving every 3 minutes. Using the simulation for 10000 times what is the average wait for a car now?

    8. It is a rainy day. Cars are arriving every 6 minutes. What is the average wait?