Figure 10-15 depicts the checking code for the addPlayer() method of Tournament. Write the checking code for the other constraints associated with Tournament depicted in Figure 9-16.
What will be an ideal response?
Figure 10-10 depicts the checking code for Tournament.removePlayer(). The key points are that
• preconditions are checked first,
• some values may have to be saved for checking postconditions before the real work is done,
• postconditions and invariants are checked after the real work is done, and
• the conditions checked (i.e., the conditions under which an exception should be raised) are the opposite of the
conditions stated in the contracts (i.e., the conditions that should always be true).
```
public class Tournament {
//...
private List players;
public void removePlayer(Player p) throws UnknownPlayer, KnownPlayer,
IllegalNumPlayers, IllegalLeague, IllegalName
{
// check precondition isPlayerAccepted(p)
if (!isPlayerAccepted(p)) {
throw new UnknownPlayer(p);
}
// check precondition getNumPlayers() < maxNumPlayers
if (getNumPlayers() == getMaxNumPlayers()) {
throw new TooManyPlayers(getNumPlayers());
}
// save values for postconditions
int pre_getNumPlayers = getNumPlayers();
// accomplish the real work
players.add(p);
p.addTournament(this);
// check post condition !isPlayerAccepted(p)
if (isPlayerAccepted(p)) {
throw new KnownPlayer(p);
}
// check post condition getNumPlayers() = @pre.getNumPlayers() - 1
if (getNumPlayers() != pre_getNumPlayers - 1) {
throw new IllegalNumPlayers(getNumPlayers());
}
// check invariant maxNumPlayers > 0
if (getMaxNumPlayers() <= 0) {
throw new IllegalMaxNumPlayers(getMaxNumPlayers());
}
// check invariant getNumPlayers() <= getMaxNumPlayers()
if (getNumPlayers() > getMaxNumPlayers()) {
throw new IllegalNumPlayers(getNumPlayers());
}
// check invariant getLeague() != null
if (getLeague() == null) {
throw new IllegalLeague();
}
// check invariant getName() != null
if (getName() == null) {
throw new IllegalName();
}
}
//...
}
```
Figure 10-10 Checking code for removePlayer()
You might also like to view...
What is an object in an object-oriented program? How can an object communicate with another object in such a program?
What will be an ideal response?
Validation ________ helps the data entry person know why the entry is being rejected
Fill in the blank(s) with correct word
Match the following terms to their meanings:
I. The path to the selected file or folder is displayed here A. TIFF II. File type used for very high level of visual quality B. tag III. Descriptive pieces of information about a file C. Address Bar IV. A custom file property D. Documents folder V. Default storage location for files used by many programs E. Properties
Which of the following statements about block comments is true?
A. Block comments are a form of documentation B. Block comments must be coded on a single line C. Block comments start with a // token D. Block comments must start in column 1 E. Block comments specify a library file that is to be loaded by the preprocessor