CSci 2101 Lab 7. June 9th.

Due Thursday, June 10th at 11:59pm

30 points

The lab is done in pairs, as assigned.

Use Eclipse in this lab. If you run into any problems or would like to know more about it, please ask.

Task 1: finish the OurLinkedList method

Test your method written in the previous lab with the updated OurLinkedList class. Note that your methods must properly update the size variable. Make sure that your methods follow the specification precisely (that includes throwing appropriate exceptions when needed). Correct them if necessary. Submit your methods together with your testing code.

Task 2: Queue implementation (code reuse)

Write a class OurQueueImplementation that implements the OurQueue interface. Create a file in Eclipse, make your class implement the interface, and then use Eclipse QuickFix to add unimplemented methods. Make sure that element and remove return a type E and the iterator returns Iterable<E>.

Then write testing code for the queue in a file TestOurQueue. Write tests for all methods (including a for-each loop that tests the iterator), then start filling in the code. We will talk in class about the easiest way of implementting a queue.

Carefully follow method description. Make sure that exceptions are thrown as appropriate. Use try/catch blocks to check for exceptions.

Task 3: exploring Java Collections hierarchy.

In this problem you will study standard Java interface List and some classes that implement it:

For simplicity all these classes should be used with Integer type parameter.

Question 1. Draw the type hierarchy for the three classes and the List interface. Note that in type hierachy diagrams a supertype is on top, a subtype is below.

Question 2. Write a program that creates instances of each of the three classes with Integer type parameter (call them arrayList, stack, and vector, respectively) and adds some elements to them. Add a variable list of type List<Integer>. Then try the following and explain your answers; use your hierarchy diagram and the description of the classes. If something doesn't compile, comment it out and explain (in comments) why it doesn't compile.

  1. Try the following assignment statement: list = stack. Print out the resulting list using a for-each loop, what would be printed? Can you call a pop method on the list variable? Can you call it on the stack variable? After the call to the pop method on the stack print out the contents of the list again and explain what happened.
  2. Try the following assignment statement: stack = list. Does it work? Would it work with the typecasting, i.e.
    stack = (Stack<Integer>) list
    Would you be able to call pop on the resulting stack? Print out the stack and the list, explain your answers.
  3. Now try the assignment list = arrayList. Can you call ArrayList methods, such as trimToSize(), on the list? Can you then assign the list to the stack variable with typecasting, as you did in the previous question -- will the code compile, and if yes, will it run?
  4. Try assignments vector = stack and stack = vector. Which ones work? Which ones work with typecasting? Which methods can you call on the result? Explain your answers.
  5. Feel free to experiment more with the Java collections.

How to submit

Submit the java file(s) with your testing code by e-mail to me. The subject of the message must be 2101 Lab 7. Make sure to CC your group partner.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.