Theme Park Queue - 10 Course Points

The purpose of this assignment is to learn how to implement the methods of a Queue using an array.

Start your assignment early! You need time to understand the assignment and to answer any questions that will arise as you read the description and the code provided.

Refer to our Programming Assignments FAQ for instructions on how to install VSCode, how to use the command line and how to submit your assignments.

  • See this video on how to import the project into VSCode and how to submit into Autolab.

The assignment has one component: Coding (10 points) submitted through Autolab.

Overview

The ThemeParkQueue.java class represents an Array-Based implementation of a simple queue. The class attributes and constructor are provided, along with helper methods.

You must fill in the code for the enqueue() and dequeue() methods, to finish the Queue of Strings and enable the Theme Park Queue Driver visualization.

The ThemeParkQueue class is a queue, implemented using a circular array. It uses indices to track the front and rear of the queue, along with a size counter. This allows you to enqueue strings at the rear of the queue, and dequeue them from the front of the queue.

A circular array implementation is where indices wrap around when they reach the end of the array. When the array becomes full, it automatically resizes to accommodate more elements.

EX:

enqueue(“Rider One”),    enqueue(“Rider Two”),    enqueue(“Rider Three”),    enqueue(“Rider Four”)

 

Then:

dequeue() -> “Rider One”

dequeue() -> “Rider Two”

Then:

enqueue(“Rider Five”),    enqueue(“Rider Six”),    enqueue(“Rider Seven”),    enqueue(“Rider Eight”)

Directions

  • DO NOT add new import statements.
  • DO NOT add any new class attributes
  • DO NOT change any of the method’s signatures.
  • DO NOT modify the given constructor.

To complete this Lab, you must implement the enqueue and dequeue methods for the array-based queue. The method signatures are already given, do not modify these or add any new ones.

Make sure you are working from the correct directory, see this image:

enqueue(String toAdd)

This method adds a new String to the array-based queue. You should always enqueue to the REAR of the queue.

The queue uses a circular array implementation, so when rear reaches the end of the array, it wraps around to index 0. If the array is full, you should call the resize() helper method to double the array size.

To Complete This Method:

  1. Check if the array is full (size == queue.length). If so, call resize().
    1. The resize() method is provided for you
    2. Note: in lecture we resize when HALF full. Here we resize when full for Driver reasons.
  2. Increment rear: rear = (rear + 1) % queue.length
  3. Store the new string at queue[rear]
  4. Increment the size counter

dequeue()

This method removes the String at the front of the queue and returns it.

If the queue is empty (size == 0), return null.

To Complete This Method:

  1. Check if the queue is empty. If so, return null.
  2. Store the string at queue[front] in a temporary variable
  3. Set queue[front] = null 
  4. Increment front: front = (front + 1) % queue.length
  5. Decrement the size counter
  6. Return the stored string

Driver

Once you implement your code, you can run the Driver.java file and interact with the driver. It will show an empty box, with two buttons “Enqueue Rider” and “Dequeue Rider”, as well as a label displaying “Line Length: 0”.

If you click the Enqueue Rider button, a rider will be enqueued onto the line (the front of the line is the left side of the Driver window). You can enqueue as many riders as you’d like. When you click the Dequeue Rider button, the rider all the way to the left will be dequeued.

Note: the driver will not check that you are properly returning the String name when dequeuing.

You can use this Driver to test your code, but it is still useful to write JUnit test cases, in case the driver does not catch all cases.

 

Writing JUnit Tests

You do not have to submit this, this is for you to test your own code.

Since this queue implementation contains public methods, and represents a generalized queue of strings, we can test it independently of the driver using a test class.

In the main project folder, there exists a “test” folder next to the “src” folder. This contains a JUnit test class, which can run and test pieces of your code. To compile and run these tests, you must install the Test Runner extension. See the Programming FAQ for more info on VScode extensions.

The two tests are for enqueue() and dequeue(), and you must fill these tests in. Once you do, remove the fail() statements at the bottom and run. 

You are provided with a premade JUnit test class for ThemeParkQueue. You can finish writing the test methods, by using JUnit test methods (assertTrue(), assertFalse(), assertEquals()) in order to test your code.

Try testing enqueue using the lineLength attribute. Then, test to see if the queue dequeues the strings in the correct order (FIFO).

Tests not running? 

First, make sure you’re in the right folder and the tests are implemented. Next, if you have the “Code Runner for Java” or Oracle “Java” extension, make sure you uninstall those extensions. Remember that you must fill in the tests for enqueue() and dequeue().

VSCode Extensions

You can install VSCode extension packs for Java. Take a look at this tutorial. We suggest:

Importing VSCode Project

  1. Download ThemeParkQueue.zip from Autolab Attachments.
  2. Unzip the file by double-clicking.
  3. Open VSCode
    • Import the folder to a workspace through File > Open Folder

Executing and Debugging

  • You can run your program through VSCode or you can use the Terminal to compile and execute. We suggest running through VSCode because it will give you the option to debug.
  • How to debug your code
  • If you choose the Terminal:
    • first navigate to ThemeParkQueue directory/folder
      • to compile: javac -d bin src/queue/*.java
      • to execute: java -cp bin queue.Driver
      • NOTE: if you have ThemeParkQueue (2) -> ThemeParkQueue or CS112 -> ThemeParkQueue in VS Code, open the INNERMOST ThemeParkQueue through File -> Open Folder.
To run JUnit tests: right click test/QueueTest.java in VS Code and select Run Tests. 

Before submission

COMMENT all printing statements you have written from ThemeParkQueue.java 

Collaboration policy. Read our collaboration policy here.

Submitting the assignment. Submit ThemeParkQueue.java separately via the web submission system called Autolab. To do this, click the Labs and Assignments link from the course website; click the Submit link for that assignment.

Getting help

If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza.

  • Find instructors and Lead TA office hours here
  • Find tutors office hours on Canvas -> Tutoring
  • In addition to office hours we have the Coding and Social Lounge (CSL) , a community space staffed with ilab assistants which are undergraduate students further along the CS major to answer questions.

By Colin Sullivan