CS 139 Algorithm Development

Baseball Statistics

In the last lab, you created a class that implemented "immutable" objects. An immutable object is an object whose state does not change after it is constructed (none of the non-constructor methods change any instance variables). In the Color139 class, all the methods (other than the constructor) created new Color139 objects; none of those methods changed the object they were being applied to (none changed this).

In this lab you will create mutable objects. Each object represents a baseball player's hitting statistics. Methods will allow additional statistics to be added into an object (as opposed to creating a new object with the modified state).

Background

Go Phillies      Go Yankees                       


Baseball is all about the statistics.  Some terms that will help you in this lab are defined below:
At bat: Each time a player has an opportunity to bat the ball in a baseball game is considered an at bat.  Each at bat can result in a single (reach one base successfully), double (two bases), triple (three bases), home run(4 bases), or an out.  A walk (where the pitcher pitches outside of the batter's "strike zone" on 4 pitches) is not considered an at bat (the batter did not have an opportunity to hit).
Batting average: The batting average is the number of hits / number of at bats.  So if a player was at bat 12 times and reached at least one base in 3 of those attempts, his batting average is (3 / 12) * 1000 or 250.   (Note, while the numbers are integers, you must calculate using double division, then change them into an integer.
Slugging percentage: The slugging percentage is the (number of bases reached / number of at bats) * 1000.  So for example, if the player above reached 1st base, 3rd base and 4th base in his 3 attempts, then he would have a slugging percentage of (8 / 12) * 1000 or 677.  
On base percentage: This statistic measures how effective the batter was at reaching at least first base and includes both hits (like the batting average) and walks.  This formula is ((number of hits + number of walks) / (number of at bats + number of walks)) * 1000.  So for our hypothetical batter if he had 2 walks in addition to his 12 at bats, his on base percentage would be ((3 + 2) / (12 + 2)) * 1000 or 357.
Please NOTE: All percentages are calculated using double division. In other words, in the example of on base percentage, you need to calculate 5/14 as 5.0/14. You may want to use a cast operation for that.

In this program, you will play with player statistics.  Each BaseStat object is one player.  You will also use the debugging technique where output lines are conditioned by a DEBUG constant.  If you want to see more detailed output, you turn it on and turn it off to just show the expected output.  The output will enable you to trace your code and find where you may be having errors.

Instructions

  1. Download a copy of the Main.java file. This implements the main function you will use to test your implementation of BaseStat.
  2. Download a copy of the BaseStat.java file. The documentation for the BaseStat class is here.
  3. Download a copy of the Util139.java file. The documentation for the Util139 class is here.
  4. Look at the parts of BaseStat that have already been defined.
  5. Implement the constructor method. Run your program and for the first input enter -1. Now set DEBUG in BaseStat to true and re-compile and re-run your program. Does it appear that the player object is being constructed correctly?
  6. Implement the other methods, one at a time. Rerun the program after implementing each method to test the implemented method. Make sure a method is working correctly before moving on to the next method. Experiment with BaseStat.DEBUG to see how turning on debugging can help show what your program is doing.
  7. When finished, submit to the Linux submit system.
Updated 11/15/10