HOMEWORK 1

QUESTIONS

  1. Let list1 and list2 contain arbitrary number of items. Describe briefly what the following code snippet does:
  2. Suppose the design requirement for the following code is to remove every other item from a given list.
    1. If colors initially contains ["violet", "indigo", "blue", "green", "yellow", "orange", "red"], then trace the code on this input by showing the value of i, value of colors.size() and contents of colors for each execution of the for loop body. To receive full credit, you should start by showing the initial values before the loop execution begins and then show results after each execution of the for loop, until you show the final results. Use this format for your answer: i = unInitialized, colors.size() = ..., colors = [...] and then i = 0, ...
    2. Does the code work as per the design requirement? Explain your answer briefly.


  3. Assume the following:
    • The Iterator<E> interface is as discussed in lecture.
    • The ArrayList<E> class implements the Iterable<E> and ListADT<E> interfaces.
    • The items in a list can be compared with the equals method or the compareTo method.
    Complete the Java method specified below, making use of iterators Hint: You would need a lot of them!. Assume this method is implemented in a main class. To receive full credit, your solution:
    • must explicitly use iterators for traversing lists
    • must not use the contains method
    • must not modify the contents of the parameter

SUBMISSION