| CIT 594 Midterm Spring 2005, Dave Matuszek |
(Answer
Key)
|
node. Assume
you have methods getLeftChild() and getRightChild().
I've started it for you:
int countNodes(BinaryTree node) {
if (node == null) return 0;
return 1 + countNodes(node.getLeftChild( )) + countNodes(node.getRightChild( ));
}
|
n = number of nodes n to mean the depth of the tree
in question #1 (that is, how far it is from node to its deepest descendant),
and we assume that the tree is balanced, what is the time required (using
Big-O notation) to execute the method in question #1?Vector has methods int size() and
int capacity(). What is the difference?
size() returns:capacity() returns:LinkedList implements Iterator. Write a
loop to print all the elements of a LinkedList named myList.
Iterator iter = myList.iterator( ); // using a while loop
while (iter.hasNext( )) {
System.out.println(iter.next( ));
}
|
// using a for loop
for (Iterator iter = myList.iterator( ); iter.hasNext( ); ) {
System.out.println(iter.next( ));
}
|
for (Object o : myList) { // using Java 5's enhanced for loop
System.out.println(o);
}
|
T or F):
Stack has both empty()
and isEmpty() methods. equals(Object)
you must also override hashCode().hashCode() you
must also override equals(Object).Observer you
must have a class that extends Observable.Observable you
must have a class that implements Observer.String parameters can
be passed to an applet.| Do the base cases first. |
| Only recur with a simpler case. |
| Don't modify global variables or objects. |
| Don't look down. |
containsAll(Collection c) |
containsAll --> subset
|
union |
addAll(Collection c) |
addAll --> union
|
intersection |
removeAll(Collection c) |
removeAll --> difference
|
difference |
retainAll(Collection c) |
retainAll -> intersection
|
member |
|
|
contains -> member |
|
try {
leafB2.setLeftChild(rootAB);
fail();
} catch (IllegalArgumentException e) {
System.out.println("This call would form a loop.");
}
|
Because test methods that succeed shouldn't produce any output. |
Collection interface, other than the
ones mentioned in the previous question. (Use the form
boolean add(Object o) void clear( ) boolean equals(Object o) int hashCode( ) boolean isEmpty( ) |
Iterator iterator( ) boolean remove(Object o) int size( ) Object[] toArray( ) Object[] toArray(Object[] a) |
Map interface. (Use the form
void clear( ) boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet( ) boolean equals(Object o) Object get(Object key) int hashCode( ) |
boolean isEmpty( ) Set keySet( ) Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size( ) Collection values( ) |
x and y are the same type.
public class Pair {
|
public class Pair<X, Y> {
|
myPair with components of type Integer
and Double, and assign some reasonable value to it.
for (int i = 0; i < myArray.length; i++) {
myArray[i] = 0;
// loop invariant: For all j <= i, myArray[j] == 0
}
|
log2 100 is between 6 (next
lower integer) and 7 (next higher integer).HashMap without writing
your own hashCode() method.Add. Add should have a factory method makeAdd(),
no other methods, and no instance variables. Write the complete class
definition.
public class Add {
static Add add = new Add( );
private Add( ) { };
public static Add makeAdd( ) {
return add;
}
|
0xdigits
or 0Xdigits, where digits is one
or more hexadecimal digits (case insensitive). Write a BNF definition (simple
or extended, your choice) for a hex number; do not worry about limiting
the number of digits.boolean hexDigit()
method.
|
boolean hexNumber( ) {
// There are many possible variations on this method;
// I've graded it mostly on whether the logic is correct
|