Docsity

CS 61B, Spring 2006, Hilfinger - Java Programming Exercises, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CS 61B, Spring, 2006, Hilfinger
1. [10 points] Indicate the outcome of executing each of the following (one of “compilation error”,
“runtime error”, or whatever is printed). In each case, briefly justify your answer:
Code Result
a.
String s = "Hello ";
String s2 = s; "not equal" – the addition creates a new
s = s + "there"; string, so s and s2 point to different things.
if( s == s2 )
System.out.println ( "equal" );
else
System.out.println( "not equal" );
b.
class Test {
public void greet () { compilation error – cannot call an instance
System.out.println( "Hello!" ); method from a static method
}
public static void main (String[] args ) {
greet();
}
}
c.
class Thing {
int x; "equal" – Thing's equals method is called
public Thing( int x ) { and returns true
this.x = x;
}
public boolean equals( Object p ) {
return x == ((Thing) p).x;
}
public static void main(String[] args ) {
Object p1 = new Thing(3),
p2 = new Thing(3);
if( p1.equals(p2) )
System.out.println("equal");
else
System.out.println("not equal");
}
}
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 61B, Spring 2006, Hilfinger - Java Programming Exercises and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 61B, Spring, 2006, Hilfinger

  1. [10 points] Indicate the outcome of executing each of the following (one of “compilation error”, “runtime error”, or whatever is printed). In each case, briefly justify your answer: Code Result a. String s = "Hello "; String s2 = s; "not equal" – the addition creates a new s = s + "there"; string, so s and s2 point to different things. if( s == s2 ) System.out.println ( "equal" ); else System.out.println( "not equal" ); b. class Test { public void greet () { compilation error – cannot call an instance System.out.println( "Hello!" ); method from a static method } public static void main (String[] args ) { greet(); } } c. class Thing { int x; "equal" – Thing's equals method is called public Thing( int x ) { and returns true this.x = x; } public boolean equals( Object p ) { return x == ((Thing) p).x; } public static void main(String[] args ) { Object p1 = new Thing(3), p2 = new Thing(3); if( p1.equals(p2) ) System.out.println("equal"); else System.out.println("not equal"); } }

Code Result d. class Thing { int x; "equal" – the equals method returns true public Thing( int x ) { this.x = x; } public boolean equals( Object p ) { return x == ((Thing) p).x; } public static void main(String[] args) { Thing p1 = new Thing(3), p2 = new Thing(3); if( p1.equals( p2 )) System.out.println("equal"); else System.out.println("not equal"); } } e. class Thing { int x; runtime error – cannot cast "(3)" to Thing public Thing( int x ) { in equals method. this.x = x; } public boolean equals( Object p ) { return x == ((Thing) p).x; } public static void main(String[] args) { Thing p1 = new Thing(3); if( p1.equals( "(3)" )) System.out.println("equal"); else System.out.println("not equal"); } }

  1. [10 points] The class Powerset represents a power set : the set of all subsets of some given set of items (initially empty). The add method adds an item to the given set. The iterator method returns an I terator<Object[]> - that is, an Iterator whose next method yields an array containing the items in one of the subsets ( a unique one each time, in some unspecified order). For example, in Powerset S = new Powerset(); S.add("Bob"); S.add("Alice"); for( Iterator<Object[]> i = S.iterator(); S.hasNext();) Do something with i.next(); i.next() takes on the Object[] values '{ }', '{ "Bob" }', '{ "Alice" }', '{ "Alice", "Bob" }' (not necessarily in that order). a. Fill in the skeleton below and on the next page to get this effect. Hint: If there are n elements in a set S, then there is a one-to-one relationship between n-bit integers and the subsets of S. Specifically, the integer b 0 b 1 ...^ bn − 1 (the bi are bits) corresponds to the subset that contains the kth element of S iff bk = 1. So for our previous example, 00 corresponds to {}, 01 might correspond to {Bob}, 10 to {Alice}, and 11 to {Alice, Bob}. public class Powerset { /** The powerset of the empty set / public PowerSet() { // FILL THIS IN elements = new List(); } /* Add X to the current given set. Throws an exception * if the size of the given set exceeds 32. / public void add( Object x ) { //FILL THIS IN if( elements.size() >= 32 ) throw next Exception( "Powerset full" ); elements.add( x); } /* An iterator over all subsets of the given set. The * remove method throws UnsupportedOperationException. */ public Iterator<Object[]> iterator () { //FILL THIS IN return new SetIterator(elements); } More space on the next page.

    //ADD ANY PRIVATE INSTANCE METHODS, VARIABLES, AND NESTED CLASSES

    //YOU NEED HERE

    private List elements; private static class SetIterator extends Iterator { public SetIterator( List elements ) { lst = elements; current = 0; } List lst; unsigned int current; public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public boolean hasNext() { return current >= ( 1 << lst.size() ); } public Object[] next() { ArrayList ret = new ArrayList(); int temp = current; int count = 0; wihle( temp != 0 ) { if( temp &1 == 1 ) ret.add( lst.get(current) ); count +=1; temp = temp >>> 1; } current += 1; return ret.toArray(); } }

    c. Assuming that next and hasNext on Powerset iterators take constant time, what is the worst-case time for subsetSum (Be as precise as possible). n2n −^1  n where n is ints.size()

    1. [1 point] Where or what is Sark? Sark is one of the Channel Islands.
    2. [10 points] Using the following class definition: class IntList { // 'final' means head can't be changed after the constructor // sets it. public final int head; public IntList tail; public IntList( int head, IntList tail ) { this.head = head; this.tail = tail; } } fill in the methods on below and on the next page to agree with their comments. Define any additional methods you'd like. /* a. / /* Move the first occurrence of the maximum item in L to the end of
      • the list, maintaining the order of all other items and returning
      • the modified list. The operation is destructive and creates no new
      • IntList elements. E.g., if L is { 4, 3, 9, 2, 9, 8 }, returns
      • { 4, 3, 2, 9, 8, 9 } */ static IntList moveMaxToEnd( IntList L ) { // FILL THIS IN IntList max = L.findMax(); if( max.next == null ) return L; if( L == max ) { IntList h = max.tail; L.findTail().next = max; max.next = null; return h; } else { IntList p = L; while( p.next != max ) { p = p.next; } p.next = max.next; L.findTail.next =max; max.next = null; return L; } } Continues on next page.

    /* b. / /* A list that is identical to L, except that the first

    • occurrence of the maximum element is remove to the end.
    • Nondestructive: has no effect on the original list L. */ static IntList maxToEnd( IntList l ) { // FILL THIS IN return moveMaxToEnd( L.copy() ); } //ADDITIONAL METHODS MAY GO HERE private IntList findMax() { if( tail == null ) return this; IntList rest = findMax( next ); if( rest != null && rest.head > head ) return rest; else return this; } private IntList findTail() { IntList p = this; while( p.next != null ) { p = p.next; } return p; } private IntList copy() { if( tail == null ) { return new IntList( head, null ); } else { return new IntList( head, tail.copy() ); } }