Q1: 10 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 10 points Q7: 20 points Total: 100 points
Some typos fixed on 12 November 2011.
Format is similar to first midterm, some text explaining the question types is omitted.
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 LinearDog<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);}
public class SortingandSearching <T> {
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) {...}}
iterator
and Iterator
remove( )
and remove(elem)
(both in
a java.util PriorityQueue
)
java.util.AbstractList
and java.util.List
DogTeam
class, we have a method
removeYoungest
that removes and returns a
SledDog
that is no older than any other
SledDog
in the calling team. Describe (in English) how
I might use this method to sort a DogTeam
object's dogs
by
age, with the youngest dog at the lead.
// using L&C's code base
String [ ] dogs = {"Cardie", "Ginger", "Ace", "Fred", "Duncan", "Ethel", "Biscuit"};
SortingandSearching.mergeSort (dogs, 0, 2);
SortingandSearching.quickSort (dogs, 2, 4);
SortingandSearching.mergeSort (dogs, 4, 6);
SortingandSearching.quickSort (dogs, 0, 2);
SortingandSearching.mergeSort (dogs, 2, 4);
SortingandSearching.quickSort (dogs, 4, 6);
for (int i = 0; i < 7; i++)
System.out.println (dogs[i]);
public static String louis (int n) {
if (n == 0) return "x";
else if (n % 2 == 0) return "z" + louis (n - 1) + "z";
else return "y" + louis (n - 1) + "y";}
System.out.println (louis (4));
public static boolean fannyLikes (String input) {
if (input.length( ) <= 1) return false;
char first = input.charAt(0); // first letter
String rest = input.substring(1); // all but first letter
if (first == rest.charAt(0)) return true;
return fannyLikes (rest);}
String [ ] test = {"dogs", "puppies", "cats", "kittens", "horses",
"colts", "fillies"};
for (int i = 0; i < 7; i++)
System.out.println (fannyLikes (test[i]);
public class Pack implements Comparable<Pack> {
private Dog [ ] members;
public Pack (int size) {members = new Dog[size];}
// lots of other methods to add and remove dogs, etc.
public boolean compareTo (Pack other) {
if (this.members.length < other.members.length) return true;
else return false;}}
public static boolean isOK (String w) {
if (w.length( ) == 0) return false;
char first = w.charAt(0); // first letter
String rest = w.substring(1); // rest of string
if (first == rest.charAt(0)) return true;
else return isOK (rest + first);}
// using L&C's code base, assume usual dogs are defined
UnorderedListADT<Dog> kennel = new Collection<Dog>( );
kennel.addToFront (ace);
kennel.addAfter (duncan, ace);
kennel.remove (ace);
kennel.removeFirst( );
try {System.out.println (kennel.first( ));}
catch (EmptyCollectionException e) { }
public static int twoToThe (int n) {
if (n <= 0) return 1;
return 2 * twoToThe (n - 1);}
public static int bar (int n) {
int counter = 0;
for (int i = 0; i < n; i++)
counter += n;
if (counter == n * n) return counter;
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
counter--;
return counter;}
// method to be added to L&C's SortingandSearching class
// n is the combined size of the two arrays
public static <T extends Comparable<? super T>> boolean
sameElements (T [ ] one; T [ ] two) {
boolean soFar = true, foundYet = false;
for (int i = 0; i < one.length; i++) {
foundYet = false;
for (int j = 0; j < two.length; j++)
if (one[i].compareTo (two[j]) == 0) foundYet = true;
soFar = soFar && foundYet;}
for (int i = 0; i < two.length; i++) {
foundYet = false;
for (int j = 0; j < one.length; j++)
if (two[i].compareTo (one[j]) == 0) foundYet = true;
soFar = soFar && foundYet;}
return soFar;}
Following a definition that L&C give later in Chapter 11, here is a generic interface defining a priority queue:
public interface PriorityQueueADT<T> {
public void addElement (T object, int pri);
// places a new entry in the PQ with contents "object" and priority "pri"
public T removeNext ( );
// removes and returns the contents of the
// entry with lowest priority in the PQ
}
Write a generic class ListPQ<T>
that implements
PriorityQueueADT<T>
. Each
ListPQ<T>
object should have an
ArrayList
(from L&C's code base) of
PQEntry<T>
objects as an instance field. A
PQEntry<T>
object has two instance fields, its
contents of type T
and its priority of type
int
.
Define the class PQEntry<T>
as well and give it
a compareTo
method so that you can use the
ArrayList
to keep the nodes in the natural order, and use
the
ListADT
and OrderedListADT
methods to
implement the addElement
and removeNext
methods.
Both classes you write should have appropriate constructors.
In this question we will implement the "dishonest guessing game"
mentioned
in lecture. Write a class public class GuessingGame
that
will have one constructor with one int
parameter. When
we construct a GuessingGame
object with a positive
parameter n and run its play
method, it should first say "I am
thinking of a number in the range from 1 through (n). Can you guess
it in (k) tries?". Here "(n)" means to substitute the parameter, and
"(k)" means to substitute the largest number such that the player is
guaranteed to lose against dishonest play. (This number is the
largest
k such that 2k ≤ n.)
Assume that a method private int getGuess( )
has been
written for you. It returns an int
supplied by the
player -- don't worry about any exceptions it might throw. The game's
job is to (1) answer "too low" or "too high" to every guess, (2)
remember the largest number it has said to be too low, and the
smallest number it has said to be too high, and (3) when the player
has exhausted her guesses, supply the "number it was thinking of",
which must be consistent with all the answers it has given. Of
course, the way it does this is to choose its "too high" or "too low"
answer each time so as to leave it the largest possible interval in
which its number might be.
The game should say "Bad guess!" if the guess cannot be correct based on the answers it has already given. (But such a guess counts against the player's total.)
Last modified 12 November 2011