Let's work out a sample program, using a data class
that we create in groups, to help us better grasp some of the notions related
to these instantiable classes. Our goal today will be to create a
Player
data class. This class will hold data related to baseball players, specifically,
enough information to calculate a players batting average, and print out a
message that displays this information.
The formula for calculating a batting average requires three pieces of information:
- The number of times a player has batted (a good name might be atBats);
- The number of hits a player has (hits);
- The number of times the player has walked (walks).
The formula is then given by
battingAverage = 1.0 * hits / (atBats - walks)
where the division is double division, not integer division.
We'll also use a String data field to hold the players name. Since players
rarely change their names, we can make that data field a constant, and declare
it as public final.
- Create a new project named battingAverages. Create a new data class
named Player and a new driver class named PlayerDriver. Add the javabook
folder to this projects folder.
- In the data class, declare the data fields name, atBats, hits, and
walks, as described above, the first is public and final, while the others
should all be private.
- Here is a constructor with minimal inputs (since the name field
is a constant, it must be set to a reasonable value originally, so a no input
constructor cannot be defined here).
public Player( String myName )
{
name = myName;
atBats = 1;
hits = walks = 0;
}
type this constructor into your data class and add another constructor that
has the declaration line below.
public Player( String somePlayer, int numAtBats, int numHits, int
numWalks )
- Write a private method named calculateAverage(), with
no inputs, that returns a double, which is equal to the player's batting
average (see the formula above).
- Write a public method named printMe() that returns
a String consisiting of something like:
Player: Sammy Sosa, Batting
Average: .32876543
using the data fields name and the calculateAverage() method.
- Compile the data class and remove all syntax errors.
- In the Driver class, import javabook.*, create a MainWindow, an InputBox,
an OutputBox, and show() the MainWindow and OutputBoxes.
- Ask the user for a player's name, number of at bats, hits, and walks,
and then call the second constructor of the Player class to create a player,
named player1, from their data. You will have to declare variables to hold
all of these values.
- Using your output box, try to print player1's name, and number of at
bats directly, using player1.name and player1.atBats. Notice
that the private field causes an error, but the public field does not.
- Print out the batting average of player1 by calling the printMe()
method of that object, inside an output box, printLine() method
call.
- Add mutator and accessor methods named setAtBats, setHits, and setWalks
for the mutators and getAtBats, getHits, and getWalks for the accessors,
to the data class. These should be public.