Q1: 10 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 10 points Q7: 20 points Total: 100 points
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");
public class Cell {
private int x;
private int y;
private boolean isOpen;
// public get and set methods, two- and three-parameter constructors
}
public class SCell extends Cell {
private boolean seen;
// public get and set methods, constructors
}
public class QCell extends SCell {
private int distance;
private QCell parent;
// public get and set methods, constructors
}
In Project #2 Maze is a two-dimensional array of SCells, in Project #4 of QCells. Both versions have methods path and isPath.
// using L&C's code base
QueueADT<Dog> q = new CircularArrayQueue<Dog>( );
q.enqueue(biscuit);
q.enqueue(king);
Dog dog1 = q.first( );
q.dequeue( );
q.dequeue( );
try{dog1 = q.peek( )}
catch (EmptyCollectionException e) {dog1 = king;}
System.out.println(dog1.getName( ));
// new method in DogTeam class
public void merge (DogTeam other) {
if (isEmpty( )) leadNode = other.leadNode;
LinearNode ourLead = removeLead( );
while (!other.isEmpty( ))
addtoLead(other.removeLead( ));
addToLead (ourLead);}
// then run this code
DogTeam team1 = new DogTeam( );
team1.addToLead (balto);
team1.addToLead (king);
team1.addToLead (buck);
team1.switchLastTwo( );
team2.add(king);
team2.add(balto);
team2.merge(team1);
team1.removeLead( );
System.out.println(team2.removeLead( ).getName( ););
// using the Maze class from Project #2
String [ ] init = {"101", "010", "101"};
Maze m = new Maze (3, 3, init);
try {
SCell sc = m.path(0, 0, 1, 1)[0];
System.out.println(sc);}
catch (ArrayOutOfBoundsException e) {System.out.println("Whoops!);}
catch (NullPointerException e) {System.out.println("Double Whoops!");}}
// new method in DogTeam
public SledDog last ( ) {
if (isEmpty( )) return null;
SledDog temp = leadNode.getElement( );
while (temp.getNext( ) != null)
temp = temp.getNext( );
return temp;}
public class DogTeamDriver {
DogTeam rcmp = new DogTeam( );
for (int i = 0; i < 5; i++)
rcmp.addToLead(new SledDog ("Dudley", 4, "Husky"));
rcmp.addToFront(king);
System.out.println(rcmp.countHuskies( ));}
// using L&C's code base
QueueADT q = new LinkedQueue<Dog>( );
q.enqueue(biscuit);
q.enqueue(duncan);
Dog temp = q.dequeue( );
q.enqueue(cardie);
while (!q.isEmpty( )) q.dequeue( );
try {temp = q.peek( );}
catch (EmptyCollectionException e) { }
System.out.println(q.size( ));
ArrayStack<Dog> s = new ArrayStack<Dog>( );
for (int i = 0; i < n; i++)
s.push(cardie);
// here "merge" is the method from 3b above
DogTeam team1 = new DogTeam( );
DogTeam team2 = new DogTeam( );
for (int i = 0; i < n; i++) {
team1.addToLead (buck);
team2.addToLead (king);}
for (int k = 0; k < n; k++)
team1.merge(team2);
A Maze is defined to be connected if every possible pair of open cells has at
least one path
from one to the other. Write an instance method public boolean isConnected( )
, to
be added to the Maze class, that
returns true if and only if the calling Maze is connected. You should use existing
methods from the Maze class, either from Project #2 or #4 (your choice).
The managers of the Iditarod sled dog race need to produce particular lists of dogs competing in the race, and would like to use your SledDog and DogTeam classes to do it. They have an array "entries" of DogTeam objects, containing all the dogs on all the teams entering the race. They want an array "ageTeams" containing the same dogs, but arranged into new teams where each team consists of dogs with the same age. You may assume that the ages of the dogs range from 0 through some constant MAX_AGE. The order of dogs within the new teams does not matter.
Write a method public DogTeam[ ] makeAgeTeams (DogTeam [ ]
entries)
to go into one of their classes -- note that it thus may use only public methods of the class DogTeam.
Assume that the DogTeams in "entries" are cloned before your method is run, so you need
not worry about destroying the copies of them you have. (Hint: Put all the dogs into
a queue, then take each dog in turn out of the queue and add it to the appropriate new
team.)
Last modified 15 October 2011