Q1: 20 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 20 points Q7: 20 points Total: 120 points
Format is similar to the midterms, 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 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 StackADT<T> {
public void push (T element);
public T pop ( );
public t peek ( );
public boolean isEmpty( );
public int size ( );
public String toString( );
public interface QueueADT<T> {
public void enqueue (T element);
public T dequeue ( );
public T first ( );
public boolean isEmpty( );
public int size ( );
public String toString ( );
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) {...}}
public interface BinaryTreeADT<T> {
public T getRoot ( );
public boolean isEmpty( );
public int size( );
public boolean contains (T target);
public T find (T target);
public String toString( );
public Iterator<T> iteratorPreOrder( );
public Iterator<T> iteratorPostOrder( );
public Iterator<T> iteratorInOrder( );
public Iterator<T> iteratorLevelOrder( );}
public interface SetADT<T> extends Iterable<T> {
public void add (T element);
public T removeRandom ( );
public T remove (T element);
public SetADT<T> union (SetADT<T> set);
public boolean contains (T target);
public boolean equals (SetADT<T> set);
public boolean isEmpty ( );
public int size ( );
public Iterator<T> iterator( );
public String toString ( );}
hasNext( )
in an iterator and next(
)
in an iterator
HashSet
and HashMap
list
method consisted of numbers (to be replaced
by letters according to the phone code) and stars (to be replaced by
any letter at all). Suppose we allow letters to appear in the input
string as well, to be replaced only by themselves. (So the call
list ("J*zz*")
would return only "jazzy"
.)
What modifications to your Project #7 code would you need?
SetADT
object was useful for
simulating a bag to draw the numbers. A Bingo card
is
a five by five array of numbers, where the first column's numbers
are in the range 1-15, the second in 16-30, the third in 31-454, the
fourth in 46-60, and the last in 61-75. No number can appear twice
in a column, and the third square of the third column has no
number. Explain (in English) how you would use one or more Set
objects to create a random Bingo card.
DogTeam
objects. Indicate (in English) how
you would write a method that would take such an input and output an
array with the same DogTeam
objects, but sorted in
increasing order of average age. (For example, a team whose
dogs averaged an age of 2.4 would come before one whose dogs
averaged an age of 3.) (You may assume that each team in the array
has at least one dog.)
// This is an L&C priority queue, where "next" is the element
// in the queue whose priority is the smallest integer. It breaks
// ties by earlier creation, i.e., it is FIFO on elements with the
// same priority. Assume that the dogs from above have been created.
PriorityQueue<Dog> pq = new PriorityQueue<Dog> ( );
pq.addElement (cardie, cardie.getAge( ));
pq.addelement (king, king.getAge( ));
Dog z = pq.removeNext( );
pq.addElement (duncan, duncan.setAge( ));
z.setAge(1);
pq.addElement (z, z.getAge( ));
z = pq.removeNext( );
pq.addElement (biscuit, biscuit.getAge( ));
pq.addElement (buck, buck.getAge( ));
pq.removeNext( );
System.out.println (pq.removeNext( ).getName( ));
// Again, assume that the dogs from above have been created.
// This code is not in the DogTeam class.
DogTeam t = new DogTeam( );
t.addToLead (king);
t.addToLead (balto);
t.addtoLead (king);
t.switchLastTwo( );
SledDog s = t.removeLead( );
s.setName ("Yukon King");
SledDog z = t.removeLead( );
System.out.println (z.getName( ));
Integer
objects, we successively add 3, 1, 4, 15, 9, 2,
6, and 5. Then we remove 4, 9, and 3 in that order. Draw the
resulting tree.
public int summation (int n) {
int count = 0, sum = 0;
while (count < n) {
sum += count;
if (count < n) sum += (count + 1);
if (count < n - 1) count += 2;}
return sum;}
// method to be added to DogTeam class
public SledDog[ ] toArray ( ) {
SledDog [ ] result = new SledDog[ ];
LinearNode<SledDog> cur = leadNode;
int index = 0;
while (cur != null) {
result [index] = cur.getElement( );
cur = cur.getNext( ); index++;}
return result;}
// method to be added to DogTeam class
public void reverse ( ) {
SledDog lead = this.removeLead( );
DogTeam temp = new DogTeam( );
while (this.getSize( ) > 0)
temp.addToLead (this.removeLead( ));
temp.reverse( );
this.addToLead (lead);
while (temp.getSize( ) > 0)
this.addToLead (temp.removeLead( ));}
public static String binary (int n) {
if (n == 0) return "0";
if (n == 1) return "1";
if (n % 2 == 0) return binary (n/2) + "0";
return binary (n/2) + "1";}
// n is size of input stack s
public static void reverse (StackADT<Dog> s) {
if (size <= 1) return;
StackADT<Dog> temp = new ArrayStack<Dog> ( );
while (s.size( ) > 1)
temp.push (s.pop( ));
reverse (temp);
Dog last = s.pop( );
while (!temp.isEmpty( ))
s.push (temp.pop( ));
s.push (last);}
// uses L&C code base; n is size of input queue
public static void reorder (QueueADT<Dog> q) {
int n = q.size( );
if (n >= 11) return;
Dog d = q.first( );
QueueADT<Dog> temp = new ArrayQueue<Dog> ( );
for (int i = 0; i < 5; i++)
if (!q.isEmpty( )) temp.enqueue (q.dequeue( ));
while (!temp.isEmpty( ))
q.enqueue (temp.dequeue( ));
while (d != q.first( ))
q.enqueue (q.dequeue( ));}
a) Recall the following code from Project #7 ("Some Games With Words"):
public class PTNode {
private String elem;
private PTNode [ ] child = new PTNode [26];
public PTNode (String w) {elem = w;}
public String getElem ( ) {return elem;}
public void setElem (String w) {elem = w;}
public PTNode getChild (char ch) {return child[ch - 'a'];}
public void setChild (char ch, String w)
{child[ch - 'a'] = new PTNode (w);}
// next method added for this problem
public void setChildNull (char ch) {child[ch - 'a'] = null;}
public class PrefixTree {
private PTNode root;
private int size; // should be the number of leaves
// getters and setters, other methods
Write a method public boolean removeString (String
target)
to be added to the PrefixTree
class.
If target
is the element of a leaf of the prefix tree,
your method should remove its node, remove any other leaves that
become
leaves as a result of its removal, and return true
. (But
don't ever remove the root node ""
.) If
target
is not a leaf or is not in the tree at all, the
method should return false
and do nothing else. You may
assume that if the tree has more than one node, then all the leaves
are at depth 5 as in the tree built in Project #7. Also, you may call
the
contains
method for PrefixTree
, which was
provided to you for Project #7.
b) Implement a stripped-down version of the generic class
HashSet<T>
as follows. Assume that the class
T
has a satisfactory hashcode( )
method. A
HashSet<T>
object should have an instance field
int capacity
and an array of
ArrayList<T>
objects (as in L&C) of size
capacity
. Write a constructor public HashSet (int
c)
that will create an empty HashSet
object with
capacity c
. Write methods public boolean add
(Object o)
and public boolean contains (Object o)
to add an element and test membership for an element, respectively.
The method add
should return false
if the
object o
is already there, but not throw an exception.
(The parameter types of these two methods are Object
to
match java.util.HashSet
, but your methods may throw
exceptions (which you need not catch) if their inputs are not
T
objects.)
Your task is to write an entire class Sudoku
, where a
Sudoku
object will allow a user to play the game. You
should store the board as a two-dimensional array of int
values, using 0 to represent a cell that has no number yet. Your
constructor should create a board with all 0 values. You should write
three methods:
public boolean move (int i, int j, int k)
, which
sets the value of cell (i, j) to be k,
public boolean check( )
, which returns whether the
board is filled successfully, and
public void undo( )
, which reverses the effect of
the last move that has not already been reversed by an
undo
command. If there are no moves that have not
already been reversed, the undo
command should do
nothing and not throw an exception.
For regular credit, write the class so that a move (i, j,
k)
command is only value if the cell (i, j)
is not yet filled,
and k
is in the correct range. The move
method
returns true
if the move is valid.
For up to five points extra credit, allow moves to replace an existing number rather than just fill in a blank cell. This makes the undo method more complicated.
You may use any of L&C's given data structures such as
ArrayStack
, ArrayQueue
,
ArrayList
, ArrayBinaryTree
, or
ArraySet
, assuming that they correctly implement the
corresponding ADT given on the handout.
Last modified 27 December 2011