Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS1027

LAB 7

Computer Science Fundamentals II

Learning Outcomes

•    Implement missing methods in a queue made with a Linked List structure

•    Use the queue collection to track cyclical movement patterns

•    Consider and compare the implications of using different implementations of queues

•    Observe the results of a simulated snail race!

Pre-Lab

•    Create a new Java project called Lab7

•    Download the file lab7Files.zip and extract these files: QueueADT.java,

LinkedQueue.java, LinearNode.java, EmptyCollectionException.java, TestQueue.java, Snail.java, SnailRace.java, and AnswersLab7.txt.

•    Save these downloaded files into the Lab7 src folder

Exercise 1 - Completing the Linked Queue class

1.   Open LinkedQueue.java and TestQueue.java and examine the code in both classes.

2.   Note that LinkedQueue is not complete; all its methods (other than the constructor) are

left empty. Without adding any additional instance variables or methods, complete the six empty methods given the following instructions:

•    enqueue(T element) - Create a new LinearNode storing the given parameter element. Link this node to the rear of the queue. If needed, update frontrear,

and count.

•    dequeue() - If the queue is empty, throw an EmptyCollectionException. If not, remove the element at the front of the queue and then return it. Remember to update the front pointer when this element is being removed and if this is the only

item then update rear as well. Decrement count to indicate this removed item.

•    first() - If the queue is empty, throw an EmptyCollectionException. If not,  retrieve the element at the front of the queue and return it without removing it.

•    isEmpty() - Return true if there are no elements in the queue, and false otherwise.

•    size() - Return the number of elements in the queue.

3.   Run the TestQueue file. This will test that the LinkedQueue methods were properly

implemented. If any of the test fails, use the debugger to fix the corresponding methods in LinkedQueue until all the tests pass. Do not add print statements in TestQueue or

alter this file in any way.

4.  Answer the questions for Exercise 1 in AnswersLab7.txt.

Exercise 2 - Completing the Snail class

In this exercise you will be simulating a snail race using queues that will contain the snails' movement patterns.

1.   Open Snail.java and SnailRace.java and examine the code. Note that the methods in

Snail.java are empty. This class is used for representing each of the snails in the race. 2.   Complete the constructor:

•    initialize the snail's position to 0

•    initialize the snail's queue movePattern and add to this queue each one of the

values stored in the array pattern passed as parameter to the constructor; the

values of the array must be stored in the queue in the same order in which they appear in the array, i.e., first enqueue pattern[0], then enqueue pattern[1], and so on.

3.   Complete the move() method:

•    dequeue the first number from movePattern and store it in a variable called step

•    enqueue step in movePattern, so this number can be used later again

•    increment the snail's position by the value of step, but do not let the snail’s

position go past the finish line; compare the snail’s position with the race length (this is stored in variable raceLength in the SnailRace class; examine how the   variable was declared to determine how you can access it) and if the snail’s

position is larger than raceLength, change the position to raceLength. 4.   Complete the getPosition() method to simply return the snail’s position   5.   Complete the display() method:

•    create two int variables called dashesBefore and dashesAfter. The value of

dashesBefore represents the distance that the snail has moved (so its value is

equal to the snail’s position). The value of dashes after represents the distance to the end of the race (so what should its value be? What is the difference between  raceLength and the snail’s position?)

•    use method System.out.print() (not Syste.out.println()) to print as many dashes (“-“) as the value of dashesBefore

•    then, display the snail's shell by printing the string “@”

•    print as many dashes as the value of dashesAfter (all on one line still, so use System.out.print() again)

•    finally, print out a newline character so the next snail's path will be on a different line than this one (System.out.print(“\n”)). The output produced by this method    might look like this:

----------@--------------------------------------

6.  When you have completed the Snail.java methods, go into SnailRace.java and run it.

Watch the live race in the console (you may need to resize the console to clearly watch the race unfold)

7.   Experiment with different movement patterns for the snails and you can even add more snails to the race. This is all done in the SnailRace constructor.

8.  Answer the questions for Exercise 2 in AnswersLab7.txt.

Screenshot of a simulated snail race about halfway through the race:

----------@--------------------------------------

------------------@------------------------------

--------------@---------------------------------- 

Submission

When you have completed the lab, navigate to the weekly module page on OWL and click the

Lab link (where you found this document). Make sure you are in the page for the correct lab.

Upload the files listed below and remember to hit Save and Submit. Check that your submission went through and look for an automatic OWL email to verify that it was submitted successfully.

Rules

•    Please only submit the files specified below. Do not attach other files even if they were part of the lab.

•    Do not ZIP or use any other form of compressed file for your files. Attach them individually.

•    Submit the lab on time. Late submissions will receive a penalty.

•    Forgetting to hit "Submit" is not a valid excuse for submitting late.

•    Submitting the files in an incorrect submission page will receive a penalty.

•    You may re-submit code if your previous submission was not complete or correct, however, re-submissions after the regular lab deadline will receive a penalty.

Files to submit

•    LinkedQueue.java

•    Snail.java

•    AnswersLab7.txt