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) {...}}
add
(in an ordered list) and
addAfter
(in an unordered list)
Cloneable
and Serializable
ArrayList
(in L&C) and java.util.ArrayList
Iterable
and Iterator
SledDog
objects occurred. Describe (in English) what code I should add to
what methods and classes.
moveOneDisk
method modify these data structures?
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( );
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));
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?");
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));
// 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( ));
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;}}
public static int twotoThe (int n) {
if (n <= 0) return 1;
return twoToThe (n-1) + twoToThe (n-1);}
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;}
// 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;}
In Lecture #25 we mentioned Counting Sort, another algoirthm to sort an array that takes O(n2) 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.
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 2k 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 2k
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