CSC 222: Object-Oriented Programming
Spring 2012

HW 7: Inheritance

Note: no late submissions will be accepted for this assignment.

This assignment involves using inheritance to calculate statistics on a baseball or softball team. The BallPlayer class is provided for you, which encapsulates basic information about a baseball/softball player (uniform number, first and last names, and position). In addition, the TeamStats class is able to read in a roster of players from a file and display that roster.

PART 1: Creating a GUI

Create a GUI that allows the user to specify a file containing baseball/softball player info (roster.txt is provided as an example). At the click of a button, the contents of that file should be read into a TeamStats object and the results of a call to the roster method displayed in a text area. For example:

PART 2: Extending BallPlayer

Typically, different performance statistics are kept for players depending on whether they are batters (i.e., position players) or pitchers. For batters, batting average, number of home runs, runs scored, and runs batted in are among the statistics commonly reported. However, pitchers rarely have these offensive statistics reported. Instead, statistics such as earned run average, walks and hits allowed, wins, and saves are reported. The Batter class extends BallPlayer to include batting statistics (number of hits and number of at bats).

  1. Similarly, define a class named Pitcher that extends BallPlayer to include pitching statistics (number of earned runs allowed and number of innings pitched). Note that the number of innings pitched may include fractions of innings, so must be represented as a real value. Your class should have methods for accessing the number of earned runs and innings pitched, and also for calculating the earned run average. Earned run average (ERA) is the average number of runs allowed per nine innings pitched. For example, if a pitcher allowed 3 earned runs in 12 innings, then his/her ERA would be 9*(3/12) = 2.25. As was the case with the Batter class, the toString method should return the basic player information along with the pitcher's ERA.
  2. Modify the TeamStats class so that it reads in a file of statistics, such as stats.txt. Each line contains the uniform number, name, and position of a player. If that player is a pitcher (i.e., position is "SP" or "RP"), then what follows are the number of earned runs and innings pitched for that player. If that player is a batter (i.e., anything but "SP" and "RP"), then what follows are the number of hits and at bats. The TeamStats constructor will need to read in the initial values on each line to determine the player's position, then construct the appropriate type of object (either Batter or Pitcher) and add it to the ArrayList. You should also add methods to TeamStats that calculate the team batting average and team earned run average. In order to do this, you will need to be able to determine the specific type of each object in the ArrayList using the instanceof operator. For example, BaseballPlayer player = ... if (player instanceof Batter) { Batter b = (Batter)player; ... } else if (player instanceof Pitcher) { Pitcher p = (Pitcher)player; ... }
  3. Modify your GUI so that it reads in the statistics file specified by the user and displays the team batting average (based on the total number of hits and at bats for all batters) and the team earned run average (based on the total number of earned runs and innings pitched for all pitchers).