Q1: 10 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 10 points Q7: 20 points Total: 100 points
Dog class:
public class Dog {
private String name;
private int age;
public Dog (String newName, int newAge) {
name = newName;
age = newAge;}
public String getName {return name;}
public void setName (String newName) {name = newName;}
public int getAge ( ) {return age;}
public void setAge (int newAge) {age = newAge;}
public boolean equals (Dog other) {
if (!this.name.equals(other.name)) return false;
return (this.age == other.age);}}
Several questions use these generic interfaces from DJW:
public interface QueueInterface<T> {
T dequeue( );
boolean isEmpty( );}
public interface BoundedQueueInterface<T> extends QueueInterface<T> {
void enqueue (T element);
boolean isFull( );}
public interface UnboundedQueueInterface<T> extends QueueInterface<T> {
void enqueue (T element);}
Question 7 uses this generic interface from DJW:
public interface ListInterface<T> {
int size( ); // number of elements in the list
void add (T element); // put "element" into the list
boolean contains (T element); // is "element" in the list?
boolean remove (T element); // returns whether element was there
T get (T element); // returns item equal to
// "element" or returns null
String toString( ); // returns string describing list
void reset( ); // sets current position to beginning
T getNext( ); // returns item at current position,
// advances current position
}
LLNode<T> and the class DLLNode<T>
markBlobs method,
which was recursive and thus implicitly used a stack. Could we
have done this job instead with a non-recursive method using
queues? Explain how we could or why we could not.
X in a
MapGrid object, which was the distance from
X to the farthest square on X's
continent. How could you use a similar queue-based method to
determine the number of squares on X's
continent?
Dog class from above is present.
Include a brief justification of your answer.
// give the return value of recurse (3, 3)
public int recurse (int j, int k) {
if ((j == 0) && (k == 0))
return 7;
if (j == 0) return (1 + recurse (0, k - 1));
return recurse (j - 1, k + 1);}
Dog cardie = new Dog("Cardie", 5);
Dog duncan = new Dog("Duncan", 3);
LLNode<Dog> list = new LLNode<Dog>(null);
LLNode<Dog> cNode = new LLNode<Dog>(cardie);
LLNode<Dog> dNode = new LLNode<Dog>(duncan);
list.setLink(cNode);
cNode.setLink(dNode);
dNode.setLink(list);
list.setLink(list.getLink( ).getLink( ));
cNode.setLink(dNode.getLink( ));
dNode.setLink(dNode.getLink( ).getLink( ));
LLNode<Dog> cur = cNode;
for (int i = 0; i < 4; i++)
cur = cur.getLink( );
System.out.println (cur.getInfo( ).getName( ));
QueueInterface<String> q = new ArrayQueue<String> ( );
QueueInterface<String> r = new ArrayQueue<String> ( );
q.enqueue ("Cardie");
r.enqueue ("Duncan");
q.enqueue ("Biscuit");
r.enqueue ("Ace");
for (int i = 0; i < 3; i++) {
String first = q.dequeue( );
String second = r.dequeue( );
if (first.compareTo(second) < 0) {
q.enqueue (first);
r.enqueue (second);}
else {
r.enqueue (first);
q.enqueue (second);}}
while (!q.isEmpty( ))
System.out.println (q.dequeue( ));
public class DogPound {
public DogPound ( ) {
ListInterface<Dog> list = new ArrayUnsortedList<Dog> ( );}
public static void main (String [ ] args) {
Dog cardie = new Dog("Cardie", 5);
list.add (cardie);
System.out.println (list);}}
public int multiply (int j, int k) {
// precondition: j and k are positive or 0
// postcondition: return value is j * k
if (k == 0)
return 0;
if (j == 1)
return k;
return (k + multiply (j - 1, k);}
Dog cardie = new Dog("Cardie", 5);
Dog duncan = new Dog("Duncan", 3);
LLNode<Dog> list = new LLNode<Dog>(null);
LLNode<Dog> cNode = new LLNode<Dog>(cardie);
LLNode<Dog> dNode = new LLNode<Dog>(duncan);
cNode.setLink(list);
dNode.setLink(cNode);
list = dNode;
LLNode cur = list;
while (cur.getInfo( ) != null) {
int totalAge += cur.getInfo( ).getAge( );
cur = cur.getLink( );}
System.out.println (totalAge);
BoundedQueueInterface<String> q = new ArrayQueue<String> (3*N);
BoundedQueueInterface<String> r = new ArrayQueue<String> (3*N);
for (int i = 0; i < N;, i++)
q.enqueue("Cardie");
boolean flag = false;
while (!q.isEmpty( )) {
if (flag)
r.enqueue (q.dequeue( ));
else r.enqueue ("Duncan");
flag = !flag;}
BoundedQueueInterface<String> q = new ArrayQueue<String> (N);
BoundedQueueInterface<String> r = new ArrayQueue<String> (N);
for (int i = 0; i < N;, i++)
q.enqueue("Cardie");
while (!q.isEmpty( )) {
while (!q.isEmpty( ))
r.enqueue(q.dequeue( ));
r.dequeue( );
while (!r.isEmpty( ))
q.enqueue(r.dequeue( ));}
UnboundedQueueInterface<String> q = new LinkedQueue<String> ( );
UnboundedQueueInterface<String> r = new LinkedQueue<String> ( );
q.enqueue("Cardie");
for (int i = 0; i < N; i++) {
while (!q.isEmpty( )) {
r.enqueue(q.dequeue( ));
r.enqueue("Duncan");}
while (!r.isEmpty( ))
q.enqueue (r.dequeue( ));}
Remember that you had a Board class whose objects have 9 by 9
arrays of numbers, with 0's representing unassigned cells. To
make things simple and uniform for this question, we will revise
the Board class to have the following methods,
which you may assume to be already written:
public int getEntry (int row, int col)
// returns array entry from that row and column
public Board move (int row, int col, int value)
// returns a new Board with entry at that row and column
// changed to "value"
public boolean isBad ( )
// returns true if calling Board has a positive number
// twice in any row, column, or box
Write a method public Board solution ( ) to be
added to the Board class. This method should
return null if the Board is not
solvable (if it is not possible to replace the zeros with
positive numbers to get a valid completed puzzle). If there is
a solution, the method should return the first one in
lexicographic order (just as SudokuSolver did in
Project #2).
Your strategy should be to move toward a solution by finding
the first unassigned cell, trying all nine possible digits in
that position, and for each one using a recursive call to see
whether there is a solution. There are two base cases for your
recursion -- one where you know that no solution is possible and
one where you know that the curerent Board is the solution.
int),
a Person object representing the owner, and a
Dog object representing the dog to be licensed.
Write the header and field declarations of the License
class.
You may assume that you have standard getters and setters for each
field,
and a constructor that takes a parameter to set each field.
But if you need this class to have any other methods, you must write
them.
You also need to write a class LicenseDatabase such
that a
LicenseDatabase object stores a set of
License
objects in a sorted list, where the objects are sorted by license
number.
You will need to make sure that there are never two licenses with the
same license number in the list. Thus you must write the
equals and compareTo
methods for License that will make this work.
LicenseDatabase will have the following methods:
public boolean addNewLicense (int number, Person owner, Dog d)
// adds new license with given data to database,
// unless another license with that number is there
// return value tells whether the addition was successful
public boolean removeLicense (int number)
// removes license with that number if it is there
// return value tells whether the removal had any effect
public License findDog (Dog d)
// returns the first License object with d.equals (dog) if any,
// otherwise returns null
The code base at the beginning of the exam has the
ListInterface
interface from DJW, and you may assume that the class
ArraySortedList
implements this interface with the intended behavior for a sorted
list.
The code base also has the Dog class from the first exam
(with an equals method added), and you may assume that
there is a Person class that also has an equals method implemented.
Remember that DJW lists do not use iterator objects, but get the same
functionality with reset and
getNext. If getNext is called when the current position is at the end, it goes to the beginning.
Last modified 1 January 2013