Instructions:
Examine the program on the following pages and answer these questions about the different tokens and constructs in the program listing. Hand your answers to me (RTH) before, or during class on Monday (Oct 18th).
1.
How many
classes are there?
2.
Which lines
contain the data mambers in class DisplayBoard?
3.
Which lines
contain declarations of constants?
4.
Which lines
contain loops?
5.
Which line
is the end of the if statement that starts on line 97?
6.
How may
parameters does the constructor function for class DisplayFrame have?
7.
Which lines
contain the moduls (remainder) operator?
8. Which lines contain a compound assignment operator?
9.
How many
methods does class DisplayFrame have?
10.
Which lines
contain a reserved word constant?
11.
Which lines
in class DisplayBoard contain a unary operator?
12.
Which lines
contain the keyword ‘this’?
import java.awt.*;
import java.util.*;
// interface and display classes
class Display {
// contains the top-level frame and the menu handler
// it is similar to the view in a document/view structure
DisplayFrame frame; // the top-level frame
Game game; // a back reference to the game (the document)
// Image carrier;
Display(Game g) {
game = g;
frame = new DisplayFrame("Battleship", this);
frame.resize(750, 400);
frame.pack();
frame.setResizable(false);
}
void show() {
frame.show();
}
GameBoard getGameBoard() {
return frame.board1;
}
ScoreBoard getScoreBoard() {
return frame.board2;
}
}
class DisplayFrame extends Frame {
// the main frame contains the two playing boards
GameBoard board1;
ScoreBoard board2;
Display disp; // a reference to the display object
MenuItem m1, m2, m3;
Location square = null; // set by HVDialog
int nShip; // set by HVDialog
Button h; // set by HVDialog
Button v; // set by HVDialog
HVDialog hvDialog = null; // set by HVDialog
DisplayFrame(String title, Display d) {
super(title);
disp = d;
// create the two boards and disable them
board1 = new GameBoard(this);
board1.disable();
board2 = new ScoreBoard(this);
board2.disable();
setLayout(new GridLayout(1, 2));
add(board1);
add(board2);
// set menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu = new Menu("Game", true);
mb.add(menu);
m1 = new MenuItem("Play against computer");
menu.add(m1);
m2 = new MenuItem("Play another program");
menu.add(m2);
menu.addSeparator();
m3 = new MenuItem("Quit");
menu.add(m3);
// place the window
move(100, 200);
}
void computerPlay() {
System.out.println("Resetting for computer play"); // TRACE
disp.game.computerPlay();
}
void netPlay() {
System.out.println("Resetting for net play"); // TRACE
disp.game.netPlay();
}
public void repaint() {
board1.repaint();
board2.repaint();
}
public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY) {
QuitDialog d = new QuitDialog(this);
d.show();
return true;
}
return super.handleEvent(e);
}
public boolean action(Event e, Object arg) {
if (e.target == m1) {
disp.game.reset();
computerPlay();
}
else if (e.target == m2) {
disp.game.reset();
netPlay();
}
else if (e.target == m3) {
QuitDialog d = new QuitDialog(this);
d.show();
}
return true;
}
}
class DisplayBoard extends Canvas {
// a playing board is a grid painted on a canvas
static final int HSIZE = 300;
static final int VSIZE = 300;
static final int NSQUARES = 10;
int hSpacing;
int vSpacing;
DisplayFrame parent;
DisplayBoard(DisplayFrame frame) {
resize(HSIZE, VSIZE);
parent = frame;
}
Game getGame() {
return parent.disp.game;
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
return new Dimension(HSIZE, VSIZE);
}
Location getSquare(int x, int y) {
return new Location(x / (size().width / NSQUARES),
y / (size().height / NSQUARES));
}
void paintGrid(Graphics g) {
Rectangle r = bounds();
// set the canvas size to be a multiple of the square size
resize(r.width - r.width % NSQUARES + 1,
r.height - r.height % NSQUARES + 1);
// draw the grid
hSpacing = r.width / NSQUARES;
vSpacing = r.height / NSQUARES;
// System.out.println("H spacing: " + hSpacing +
// " V spacing: " + vSpacing); // TRACE
for (int x = 0; x <= r.width; x += hSpacing)
g.drawLine(x, 0, x, r.height);
for (int y = 0; y <= r.height; y += vSpacing)
g.drawLine(0, y, r.width, y);
}
}