ITEC 120
HW7: Reading GolferScores from a file (100 pts)

Academic Integrity

Be reminded that this homework assignment must be your own work.

Objectives

You will read and parse data from a file, and build an array of GolferScore objects. You will implement the compareTo method for GolferScores and sort the array of GolferScores.

Assignment

You'll write a program that will:

You'll write a class called TournamentScores which has an array of GolferScore as instance data. You'll write a driver, GolfFileDrv, which will create a TournamentScores object, and print it out.

DOWNLOAD: GolferScore.java, TournamentScores.java, GolfFileDrv.java, and scores.dat

The scores.dat file contains data in comma separated format. Here are the first 4 lines from that file:

Windy Knolls Open,50
Woody Acres Fairways,3,4,4,4,3,4,4,3,4,5,4,4,5,4,3,5,4,5
Meryl Beryl,4,6,5,5,1,3,3,2,3,6,3,2,3,4,3,5,3,5
Barney Fife,3,2,5,6,1,6,5,4,4,7,3,2,5,5,4,7,4,6

Line 1 contains the name of the tournament followed by the number of golfers who played in it.
Line 2 contains the name of the course followed by the pars for each hole.

The rest of the file contains one line per golfer who played in the tournament, containing the golfer's name followed by that golfer's score for each hole.

GolferScore class

This is the GolferScore class for HW6. For this assignment, start with this version of that program (rather than your own). You will add two methods to this file: another constructor, and the compareTo method. Details are in comments in the file.

The TournamentScores class

The TournamentScores class contains the tournament name and an array of GolferScores as instance data and has several methods which operate on that array.

The constructor

takes a filename as a parameter (scores.dat) and creates and initializes the array.

In order to create an array, you must first know how long you want it to be. That number is given to you on line 1 of the scores.data file. Be sure to use this number. This data file has 50 golfers, but, your program will be graded using a different file with a different number of golfers.

All of the file reading happens in this constructor.

The getName method

returns the name of the tournament.

The averageScore method

returns a double that is the average of all the golfers' scores.

The topThree method

returns a String that has "Top Three Golfers" followed by the GolferScore toString of the top three golfers. (In golf, low score wins.)

The toString method

returns a String of the tournament name followed by all the GolferScores in the array.

 

The driver - GolfFileDrv

creates a TournamentScores object and prints it out. After you have that working, remove that print statement and instead print the name of the tournament, the average score, and the top three golfers.

The final output for given data file would look something like this:

Windy Knolls Open
Average score is: 73.98

Top Three Golfers:

Eddie Haskell's score on Woody Acres Fairways
Hole:	1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18   Total
Par:	 3  4  4  4  3  4  4  3  4  5  4  4  5  4  3  5  4  5     72
Score:   2  4  2  2  3  2  2  2  4  5  2  2  7  6  2  3  6  6     62  10 under Par

Greta Garbo's score on Woody Acres Fairways
Hole:	1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18   Total
Par:	 3  4  4  4  3  4  4  3  4  5  4  4  5  4  3  5  4  5     72
Score:   1  4  4  3  2  3  3  2  4  3  6  3  6  2  1  6  5  5     63  9 under Par

Frederick "Fez" Partin's score on Woody Acres Fairways
Hole:	1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18   Total
Par:	 3  4  4  4  3  4  4  3  4  5  4  4  5  4  3  5  4  5     72
Score:   2  6  4  2  1  2  2  3  5  5  5  3  4  6  3  5  2  4     64  8 under Par

Sorting your array

Get your file reading to work first. Complete the constructor and toString in TournamentScores, and run your driver (GolfFIleDrv) to see that the data is being read and written correctly. AFTER you get that to work, then work on getting the top three golfers. You could write a lot of logic in your topThreeGolfers method to figure that out, but, wouldn't it be a lot easier if you could somehow magically sort your array? Then you would know that the first three in the array were the top three golfers.

There is a very handy method called Arrays.sort which you can use to sort your array. To use it, you must write the compareTo method, and certify that you've done so by adding

implements Comparable <GolferScore>

to the header of the class where the compareTo method lives (which is the GolferScore class). So the header would look like this:

By writing a very simple method that defines how to compare two golf scores, you can take advantage of code that has already been written to sort a whole array of objects! Powerful stuff.

You might as well sort your array of GolfScores right when you create it - in the constructor of TournamentScores. You will need to import java.util.Arrays at the top of the TournamentScores class.

 

Submit Your Assignment

Submit your GolferScore.java, TournamentScores.java, and GolfFileDrv.java file to the HW7 Assignment dropbox on D2L.