Java Reference
In-Depth Information
* not be thrown for this batter.
* @return an integer showing the number of additional at-bats
* needed
*/
public int getNeeded(){
return atBatsNeeded;
}
}
Now we need to change our Batter interface to use the exception. Not all of the methods
in that interface need to be declared to throw the exception; there are some measures that
are reasonable without waiting for some minimum number of at-bats. So the result looks
something like:
package examples;
/**
* An interface that defines the notion of a batter
* in a baseball statistics package. Each at-bat will
* be recorded for the hitter, along with the result of
* that at-bat. Running totals of the important statistics
* will be available.
*
*/
public interface Batter {
/**
* The possible results of an at-bat for the hitter.
*
*/
enum AtBatResult {
strikeOut, fieldOut, base1, base2, base3, base4, walk,
reachOnError, sacrifice
}
/**
* Return the name for this batter. Note that this
* interface does not define how the name is set;
* this will be done when the object implementing this
* interface is created and cannot be changed.
* @return The name of the batter, as a string
*/
String getName();
/**
* Method used to record an at-bat. The result
Search WWH ::




Custom Search