CS367 Homework 1
Lecture 3, Fall 2015
Due by 11:59 pm on Friday, September 18 (not accepted late)

Announcements

Check here periodically.

9/8/2015  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: https:piazza.com/wisc/fall2015/cs3673 and sign-in using your wisc.edu account.

Questions

Homework assignments must be done individually. Collaboration on homework assignments is not allowed.

Question 1:

Consider the following Java method that is intended to swap two items in a ListADT instance.

void swap(int i, int j, ListADT<String> myList) {
    String temp = myList.get(i);
    myList.add(i, myList.get(j));
    myList.add(j, temp); 
}

For example, if myList is ["a", "b", "c", "d", "e", "f"], then swap(1, 4, myList) should change myList into ["a", "e", "c", "d", "b", "f"]. However, the method doesn't work as intended.

Part A: Trace the code on the example above by showing the contents of myList after each line of the swap method is executed.

Part B: Rewrite the swap method so that is behaves as described. You must leave the method signature unchanged. Your method must not return a new list; it must operate on the list instance passed in the myList parameter. You do not need to worry about input validation: you may assume that i and j are valid indices, that i < j, and that myList is not null.

Question 2:

Assume that ArrayList implements ListADT, that the ArrayListand ArrayListIterator classes are implemented as expected, and that BadListException is an unchecked exception with a zero-argument constructor. Assume also that null elements may not be added to a list.

Complete the Java method specified below, making use of iterators. In order to receive full credit, your solution:

  • must explicitly use iterators for traversing lists (i.e., you may not use a for-loop or Java's extended-for loop),
  • must not use the contains method,
  • may use any ListADT methods (except contains) described in the on-line reading, including ListADT.iterator(), but must not use any other List methods not mentioned there, and
  • must not modify the contents of the parameters.
public static ListADT<String> union(ListADT<String> list1, ListADT<String> list2) {
// If list1 or list2 (or both list1 and list2) is null, throw a BadListException. 
// If list1 and list2 are both empty, return a new empty list.
// If list1 is empty (but not list2), return a new list containing the strings in
//     list2 with all duplicates removed.
// If list2 is empty (but not list1), return a new list containing the strings in
//     list1 with all duplicates removed.
// Otherwise, create and return a new list that contains the strings in list1 and
//     the strings in list2 with all duplicates removed.
//
// Examples:
//  list1: "a","b","c"          list2: "d","e","f"      result: "a","b","c","d","e","f"
//  list1: "a","c","b","d"      list2: "e","d","a","f"  result: "a","c","b","d","e","f"
//  list1: "a","b","c","b","a"  list2: "c","a","b"      result: "a","b","c"
//
// Note: the list returned does not need to be in any particular order

You are encouraged to test your solution (e.g., using Eclipse). While testing, it may be useful to replace ListADT with List (the interface in Java's java.util package); however, make sure that your solution only uses the methods that are in the ListADT interface described in the on-line reading (including an iterator method, as mentioned above).

Handing in

Please include your name at the top your file.

Put your answers to both questions into one file named Homework1 with the appropriate file extension, e.g., Homework1.pdf (see File Format for acceptable file formats).

Electronically submit your work to the Homework 1 Dropbox on Learn@UW.

Last Updated: 9/21/2015     © 2015 Beck Hasti and Charles Fischer