CS210: Intermediate Computing/Data Structures (Java)

Lab #5

Purpose

This lab gives you experience developing a class that implements a queue using a circular array.  In this lab, you will ignore the possibility of needing to expand capacity.  The test case code provided does not test your code for its behavior in that situation.

Pre-Lab Assignment

Prior to the lab session, do the following to get started:

Download, save, and unzip the file Lab5.zip on your removable media (floppy disk, Zip disk, or memory stick) creating a directory named Lab5.

Open the Dr Java Application on your PC. 

Create a new Dr Java project in the Lab4 directory by selecting the menu Project -> New… naming the project file Lab5 and saving it in your new Lab5 directory.  (Dr Java automatically opens an Untitled file in the editor window which you will ignore.)

Using the menu File-> Open, open the files: CircularArrayQueue.java, EmptyCollectionException.java, QueueADT.java, and QueueTest.java.  Dr Java will automatically include this source file in the Project.  Use the menu Project -> Save to save the updated project file. 

Study the code for the CircularArrayQueue class so that you understand what it needs to do.  This code is not complete.

Lab Activities

You need to design and add code to the methods of the CircularArrayQueue class in places as indicated by the comments:

1.       In the constructor(int initialCapacity), you need to instantiate the array and initialize the attributes: front, rear, and count.

2.       In the enqueue method, you need to add an element to the array in the correct place based on the state of the circular array.

3.       In the dequeue method, you need to return an alias of the reference to the first element in the queue and remove it.

4.       In the first method, you need to return an alias of the reference to the first element in the queue.  

Using the menu Project -> Compile Project, compile the program.  Test your code by selecting the TestQueue.java file and using the DrJava Tools -> Test Current Document menu.  This will run four different JUnitTestCases on your implementation of the CircularArrayQueue class.  This will verify that your code: 

1.                               Instantiates a CircularArrayQueue object with the correct initial state. 

2.                               Processes enqueue method calls and leaves the object in the correct state. 

3.                               Processes dequeue method calls and leaves the object in the correct state.

4.                               Handles wraparound of the contents of the queue in the array correctly.

Show your code to the TA before leaving the lab.  When you are through with the lab, close the Project using the menu Project -> Close.  Be sure to remove your media and take it with you.  If you have not finished the code, finish it by the next lab period.  You will need to add to this code in the next lab to handle expand capacity.

Before you leave, have your TA check your code in progress.  If you have not completed it, finish it before the next lab session to include a hard copy with your lab report.

Lab Report

Write a document that answers the following questions.   Your lab must be printed (not handwritten). 

Answer the following questions related to implementing a circular queue in an array.

1.       What is the benefit of using a circular queue relative to implementing a regular queue in an array?

2.       How and where do you use the modulo operator in the implementation?

Note:  You should work alone on the lab report.

Note:  The assignment is due at the BEGINNING of your next lab.  No late assignments will be accepted.  Emailed assignments will not be accepted.  If you are not going to be in lab on the due date, you can turn the assignment ahead of time to the CS210 TA box in the CS department office.