Chapter 8: Putting a System Together.

Download Report

Transcript Chapter 8: Putting a System Together.

An Introduction to
Programming and Object
Oriented Design using Java
3rd Edition. Dec 2007
Jaime Niño
Frederick Hosch
Chapter 8: Putting a System Together.
Software Life cycle
 The
software
involves
life
cycle
 The process is
 iterative;
 analysis;
 incremental;
 specification;
 compositional.
 design;
 implementation;
 testing;
 maintenance.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
1
Design of a System
 Game of “simple nim”: there are two players and a pile
of sticks. Each player, in turn, removes one, two, or
three sticks from the pile. Player who removes the last
stick loses.
 Initial implementation games will be played “computer
vs. computer.”
 User determines whether to play another game and how
many sticks to start with.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
2
Functional specification
 Basic function of the system: Play a number of
games of “simple nim,” reporting the results to the user.
 System functionality:
 Allow user to specify number of sticks at start of
game.
 For each player in turn, determine what play to make
(number of sticks to remove) and make the play.
 Display state of game after each play:
number of sticks taken,
number of sticks left,
when game is over, who won.
 Allow user to choose to play another game.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
3
Functional specification
User interface
 When the program is run, the user is offered the
following menu:
Enter the number denoting the action to perform:
Run game...............1
Exit...................2
Enter choice:
 Entering 2 terminates the program.
 Entering 1 produces the following prompt:
Enter number of sticks (a positive integer):
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
4
Functional specification
 User interface
 A play by play description of game is displayed,
similar to the following:
Player Player1 takes 1 stick(s), leaving 4.
Player Player2 takes 3 stick(s), leaving 1.
Player Player1 takes 1 stick(s), leaving 0.
Player Player2 wins.
 The original menu is again displayed.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
5
System design
 Need a model and user-interface components.
 No data component required.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
6
System design
 Identifying model objects
 Two players modeled by class Player.
 Pile of sticks modeled by class Pile
 Game, (rules of the game), modeled by class Game.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
7
System Design
Class: Pile
a pile of sticks for playing simple nim
Responsibilities:
Collaborators
do:
remove sticks
know:
number of sticks
remaining in
the Pile
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
8
System Design
Class: Player
a player of the simple nim game
Responsibilities:
Collaborators
Pile
do:
make a play by removing
sticks
from the Pile
know:
Player’s name
the number of sticks removed
on
this Player’s most recent turn
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
9
System Design
Class: Game
a manager of a simple nim game
Responsibilities:
do:
Collaborators
Players, Pile
conduct a play of game,
instructing appropriate Player to
take a turn
know:
the Players
the Pile
number of sticks that can be
taken on a turn
which Player plays next
which Player played last
when the game is over
which Player won when game is
over
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
10
System Design
Class: NimTUI
text-based user interface for the simple nim system
Responsibilities:
Collaborators
do:
allow user to indicate
whether or not another
game is to be played
allow user to specify number
of sticks to be used in a
game
have a game played
display each play of a game,
when the game is over,
and which player has won
Dec 2007
Game
Game, Player(s)
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
11
Relationship between objects
dir ects
G am e
2
P lay er
p ro vid es
Pi le
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
12
Case: game not over, player1 turn.
G am e
Pl ay er p la ye r1
Pla ye r p la yer 2
P ile
p lay
s tic k s
int
tak e Tu rn
s t ic k s
int
r em o v e
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
13
Pile specifications
class Pile
A pile of sticks for playing simple nim
public Pile (int sticks)
Create a new Pile, with the specified
number of sticks.
require: sticks >= 0
public int sticks ()
The number of sticks remaining in this Pile.
ensure: this.sticks() >= 0
public void remove (int number)
Reduce the number of sticks by specified amount.
require: number >= 0 and number <= this.sticks()
ensure: this.sticks() == old.sticks() - number
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
14
Player specifications
class Player
A player in the game simple nim.
public Player (String name)
Create a new Player with the specified name.
ensure:
this.name().equals(name)
public String name ()
This Player’s name.
public int sticksTaken ()
Number of sticks removed on this Player's most
recent turn.
Returns 0 if this Player has not yet taken a turn.
ensure:
this.sticksTaken() >= 0
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
15
Game specifications
class Game
A game manager in the game simple nim.
public Game (Player player1, Player player2,
int sticks)
Create a nim Game, with specified Players
and specified number of sticks.
First Player (player1) plays first in game.
require:
sticks > 0
public int sticksLeft ()
The number of sticks remaining in the pile.
ensure: this.sticksLeft() >= 0
public Player nextPlayer ()
The Player whose turn is next.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
16
Game specifications
public Player previousPlayer ()
The Player who last played; returns null if
no play has been made yet.
public boolean gameOver ()
The game is over.
public Player winner ()
winning Player: did not make last play in game.
Returns null if game is not over.
ensure: if this.gameOver(),
this.winner() != this.previousPlayer()
public void play ()
Conduct a play of the game, allowing the appropriate
Player to take a turn.
Has no effect if the game is over.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
17
User interface specifications
 User interface is a client of the Game and the Players.
 Give user interface creation responsibility for Game, as
several games might be played.
 Give user interface creation responsibility for Players
since we might want to let user name the Players.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
18
User interface specifications
class NimTUI
A simple text-based user interface for
the simple nim system.
public NimTUI ()
Create a new user interface.
public void start ()
Start the interface.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
19
Initializing class
 The initiating class will look like this:
public class NimGame {
public static void main (String[] argv) {
(new NimTUI()).start();
}
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
20
Creation responsibilities
cre ate s
Pla ye r
cre ate s
N im G a m e
Ni m T UI
cre ate s
cre ate s
G am e
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
Pil e
21
Implementing class Pile
class Pile {
private int sticks;
public Pile (int sticks) {
this.sticks = sticks;
}
public int sticks () {
return sticks;
}
public void remove (int number) {
assert number <= sticks :
"precondition: number <= this.sticks()";
sticks = sticks - number;
}
}
public String toString () {
return "Pile: " + sticks + " sticks.";
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
22
Test-driven implementation of Player
 Stubbed implementation of Player.
class Player {
public Player (String name) {
}
public String name () {
return null;
}
public int sticksTaken () {
return 0;
}
public void takeTurn (Pile pile, int maxOnATurn) {
}
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
23
Test-driven implementation of Player
 Test initial state of the Player.
@Test
public void testInitialState () {
assertEquals("Player", player.name());
assertEquals(0, player.numberTaken());
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
24
Test-driven implementation of Player
 To satisfy initial state test, implement queries name,
sticksTaken and constructor.
private String name;
private int sticksTaken;
public Player (String name) {
this.name = name;
this.sticksTaken = 0;
}
public String name () {
return name;
}
public int sticksTaken () {
return sticksTaken;
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
25
Testing method takeTurn
 Method requires two arguments, a Pile and
maxOnATurn.
 Cases to consider testing:
 maxOnATurn is smaller than number of sticks in Pile;
 maxOnATurn is equal to the number of sticks in Pile;
 maxOnATurn is larger than number of sticks in Pile.
 Test boundaries of Pile size and maxOnATurn.
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
26
Testing method takeTurn
 we’ll test the following cases:
Pile size maxOnATurn
5
3
2
1
5
1
Dec 2007
3
3
3
3
1
1
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
27
Testing method takeTurn
 Include four Piles in the test fixture:
private Player player;
private Pile pile5;
// Pile
private Pile pile3;
// Pile
private Pile pile2;
// Pile
private Pile pile1;
// Pile
@Before
private void setUp () {
player = new Player("Player");
pile5 = new Pile(5);
pile3 = new Pile(3);
pile2 = new Pile(2);
pile1 = new Pile(1);
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
with
with
with
with
5
3
2
1
sticks
sticks
sticks
stick
28
Testing method takeTurn
/**
* Test the takeTurn method with maxOnATurn 1.
*/
@Test
public void testTakeTurnMax1 () {
player.takeTurn(pile5,1);
assertEquals(4, pile5.sticks());
assertEquals(1, player.numberTaken());
player.takeTurn(pile1,1);
assertEquals(0, pile1.sticks());
assertEquals(1, player.numberTaken());
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
29
Testing method takeTurn
/**
* Test the takeTurn method with maxOnATurn 3.
*/
@Test
public void testTakeTurnMax3 () {
player.takeTurn(pile5,3);
assertTrue(1 <= player.numberTaken() &&
player.numberTaken() <= 3);
assertEquals(pile5.sticks(), 5 - player.numberTaken());
player.takeTurn(pile3,3);
assertTrue(1 <= player.numberTaken() &&
player.numberTaken() <= 3);
assertEquals(pile3.sticks(), 3 - player.numberTaken());
player.takeTurn(pile2,3);
assertTrue(1 <= player.numberTaken() &&
player.numberTaken() <= 2);
assertEquals(pile2.sticks(), 2 - player.numberTaken());
player.takeTurn(pile1,3);
assertEquals(1, player.numberTaken());
assertEquals(0, pile1.sticks());
NH-Chapter 8: An Introduction To Programming And
Dec 2007
30
Object
Oriented
Design
Using
Java
}
Testing method takeTurn
 Simplest implementation of the method : always remove
one stick from the Pile.
public void takeTurn (Pile pile, int maxOnATurn) {
pile.remove(1);
sticksTaken = 1;
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
31
Implementing class Game
 Implementation easily follows from the specs:
class Game {
private
private
private
private
private
private
Dec 2007
static final int MAX_ON_A_TURN = 3;
Player player1;
Player player2;
Player nextPlayer;
Player previousPlayer;
Pile pile;
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
32
Implementing class Game
public Game (Player player1, Player player2,
int sticks) {
assert sticks > 0 :
"precondition: initial sticks > 0";
this.player1 = player1;
this.player2 = player2;
this.nextPlayer = player1;
this.previousPlayer = null;
this.pile = new Pile(sticks);
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
33
Implementing class Game
public int sticksLeft () {
return pile.sticks();
}
public Player nextPlayer () {
return nextPlayer;
}
public Player previousPlayer () {
return previousPlayer;
}
public boolean gameOver () {
return pile.sticks() == 0;
}
public Player winner () {
if (gameOver())
return otherPlayer(previousPlayer);
else
return null;
}
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
34
Implementing class Game
public void play () {
if (!gameOver()) {
nextPlayer.takeTurn(pile,MAX_ON_A_TURN);
previousPlayer = nextPlayer;
nextPlayer = otherPlayer(nextPlayer);
}
}
public String toString () {
return "Game with players: " + player1 + ", and “ +
player2;
}
private Player otherPlayer (Player player) {
if (player == player1)
return player2;
else
return player1;
}
}//end of Game implementation
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
35
Implementing the TUI
 The implementation is similar to those seen in chapter 7.
 The actual implementation is shown in the textbook
section 8.3.5
Dec 2007
NH-Chapter 8: An Introduction To Programming And
Object Oriented Design Using Java
36