Q1: 20 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 20 points Q7: 20 points Total: 100 points
public class Dog {
private String name;
private int age;
public Dog (String name, int age) {
this.name = name;
this.age = age;}
public String getName {return name;}
public void setName (String name) {this.name = name;}
public int getAge ( ) {return age;}
public void setAge (int age) {this.age = age;}
public boolean equals (Dog other) {
if (!this.name.equals(other.name)) return false;
return (this.age == other.age);}}
public interface ListInterface<T> {
public int size( ); // number of elements in the list
public void add (T element); // put "element" into the list
public boolean contains (T element); // is "element" in the list?
public boolean remove (T element); // returns whether element was there
public T get (T element); // returns item equal to
// "element" or returns null
public String toString( ); // returns string describing list
public void reset( ); // sets current position to beginning
public T getNext( ); // returns item at current position,
// advances current position
}
public interface PriQueueInterface<T extends Comparable>> {
public boolean isEmpty( );
public boolean isFull( );
public void enqueue (T element);
public T dequeue( );}
public class BSTNode<T extends Comparable<T>> {
protected T info;
protected BSTNode left;
protected BSTNode right;
public BSTNode (T info) {this.info = info; left = right = null;}
public void setInfo (T info) {this.info = info;}
public T getInfo ( ) {return info;}
public void setLeft (BSTNode link) {left = link;}
public void setRight (BSTNode link) {right = link;}
public BSTNode getLeft ( ) {return left;}
public BSTNode getRight ( ) {return right;}}
w1 == w2
and w1.equals(w2)
(when w1
and w2
are String
values)
get(x)
(in a list) and
remove(x)
(in a list)
remove
and remove(x)
in java.util.PriorityQueue
java.util
version
of a priority queue, which has many methods other than those in
DJW's PriQueueInterface
. How would the
implementation have changed if we had been required to use only
the methods specified in that interface?
FrequencyList
class to use a priority queue instead
of a binary search tree. Suppose we wanted to keep the
WordFreq
objects in an alohabetically sorted list
(implementing DJW's ListInterface
) rather than in a
priority queue. How (in general terms) would our adaptation of
the DJW code proceed? Would this have been more or less
complicated to code than the actual assignment?
// uses DJW definitions
ListInterface<Dog> dl = new ArrayUnsortedList<Dog> ( );
Dog cardie = new Dog("Cardie", 5);
Dog otherCardie = new Dog("Cardie", 5);
dl.add(cardie);
dl.add(otherCardie);
System.out.println (Dl.contains(cardie));
Dog x = dl.get(cardie);
dl.remove(cardie);
System.out.println (dl.contains(cardie));
Dog y = dl.get(cardie);
System.out.println (x.equals(y));
public class Question3b {
public static void doubleAge (Dog d) {
d.setAge (2 * d.getAge( ));}
public static void switchAges (Dog d1, Dog d2) {
d1.setAge (d2.getAge( ));
d2.setAge (d1.getAge( ));
public static void main (String [ ] args) {
Dog cardie = new Dog ("Cardie", 5);
Dog duncan = new Dog ("Duncan", 3);
doubleAge (duncan);
switchAges (cardie, duncan);
switchAges (duncan, cardie);
doubleAge (cardie);
System.out.println (cardie.getAge( ));
System.out.println (duncan.getAge( ));}}
Heap<Integer>
object, insert the values 3, 1,
4, 1, 5, and 9. Draw the resulting heap. Then remove the
largest element three times. Draw the resulting heap. (A heap
may have multiple elements with the same value.)
// method to be part of some generic class
public static T[ ] toArray (Stack<T> s) {
// return array containing the elements of s, top element first
// uses java.util.Stack where pop both removes and returns
T[ ] out = (T[ ]) new Object[s.size( )];
for (int i = 0; i < s.size( ); i++)
out[i] = s.pop( );
return out;}
public class Terrier extends Dog {
String color;
public Terrier (String name, int age, String color) {
super (name, age);
this.color = color;}
public String getColor( ) {return color;}
public String setColor (String c) {color = c;}
public boolean equals (Dog other) {
if (super.equals (other))
return (this.color.equals(other.color));
else return false;}}
public static void sortStrings (String [ ] arr) {
if (arr.length <= 1) return;
int minIndex = 0;
String min = arr[0];
for (int i = 0; i < arr.length; i++)
if (arr[i].compareTo(min) < 0) {
minIndex = i;
min = arr[i];}
String temp = arr[minIndex];
arr[minIndex] = arr[0]; // additional error corrected during test
arr[0] = temp;
sortStrings (arr);}
public static void PQSelectSortStrings (String [ ] arr) {
// this is a DJW Heap which implements PriQueueInterface
int n = arr.length;
Heap<String> heap = new Heap<String> (n);
for (int i = 0; i < n; i++)
heap.enqueue (arr[i]);
for (int j = 0; j < n; j++)
arr[j] = heap.dequeue( );}
public static void rearrange (String [ ] arr) {
// uses java.util.Stack where pop both removes and returns
int n = arr.length;
boolean flag = false;
Stack<String> s = new Stack<String> ( );
Stack<String> temp = new Stack<String> ( );
for (int i = 0; i < n; i++)
s.push(arr[i]);
int index = 0;
while (s.size( ) > 0) {
while (s.size( ) > 0) {
if (flag) temp.push(s.pop( ));
else {arr[index] = s.pop( );
index++;}
flag = !flag;}
while (temp.size( ) > 0)
s.push(temp.pop( ));}}
public static int square (int n) {
if (n > 0) {
int previousSquare = square (n - 1);
return (previousSquare + 2*n - 1);}
// above line had +1 on test, getting wrong answer, same timing
else return 0;}
UnsortedKistPQ
that implements that DJW PriQueueInterface
by using
a DJW ArrayUnsortedList<T>
object to hold the
elements of the priority queue. I have not given you code for
ArrayUnsortedList
, but you don't need it because you
may only use the methods in it that are mentioned in the
interface ListInterface<T>
. Note, however, that
ArrayUnsortedList
has one constructor with no
parameters, and that an ArrayUnsortedList
object
expands as needed to hold any number of elements.
public static String
sortedString (String str)
that assumes that each
character in its input String
is one of the char
values from 'a'
thorugh 'z'
,
inclusive, and returns a sorted version of the string. For
example, sortedString("cardie")
should return
"acdeir"
and sortedString("duncan")
should return "acdnnu"
. For full credit, your
method should run in O(n) time on an input string of n
letters. (But you can get partial credit for any correct method.)
DogTree
which will hold an
ordered collection of Dog
objects as a binary search
tree. The class should not be generic and should not use the
class BinarySearchTree<T>
, though it will use the
generic class BSTNode<T>
for its nodes. The
ordering we want on Dog
objects uses the
compareTo
operation on the String
objects
for their names -- note that the Dog
class has no
compareTo
method and you may not add one.
DogTree
, such that
the instance method int size( )
will return the
number of dogs in the tree and will take O(1) time.
int size( )
that takes
O(1) time (no matter how many dogs are in the tree).
boolean add (Dog
d)
that will insert the new dog d
into the
tree if there is no dog already in the tree with the same
name. Your method should return true
if the new dog
is added and false
if there was already a dog there
with that name. (Hint: You may want to use a helper method
boolean recAdd (Dog d, BSTNode<Dpg> n)
but
you are not required to do so.)
Dog youngest( )
for DogTree
that will return a Dog
(not
a node) in the calling tree that has an age no larger than that
of any other Dog
in the tree. The method should
return null
if the tree is empty. This method
must make use of recursion -- I suggest making a helper
method Dog recYoungest (BSTNode<Dog> node)
that
returns the youngest dog in the subtree under node
.
Last modified 5 January 2013