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 ( );}
public class Foo
and public class Foo
extends Object
java.util.List
and java.util.LinkedList
directions
that would take a source and destination cell and return a string
describing a sequence of north, east, south, and west moves that
would
take you from the source to the destination, using only open cells?
(It should return the string "no path" if there is no path.)
// uses L&C code base, Dog class, standard dogs from above
Set<Dog> kennel = new ArraySet<Dog>( );
kennel.add (cardie);
kennel.add (duncan);
boolean b = kennel.contains (duncan);
boolean c = kennel.contains (biscuit);
if (b || c) kennel.remove (cardie);
kennel.add (duncan);
Dog z = kennel.removeRandom ( );
System.out.println (z.getName( ));
Dog terrier = duncan;
Dog retriever = cardie;
terrier.setName (cardie.getName( ));
retriever.setName (duncan.getName( ));
if (terrier == cardie) duncan.setName ("Biscuit");
System.out.println(cardie.getName( ) + " " + duncan.getName( ));
removeMin
operations. What are the resulting
contents of the array? (Assume that the initial capacity was 10.)
// method to be added to the DogTeam class
public SledDog [ ] toArray ( ) {
SledDog [ ] ret = new SledDog [size];
if (size == 0) return ret;
ret[0] = leadNode.getElement( );
int index = 0;
LinearNode<SledDog> cur = leadnode;
while (cur.getNext( ) != null) {
index++;
ret[index] = cur.getElement( );
cur = cur.getNext( );}
return ret;}
// method to be added to the DogTeam class
public DogTeam clone( ) {
DogTeam ret = new DogTeam( );
if (size == 0) return ret;
LinearNode<SledDog> cur = leadNode;
leadNode = cur.getNext( );
size--;
LinearNode<SledDog> newLead =
new LinearNode<SledDog>(cur);
ret = this.clone( );
ret.addToLead (newLead);
return ret;}
// method to be added to the Maze class of projects 1 and 2
public int countOpenCells ( ) {
count = 0;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
if (cells[i, j].isOpen( )) count++;
return count;}
// method to be added to DogTeam class
public void rotate ( ) {
if (size <= 1) return;
LinearNode<SledDog> old = leadNode;
leadNode = leadNode.getNext( );
rotate ( );
LinearNode<SledDog> second = leadNode.getNext( );
leadNode.setNext (old);
old.setNext (second);}
// method to be added to DogTeam class
public void reverse ( ) {
if (size <= 1) return;
if (size == 2) {
SledDog first = removeLead( );
SledDog second = removeLead( );
addToLead (first);
addToLead (second);
return;}
SledDog oldLead = removeLead( );
reverse( );
SledDog oldRear = removeLead( );
addToLead (oldLead);
reverse( );
addToLead (oldRead);}
// uses L&C code base; n is size of input queue
public static void reorder (QueueADT<Dog> q) {
int n = q.size( );
if (n <= 6) return;
Dog d = q.first( );
QueueADT<Dog> temp = new LinkedQueue<Dog>( );
for (int i = 0; i < 3; i++)
temp.enqueue (q.dequeue( ));
for (int j = 0; j < 3; j++)
q.enqueue (q.dequeue( ));
while (!temp.isEmpty( ))
q.enqueue (temp.dequeue( ));
while (d != q.first( ))
q.enqueue (q.dequeue( ));}
a) Write a method public DogTeam merge (DogTeam
other)
to be added to the DogTeam
class. If x
and y are two DogTeam
objects that are sorted by
age, then x.merge (y)
should return a team
consisting
of exactly the dogs from both x and y, also sorted by age. ("Sorted
by age" means that each dog in the team has an age less than or equal
to the dog following it.) You may assume that no individual dog
appears in both teams.
If either team is not sorted by age, the method should throw an
UnsortedException
-- assume that this exception class
has been defined.
b) Write a method public boolean hasDuplicate( )
to
be added to the DogTeam
class. It should return two if
any two of the dogs in the calling team are the same dog
according to the equals
method of the
SledDog
class. You may use any of the data structures
defined by L&C, but not an iterator for DogTeam
unless you write it. Your method should leave the calling
DogTeam
exactly as it was before the call.
Your job here is to write code to maintain the waiting list for
admission to a course. Students are stored as Student
objects, and the roster of those students already in the course is
stored as an object roster
of type
SetADT<Student>
. The class Student
has a compareTo
method so that the "smallest" student in
a collection is the one most eligible to fill a vacancy in a course.
OrderedList
isn't a class in the L&C
code base, and we can't extend OrderedListADT
because
it's
an interface.
You are to define a class Waitlist
extending the class
ArrayOrderedList<Student>
, with these three methods:
Waitlist ( )
that establishes an empty waiting list,
addStudent (Student s)
that
puts a new student into the list,
fillCourse (SetADT<Student>
roster, int capacity)
that takes students from the calling waiting list, in order of
their eligibility, and adds them to
roster
until either (a) the size of roster
reaches
capacity
or (b) the waiting list is empty. It is all
right to add a student to roster
who is already in the
course -- the SetADT
methods will ensure that there is
no duplicate entry and that the size is maintained correctly.
Last modified 10 December 2011