Java Reference
In-Depth Information
Design an interface
Game
that describes a board game. Think about what
your program needs to do. It asks the first player to input a moveȌa
string in a game-specific format, such as
Be3
in chess. Your program
knows nothing about specific games, so the
Game
interface must have a
method such as
boolean isValidMove(String move)
Once the move is found to be valid, it needs to be executedȌthe
interface needs another method
executeMove
. Next, your program
needs to check whether the game is over. If not, the other player's move
is processed. You should also provide some mechanism for displaying
the current state of the board.
Design the
Game
interface and provide two implementations of your
choiceȌsuch as
Nim
and
Chess
(or
TicTacToe
if you are less
ambitious). Your
GamePlayer
class should manage a
Game
reference
without knowing which game is played, and process the moves from
both players. Supply two programs that differ only in the initialization of
the
Game
reference.
435
436
ANSWERS TO SELF-CHECK QUESTIONS
1. It must implement the
Measurable
interface, and its
getMeasure
method must return the population.
2. The
Object
class doesn't have a
getMeasure
method, and the add
method invokes the
getMeasure
method.
3. Only if
x
actually refers to a
BankAccount
object.
4. NoȌa
Coin
reference can be converted to a
Measurable
reference, but
if you attempt to cast that reference to a
BankAccount
, an exception
occurs.
5.
Measurable
is an interface. Interfaces have no fields and no method
implementations.