- Answer the problems on the exam pages.
- There are seven problems for 100 total points. Likely scale is A=88, C=64.
- If you need extra space use the back of a page.
- No books, notes, calculators, or collaboration.

Q1: 10 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 10 points Q7: 20 points Total: 100 points

Here is a code base from previous assignments, that is assumed to be available throughout the exam:

```
public class Dog {
private String name;
private int age;
// public get and set methods, two-parameter constructor
}
public class SledDog extends Dog {
private String breed = "Husky";
// public get and set methods, three-parameter constructor
}
public class LinearNode<T> {
private T element;
private LinearNode<T> next;
// public get and set methods, zero-parameter and one-parameter constructors
}
public class DogTeam {
private LinearNode<SledDog> leadNode;
private int size;
// public get and set methods, zero-parameter constructor
public void addToLead (SledDog newLead) {…}
public SledDog removeLead ( ) {…}
public void switchLastTwo ( ) {…}
public SledDog removeYoungest ( ) {…}
public int countHuskies ( ) {…}
}
Code run before other code fragments:
Dog ace = new Dog ("Ace", 6);
Dog biscuit = new Dog ("Biscuit", 1);
Dog cardie = new Dog ("Cardie", 3);
Dog duncan = new Dog ("Duncan", 1);
SledDog balto = new SledDog ("Balto", 92, "Husky");
SledDog king = new SledDog ("King", 73, "Husky");
SledDog buck = newSledDog ("Buck", 108, "Mixed");
Some useful API information for L&C's code base:
public interface ListADT<T> extends Iterable<T> {
public T removeFirst( );
public T removeLast( );
public T remove (T element);
public T first( );
public T last( );
public boolean contains (T target);
public boolean isEmpty( );
public int size( );
public Iterator<T> iterator( );
public String toString( );}
public interface OrderedListADT<T> extends ListADT<T> {
public void add (T element);}
public interface UnorderedListADT<T> {
public void addToFront (T element);
public void addToRear (T element);
public void addAfter (T element, T target);}
// Note: For this exam is is assumed that the class ArrayList implements UnorderedListADT
public class SortingandSearching {
public static <T extends Comparable<? super T >> boolean
linearSearch (T [ ] data, int min, int max, T target) {...}
public static <T extends Comparable<? super T >> boolean
binarySearch (T [ ] data, int min, int max, T target) {...}
public static <T extends Comparable<? super T >> void
selectionSort (T [ ] data) {...}
public static <T extends Comparable<? super T >> void
quickSort (T [ ] data, int min, int max) {...}
public static <T extends Comparable<? super T >> void
mergeSort (T [ ] data, int min, int max) {...}}
```

**Question 1: Java Concepts**Briefly explain the difference between the two concepts in each pair (2 points each).- a) selection sort and insertion sort
- b)
`add`

(in an ordered list) and`addAfter`

(in an unordered list) - c)
`Cloneable`

and`Serializable`

- d)
`ArrayList`

(in L&C) and`java.util.ArrayList`

- e)
`Iterable`

and`Iterator`

**Question 2: Software Engineering**-- Briefly discuss how you would make the following modifications to the code for specified programs, with specific reference to the code (5 points each):- a) Suppose I want to know, in the course of a test of a solution
to Project #6, how many total comparisons of
`SledDog`

objects occurred. Describe (in English) what code I should add to what methods and classes. - b) Modify a solution to Project #5 to change the "tax rate" on spoons brought into a town from 5% (rounded up) to 10% (rounded up).
- c) Suppose that in a simulation of the Towers of Hanoi
problem, I want to represent each disk by an object and keep track,
through the simulation, of which disks are on which pins. What new
data structures should I use, and how would the
`moveOneDisk`

method modify these data structures?

- a) Suppose I want to know, in the course of a test of a solution
to Project #6, how many total comparisons of
**Question 3: Tracing Code**-- indicate the output of each code fragment (5 points each)- a)
`UnorderedListADT<Dog> pack = new ArrayList<Dog>( ); pack.addToFront (duncan); pack.addAfter (cardie, duncan); pack.addToRear (biscuit); Dog x = pack.remove (duncan); Dog y = pack.removeLast( ); pack.addToFront (x); pack.addAfter (y, cardie); System.out.println (pack.last( ).getName( );`

- b)
`public static int fred (int n) { if (n == 0) return 1; else return ginger (n) + fred (n-1);} public static int ginger (int n) { if (n == 0) return 0; else return ginger (n-1) + fred (n-1);} System.out.println (fred(3));`

- c)
`public static int netCaps (String input) { if (input.equals ("")) return 0; char first = input.charAt(0); // first letter String rest = input.substring(1); // all but first letter if ('A' <= first && first <= 'Z') return netCaps (rest) + 1; return netCaps (rest) - 1;} System.out.println (netCaps ("i Can HaZ chEEzburGer?");`

- a)
**Question 4: Finding Errors**-- indicate whether the code fails to compile, throws an exception on reasonable input, or has clearly unintended output (5 points each):- a)
`public int abbott (int n) { if (n == 0) return n; else return abbott (n-1) + costello (n);} public int costello (int n) { if (n == 0) return 1; else return abbott (n) + costello (n-1);} System.out.println (abbott (2) + costello (3));`

- b)
`// uses L&C code base, usual dogs are defined as above ArrayList<Dog< kennel = new ArrayList<Dog>( ); kennel.addToFront (cardie); DogTeam redSox = new DogTeam( ); redSox.addToLead (balto); kennel.addAfter (king, cardie); kennel.removeFirst ( ); redSox.addToLead (kennel.removeFirst( )); if (kennel.isEmpty( )) System.out.println ("No more dogs"); else System.out.println (kennel.first( ).getName( ));`

- c)
`public class Group<T extends Comparable<T>> { private T[ ] members; private int size; public Group (int capacity) { members = new T[capacity]; size = 0;} public T getMember (int i) {return members[i];} public void setMember (int i, T newElem) { if (members[i] == null && newElem != null) size++; if (members[i] != null && newElem == null) size--; members[i] = newElem;} // more methods public int compareTo (Group other) { if (this.size < other.size) return -1; if (this.size == other.size) return 0; return 1;}}`

- a)
**Question 5: Timing Analysis**Indicate the big-O worst case running time of each method or code fragment in terms of n. Include a brief justification of your answer. In each case we use L&C's code base. (5 points each)- a)
`public static int twotoThe (int n) { if (n <= 0) return 1; return twoToThe (n-1) + twoToThe (n-1);}`

- b)
`public static int foo (int n) { int x = 0; y = n; for (int i = 0; i < n; i++) { y = y/2; if (y <= 1) break; for (int j = 0; j < n; j++) x += (y * i + j);} return x;}`

- c)
`// uses L&C's binarySearch method, goes in L&C's SortingAndSearching class // "n" is the combined length of the two input arrays public static <T extends Comparable <? super T>> boolean sameElements (T [ ] one, T [ ] two) { boolean soFar = true; for (int i = 0; i < one.length; i++) if (!binarySearch (two, 0, two.length - 1; one[i])) soFar = false; for (int j = 0; j < two.length; j++) if (!binarySearch (one, 0, one.length - 1; two[j])) soFar = false; return soFar;}`

- a)
**Question 6: Short Code Writing**(10 points)In Lecture #25 we mentioned Counting Sort, another algoirthm to sort an array that takes O(n

^{2}) time. The idea is that if the n elements of the array are distinct, we can compare each element x to the other n - 1 elements in the array, and thus determine exactly how many elements in the array are less than x.Write a generic method

`public static <T extends Comparable<? super T>> T [ ] countingSort (T [ ] input)`

that returns an array that is a sorted copy of the array input. Your method should not change the input array at all. The declaration in angle brackets allows you to assume that

`T`

has a`compareTo`

that we use to define the proper order on its elements.**Question 7: Long Code Writing**(20 points)A

**subteam**of a`DogTeam`

object x is a`DogTeam`

that contains a subset of the dogs in x, in the same order as they occur in x. For example, if x contains dogs a, b, and c, there are eight subteams of x: the empty team (written [ ]), [a], [b], [c], [a, b], [a, c], [b, c], and [a, b, c].The following is a recursive definition of the set of subteams of x. If x has no dogs in it, there is exactly one subteam which also has no dogs in it. Otherwise, let d be the lead dog of x and let y be the team made from x by removing d. Then the subteams of x consist of the subteams of y, plus every team that is made from a subteam of y by adding the dog d to it at the lead.

Write a recursive method

`public DogTeam[ ] listSubteams( )`

to be included in the`DogTeam`

class. If this method is called by a`DogTeam`

object with k dogs in it, it should return an array consisting of all the 2^{k}possible subteams of x. You are not required to list the subteams in any particular order, but ther is an order that is most natural for the recursive definition. Assume that you have a static method`int twoToThe (int k)`

that will return the number 2^{k}when given input k.Make sure that your method has no side effects on the calling

`DogTeam`

object.If you find it convenient, you may assume that you have the

`iterator`

method from Project #6, and the corresponding iterator class, available to use in your code.

Last modified 19 November 2011