PART I:
All files (java, test data, etc.) Java documentation
Let's review our object-oriented approach to programming and add a little bit of inheritance to it this time. We'll need a class Team to model a baseball team; League to model collection of Teams; a class Player for players; and two more classes, Pitcher and Hitter for specific types of players. Think about what the relationships between these classes should be. Unlike Major League Baseball, we will put all our teams in one League this.
Although we will have plenty of data on which statistics could be calculated, the emphasis of the project is on organization, not number-crunching: organizing the data among classes, getting the data into the right classes and being able to locate it; and organizing your classes and packages so that things compile and execute properly.
As data, we will use files with real information, derived from the Major League Baseball Statistics site (you will need to visit that site to know what the fields are). The information has already been extracted for you in the form of text files. Using a class we supply called DelimitedTextFile, you can read each line of a file into an ArrayList of Strings, one string for each field. Then you can build objects of the right types from that.
Here is an outline of the required processing:
- The League constructor should have one String parameter, which is the name of a file to be processed. This will be a file with information about teams. The League constructor should create Team objects and keep a list of them.
- The League class has several methods which read data files of
players,
and other methods which print information teams and players.
For details, see the Java interface named ILeague. You must
implement those methods (correctly and completely, of course!).
Each unique player and team that is read in should be modeled by a
separate object of the proper type.
- ILeague, and the classes you create, should be in a package named baseball.
- All classes should have suitable toString and compareTo
methods.
Teams are compared by team name (i.e., alphabetically). Players
are compared by name in the usual way (last name, first name).
toString for players should include team name or code, player name, and
position. toString for Hitters should include batting
average. toString for Pitchers should show ERA and games
won-lost. toString for teams should show won-loss record and
average.
- League should include a main method which causes the files to be
read in
and several test operations to be performed: enough to show that your
program basically works. Your main should not require any user
input.
- Be careful to use exactly the class and method names mentioned in
the assignment.
- Your League class must implement ILeague, and you must not modify ILeague (you won't be able to turn in a modified version).
- The filenames and paths you use in main must not be specific to your computer! Use relative names (or absolute URLs to the class web site). One possibility is to place your textfiles in a directory on your classpath (i.e. on the same level as the baseball and textfile packages, not inside those packages!) and they can be found regardless of what system you (or the TAs) are running on. if you aren't careful about this, a main that runs on your computer might explode on your TA's -- not a desirable situation!
- Don't
forget that Java standard library classes and methods are
allowable. For example, Arrays.sort and/or Collections.sort can
often be useful...
Hand in the files electronically
electronically by the due date at the top of this page.
In addition, write a short technical description (at most one page,
but a paragraph will probably do) of your program and how it works.
Turn this in (hardcopy) at the next lecture after the due date
above. As usual on any written part, unless you handwriting is
exceptionally clear,
please type it!
Note on the data files:
We have supplied a class called
DelimitedTextFile to make it easy to read the files. To use
it, construct a DelimitedTextFile passing the name of the file as a
parameter. Then the only method you will probably need is called
readLineAsFieldList, which reads a line, parses it into fields, and
gives you all the fields as a List. For example, the following
should actually compile and run:
DelimitedTextFile myfile = new DelimitedTextFile(aFileName);
List fields = myfile.readLineAsFieldList();
while (fields != null) {
//do something with the fields -- create an object, save data, whatever
fields = myfile.readLineAsFieldList();
}To find out what the fields are, visit the baseball site mentioned earlier and look at the original data. The files will have many more fields than you actually need for this assignment! There's no requirement to use or save all of that data in the objects you create. You may ignore the fields that are not going to be used. On the player files, a hitter's batting average is the number in the last column (marked AVG on the web site).
One thing missing from the files is a way to match a team name (like "Seattle Mariners", used on the team file), with the team code (like SEA, used in the player files). Somehow you should build this into your program so you can put players on the right team (if that matters). Do not modify the data file! If you do, your program won't work when the TA runs it with the original file.
Get started early and good luck!