CSC148H lab -- week 4


Here are the instructions for the week 4 CSC148H lab. To earn your lab mark, you must actively participate in the lab. As always, you will work as a pair, taking turns being the driver and the navigator. The driver types, and the navigator watches for mistakes, thinking ahead. At the end of each exercise, show your TA what you've done, and then switch roles.

This week's exercises are on the topic of queues and linked lists.

Getting started

When it's your first turn as driver, begin by making a new folder "lab4". (It's OK to use just one partner's account, to save retyping completed work.) In lab4, save the files that we've provided for you: ListNode.java and Queue.java.

Advice on casting

You probably won't need to cast values from one class type to another, but in case you do, we offer this reminder: casting is a very low-priority operation, so you generally need to parenthesize it. For example, if you want to call method meth() on object obj, treating obj as if it were an instance of the class Blah, then you should not say this:

               x = (Blah) obj.meth(); // WRONG -- casts the result

Instead, you should say this:

               x = ((Blah) obj).meth(); // casts obj before calling meth()

Exercise 1: Starting class LinkedQueue

  1. Open Queue.java and ListNode.java in DrJava.
  2. Write a new class LinkedQueue that implements the Queue interface.
  3. Write method headers for any necessary methods, and compile. Do not write anything in the method bodies. Just write the headers and empty braces.
  4. Try to compile. Any method in LinkedQueue that has a non-void return type won't compile, because it doesn't have a return type.
  5. In the method bodies write stub code: just enough code to get it to compile. Use one of these two statements:

                   return null;
                   return -1;
    

Exercise 2: Writing methods enqueue and dequeue

Exercise 3: Completing class LinkedQueue

  1. Write methods head and size for your class LinkedQueue.
  2. Write method toString for your class LinkedQueue. This method should return a String representation of the contents of the queue, with a dash (" -- ") between the elements.

    For example, if the queue contains a String "Hi!" followed by an Integer with value 25 followed by another String "blah", then method toString should return the String "Hi! -- 25 -- blah" (of course, the quotation marks " are not part of the String that you return).

    Note that if the queue contains only one element (for example, a String "alone"), then method toString should return this element by itself ("alone") and if the queue is empty, then method toString should return the empty String ("").

    Remember that you can get a String representation of any object by calling the toString method on that object.

  3. Make sure that you talk with your partner about what you are doing, and draw pictures of the work the method must accomplish so that both of you understand what is going on.

Exercise 4: Testing class LinkedQueue

This part is more open-ended than the other ones. When you're finished, show your TA what you've done.

  1. Write a new JUnit test class QueueTester. Remember that you can create a new JUnit test case in DrJava under "File-->New JUnit Test Case".
  2. Write a test method for each Queue method: enqueue, dequeue, head, and size. Talk with your partner to figure out how to test each method. Don't forget about the "boundary cases" (such as an empty queue or a one-element queue).
  3. The toString method can help with testing, especially for enqueue. Remember to use the assert methods: assertEquals, assertTrue, and assertFalse.
  4. If you discover bugs in your implementation of class LinkedQueue, try to fix them. (You may find it useful to trace your code by hand to see where it may be going wrong.)