Java Reference
In-Depth Information
int getAtBats();
This interface tells us that we can get the name of a player, get the number of times the player
has been to the plate, and record an at-bat without worrying about having to deal with the new
exception. But if we want to know the total bases, the average, the on-base percentage, or the
slugging percentage for the player, we need to be ready to be told that the player has not been
up enough to make such a statistic meaningful.
We can see the effect of this if we look at the code that we wrote to test implementations of the
Batter interface. Suppose we know that the minimum number of at-bats required for mean-
ingful statistics was 10 (if we were being really careful, we would define this in some static
variable, but this is a chapter on exceptions, so we will just use a constant). Then, our test code
might contain something like:
package examples;
* Test that the slugging average is calculated correctly
* with respect to the at-bats and the total number of bases.
* This method assumes that an array of objects of type batter
* have been generated by the setUp method.
public void testGetSlugging() {
float testAvg; calcAvg;
for (int i = 0; i < testBatters.length; i++){
testAvg = testBatters[i].getSlugging();
calcAvg = (float)testBatters[i].getTotalBases() /
Assert.assertEquals("Slugging test", testAvg, calcAvg, .02);
} catch (NotEnoughAtBatsException e){
Assert.assertTrue("Slugging exception",
(10 == (e.getNeeded() + testBatters[i].getAtBats()));
Search WWH ::

Custom Search