Q1: 10 points Q2: 15 points Q3: 15 points Q4: 15 points Q5: 15 points Q6: 10 points Q7: 20 points Total: 100 points
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 {
private T element;
private LinearNode next;
// public get and set methods, zero-parameter and one-parameter constructors
}
public class DogTeam {
private LinearNode 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 ( ) {...}
}
// block of code to be 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 = new SledDog("Buck", 108, "Mixed");
// back to class definitions
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
Remember that the Maze class is a two-dimensional array of SCell
objects in Project #2 and of QCell
objects in Project
#4. It has public methods isPath
and path
. For
Question #7 here, we are givingit an additional public method
getCell (int x, int y)
that returns the cell in position (x, y).
Here is a picture of a 3 by 3 hexagonal grid, ready for a game of Hex as defined in Question #7:
| RED
| _ _
_/ \_ _/ \_ _/ \_ _/
/ \ / \ / \ /
| 0, 0 | 1, 0 | 2, 0 |
| | | |
_/ \_ _/ \_ _/ \_ _/
/ \ / \ / \ / BLUE
| 0, 1 | 1, 1 | 2, 1 |
| | | |
BLUE _/ \_ _/ \_ _/ \_ _/
/ \ / \ / \ /
| 0, 2 | 1, 2 | 2, 2 |
| | | |
_/ \_ _/ \_ _/ \_ _/ \_
/ \ / \ / \ / \
RED
enqueue
(in QueueADT
, in L&C) and
add
(in Queue
, in java.util
)
LinearNode
and linear list
LinearNode<T>
and
LinearNode<SledDog>
DropoutStack
class so that there is
a new method called dropped( )
, which returns an array full of
all the elements that have been dropped by the calling stack since it was
created.
int
value k and get back an array
containing all the cells, if any, at distance exactly k from z.
char
values) and be able to encrypt or decrypt arbitrary messages.
DropoutStack<Dog> d = new DropoutStack<Dog>( ) // recall default capacity is 3
d.push (ace);
Dog dog1 = d.peek( );
d.push (duncan);
Dog dog2 = d.pop( );
d.push (cardie);
d.push (ace);
if (dog1 = d.peek( )) d.pop( );
d.resize (5);
System.out.println (d.size( ));
// new method in DogTeam class
public void reorder ( ) {
DogTeam temp = new DogTeam( );
while (!isEmpty( ))
temp.addToLead (remove( ));
leadNode = temp.leadNode;}
// then run this code
DogTeam team = new DogTeam( );
team.addToLead (king);
for (int i = 0; i < 4; i++) {
team.addToLead (balto);
team.reorder ( );
team.addToLead (buck);}
for (int j = 0; j < 6; j++)
team.removeLead( );
System.out.println (team.countHuskies( ));
String [ ] init = {"1111", "1001", "1001", "1111"};
Maze m = new Maze (4, 4, init); // this is a project #4 Maze
QCell [ ] path1 = m.path (0, 0, 3, 3);
System.out.println (path1.length);
DogTeam
class:)
public boolean containsHusky( ) {
boolean done = false;
LinearNode<SledDog> current = leadNode;
while (!done) {
if (current.getElement( ).getBreed( ).equals ("Husky"))
done = true;
current = current.getNext( );}
return done;}
public class DogTeamDriver {
public static void main (String [ ] args) {
DogTeam team = new DogTeam( );
for (int i = 0; i < 6; i++)
team.addToLead (balto);
LinearNode<SledDog> temp = team.leadNode;
for (int j = 0; j < 3; j++) {
team.getElement.setBreed ("Cairn Terrier");
temp = temp.getNext( );}
System.out.println (team.countHuskies( ));}}
StackADT<Dog> s = new ArrayStack<Dog>( );
DogTeam team = new DogTeam( );
for (int i = 0; i < 4; i++)
s.push (new SledDog("Dog" + i, 3, "Dachshund"));
while (!s.isEmpty( ))
team.addtoLead (s.pop( ));
System.out.println (team.countHuskies( ));
LinkedQueue
, like all of
L&C's
collection interfaces, has a toString
method that
returns a description of every element in the collection.)
B:)
LinkedQueue<Dog> q = new LinkedQueue<Dog>( );
for (int i = 0; i < n; i++)
q.enqueue (cardie);
while (!q.isEmpty( )) {
System.out.println (q);
q.dequeue( );}
LinkedQueue
class that takes
parameter
k
and returns (but does not remove) the
k
'th
element in the calling queue, if there is one. Analyze the
worst-case running time of your method in terms of n, the
number of elements in the queue.
LinkedStack
.
Analyze its worst-case running time in terms of n, the number
of
elements in the stack.
public void reverse( ) {
if (isEmpty( )) return;
LinkedStack<T> temp = new LinkedStack<T>( );
while (size( ) > 1) {
LinearNode<T> curr = top.getNext( );
LinearNode<T> prev = top;
while (curr.getNext( ) != null) {
curr = curr.getNext( );
prev = prev.getNext( );}
prev.setNext(null);
curr.setNext(temp.top);
temp.top = curr;
temp.size++;
size--;}
top.setNext(temp.top);
size += temp.size;}
public void feedTeam(
)
to be added to the DogTeam
class. Assume
that the SledDog
class has a method public
boolean feed( )
which has some side effect on the
calling SledDog
and returns true
if
the calling dog is "full" and false
if it is not.
(We do not ask the dogs directly whether they want more food.)
Your method should put the dogs of the colling team in a
queue and call the feed
method for each in turn,
adding each dog to the back of the queue if it is not yet
full. Your method should not terminate until all dogs are full
-- do not worry about the case where some dog remains hungry
indefinitely.
The game works like this. Initially all cells are unowned. Red and Blue alternate moves, with Red moving first. A move consists of making a previously unowned cell belong to the player moving. The winner is the first player to make a path, of cells that they own, between the two sides of the grid that they own -- north and south for Red, east and west for Blue.
Your task is the implement this game using the Maze class
from either Project #2 or Project #4 (the differences are
unimportant.) You may asssume that the Maze class has been
altered to give a hexagonal grid, but you may not change the
Maze class otherwise. (But the Maze class has a method
public QCell getCell (int x, int y)
which returns
the cell in position (x, y).)
Remember that the two-parameter constructor for Maze makes every
cell open. You may find it useful to run this, then use for
loops and getCell(i, j).setIsOpen (false)
to make
every cell closed.
You also get a Position
class, where a
Position
object has int
fields
x
and
y
, methods getX
, setX
,
getY
, setY
, and a two-parameter
constructor.
You will get input from the players by two methods
getRedMove
and getBlueMove
, each of
which return a Position
object and have no
parameters. (I did not say this clearly enough on the test
paper, but my intention was that you should not write
these methods, but assume that they are given to you and treat
them with black boxes.) Don't worry about excpetions that might
be thrown by these two methods -- it is ok if they crash your
program.
You should write a class HexGame
so that a
HexGame object allows the two players to play one game of Hex.
Use one or more Maze objects to store the state of the game.
When it is Red's move, you should call getRedMove
as many times as needed to get a valid Position
(in
the grid and unowned) and then update your Maze objects to
reflect the Red move. It then becomes Blue's turn, and you get
and process a valid Blue move the same way. The game should
continue until one player has won -- it turns out the draws in
this game are impossible. Announce invalid moves and the end of
the game on via System.out.println
.
Here is a good way to get started. Before you have
necessarily decided how to store the state of the game, assume
that you have methods isValidRedMove
,
isValidBlueMove
, processRedMove
,
processBlueMove
, hasRedWon
, and
hasBlueWon
already written. You will get
substantial partial credit for writing a main method of
HexGame
that calls on these methods. Then you can
decide on the representation and write these six methods.
Finally, the easiest way to code hasRedWon
and
hasBlueWon
is inefficient, in that these methods
make O(n2) calls to the isPath
method of
Maze
, when it is possible to make only O(1) calls.
You can get up to 16 of the 20 points for this problem for this
inefficient implementation. You can get full credit for coding
the inefficient implementation, then describing clearly in
English how to fiex it to get only O(1) calls. (Of course you
can also get full credit for coding the efficient version
immediately.)
Last modified 22 October 2011