CH 02
CH 02
Describe the effect of each of the following operations on object myList as shown at the bottom of Figure 2.2.
What is the value of myList.size() after each operation?
myList.add("Pokey");
myList.add("Campy");
int i = myList.indexOf("Happy");
myList.set(i, "Bouncy");
myList.remove(myList.size() - 2);
String temp = myList.get(1);
myList.set(1, temp.toUpperCase());
start size 6
myList.add("Pokey");
adds element to end of list, size now at 7
myList.add("Campy");
adds element to end of list, size now 8
int i = myList.indexOf("Happy");
returns integer 4 to i
myList.set(i, "Bouncy");
sets element i (4) to "Bouncy". Size = 8
myList.remove(myList.size() - 2);
Removes item 6 ("Pokey"). Size = 7
String temp = (String) myList.get(1);
Stores "Awful" in temp. Size = 7
myList.set(1, temp.toUpperCase());
Sets item 1 to "AWFUL", replacing "Awful".
Final size = 7
2.2.1
2.3.1
2.4.1
Determine how many times the output statement is displayed in each of the following fragments. Indicate
whether the algorithm is O(n) or O(n2).
a. for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
System.out.println(i + " " + j);
b. for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
System.out.println(i + " " + j);
c. for (int i = 0; i < n; i++)
for (int j = n - 1; j >= i; j--)
System.out.println(i + " " + j);
d. for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (j % i == 0)
System.out.println(i + " " + j);
a. n2 O(n2)
b. 2n O(n)
c. n(n-1)/2 O(n2)
d. n-1 O(n)
2.4.3
How does the performance grow as n goes from 2000 to 4000 for the following? Answer the same question
as n goes from 4000 to 8000. Provide tables similar to Table 2.4.
a. O(log n)
b. O(n)
c. O(n log n)
d. O(n2)
e. O(n3)
2.5.1
2.5.3
Since a reference to the tail of the list is available, the add method is constant or O(1).
2.5.5
For the single-linked list in Figure 2.16, data field head (type Node) references the first node. Explain the effect
of each statement in the following fragments.
a. head = new Node<String>("Shakira", head.next);
b. Node<String> nodeRef = head.next;
nodeRef.next = nodeRef.next.next;
c. Node<String> nodeRef = head;
while (nodeRef.next != null)
nodeRef = nodeRef.next;
nodeRef.next = new Node<String>("Tamika");
d. Node<String> nodeRef = head;
while (nodeRef != null && !nodeRef.data.equals("Harry"))
nodeRef = nodeRef.next;
if (nodeRef != null) {
nodeRef.data = "Sally";
nodeRef.next = new Node<String>("Harry", nodeRef.next.next);
}
2.6.1
a. Each node in a single-linked list, has a reference to the data and the next node.
b. In a double-linked list, each node has a reference to the data, the next node, and the previous node.
c. To remove an item from a single-linked list, you need a reference to the previous node.
d. To remove an item from a double-linked list, you need a reference to the node.
2.7.1
The method indexOf, part of the List interface, returns the index of the first occurrence of an object in a
List. What does the following code fragment do?
int indexOfSam = myList.indexOf("Sam");
ListIterator<String> iteratorToSam = listIterator(indexOfSam);
iteratorToSam.previous();
iteratorToSam.remove();
where the internal nodes of myList (type LinkedList<String>) are shown in the figure below:
2.7.3
In Question 1, what if we omit the statement
iteratorToSam.previous();
2.8.1
2.8.3
What happens if we call remove after we call add? What does the Java API documentation say? What does our
implementation do?
2.9.1
Look at the AbstractCollection definition in the Java API documentation. What methods are abstract?
Could we use the KWArrayList and extend the AbstractCollection, but not the AbstractList, to develop an
implementation of the Collection interface? How about using the KWLinkedList and the AbstractCollection,
but not the AbstractSequentialList?
Only two methods, iterator and size, are abstract in the AbstractCollection class. Our
KWArrayList class does not implement the iterator method, so it does not meet the
Collection interface. Our KWLinkedList class implements the iterator and size methods,
thus, by extending AbstractCollection it fully implements the Collection interface. Our
KWLinkedList class also can extend AbstractSequentialList to implement the
Collection interface. By doing so, it also fully implements the List interface.
2.10.1
Why don’t we implement the OrderedList by extending LinkedList? What would happen if someone called
the add method? How about the set method?
By extending the LinkedList class we expose all of the public methods of the LinkedList class. If
either the add or set method were called the invariant that the items were ordered could be violated.
2.10.3
Why don’t we provide a listIterator method for the OrderedList class?
2.11.1
Explain why a method that does not match its declaration in the interface would not be discovered during
white-box testing.
Methods which do not match their declaration in the interface are detected by the Java compiler, and
thus would never get to white-box testing.
2.11.3
List two boundary conditions that should be checked when testing method readInt below.
minN == maxN (e.g. minN = maxN = 1), and test for input 1 and 2
minN > maxN (e.g. minN = 5 and maxN = 2)