For this assignment, you will work with and extend a class that keeps score for a basketball team. The HoopsScorer
class has methods for adding points to a team's score and accessing that score.
HoopsScorer
object and test the recordMadeShot
and getScore
methods.recordMadeShot
method will take as input? Can you enter a number greater than three? Can you enter zero or a negative number? How are these inputs handled by the method?
recordMadeShot
, such as 1.5 or "foo"?
recordMadeShot
method so that it checks to make sure that the number of points must be an integer between 1 and 3. Do so by replacing the one statement inside the method with the following if-else statement:
if (numPoints >= 1 && numPoints <= 3) { this.score += numPoints; } else { System.out.println("Illegal number of points."); }Test your modified function to make sure that it disregards illegal point values.
/** * Resets the score. */ public void reset() { this.score = 0; }Test your new method to make sure it behaves as it should.
reset
method now contain the same code (a single assignment statement). That is because starting and resetting a HoopsScorer
are essentially the same task - setting the score field to zero. While having two methods that do the same thing is perfectly legal, it is also redundant (and redundancy in code is generally bad). Modify the constructor so that, instead of initializing the field itself, it calls the reset
method to perform the task. That is, replace the assignment statement in the constructor with the method call:
this.reset();
ScoreBoard
and call its showBoard
method to show the score board GUI.
Note that the ScoreBoard
actually creates two HoopsScorer
objects, one for the home team and one for the visiting team.
HoopsScorer
class so that it also records missed shots and is able to report the shooting percentages for a team. In order to accomplish this, you will need make several changes and additions to the existing class.
reset
method (and thus indirectly initialized by the constructor). You will also need to modify the recordMadeShot
method so that the new fields are updated appropriately. Be sure to test your additions/modifications thoroughly before moving on to the next step.recordMissedShot
method that looks similar to the existing recordMadeShot
method, in that it will have a parameter that specifies which type of shot was missed. When called, it will update the appropriate field(s) so that a missed shot of that type is recorded.getPercentage
method that similarly has a parameter specifying the type of shot. The method should calculate and return the shooting percentage (100.0*madeShots/totalShots) for that type of shot, rounded to the nearest integer. For example, if a team took 6 3-point shots and made only 2 of them, then a call to getPercentage
with input 3 should return 33. If a team has not taken any shots of a specific type, the shooting percentage should be reported as 0.
Hint: To round a number to the nearest integer, you will want to make use of the built-in Math.round
method. This method takes a single number as input and returns the whole number closest to that number. Oddly enough, it returns it as a double
, so the call Math.round(33.3)
will actually return 33.0. To cast that into a value of type int, place "(int)
" in front of the call. For example, (int)Math.round(33.3)
will return the int value 33.
getFGPercentage
that has no parameters. This method should return the combined shooting percentage of all field goals (2- and 3-pointers), again rounded to the nearest integer. For example, if a team made 4 of 6 2-pointers and 1 of 4 3-pointers, their overall field goal percentage should be reported as 50.
Once you have tested methods, download the StatBoard GUI class that makes use of the extended functionality of HoopsScorer
to display shooting percentages as well as scores.