Java Reference
In-Depth Information
testBatters = new BatterImpl[100];
for (int i = 0; i < 100; i++) {
testBatters[i] = new BatterImpl();
initBatter(testBatters[i]);
}
System.out.println("Test setup successfully complete");
}
/**
* Initialize the test cases that will be used for these tests. Each object
* will be given a random number of at-bats ranging from 0-100; if the
* number of at-bats is less than 10, we increment the number by 10 to
* ensure that each test case has the minimum number of at-bats. Then each
* at-bat will be given a random result, which will be recorded by a call to
* the {@link BatterImpl.atBat()} method.
*
* @param batterImpl
* The batter object being initialized
*/
private void initBatter(BatterImpl batter) {
int numbats;
HashMap<Integer, AtBatResult> resultsTable
= new HashMap<Integer, AtBatResult>();
resultsTable.put(0, AtBatResult.
strikeOut
);
resultsTable.put(1, AtBatResult.
fieldOut
);
resultsTable.put(2, AtBatResult.
base1
);
resultsTable.put(3, AtBatResult.
base2
);
resultsTable.put(4, AtBatResult.
base3
);
resultsTable.put(5, AtBatResult.
base4
);
resultsTable.put(6, AtBatResult.
walk
);
resultsTable.put(7, AtBatResult.
reachOnError
);
resultsTable.put(8, AtBatResult.
sacrifice
);
numbats = dataGen.nextInt(100);
if (numbats < 10)
numbats += 10;
for (int i = 0; i < numbats; i++) {
batter.atBat(resultsTable.get(dataGen.nextInt(9)));
}
}
This will give us an array of 100 batter objects that we can use to test all of our methods that
return batting statistics. We randomly assign a number of at-bats for each batter, but make sure