The objectives of this lab are to reinforce and help teach:
In this lab you will extend and change the program you developed in Lab 6 (here), this time, making your updated program additionally print out the scores of the top 3 dives (the three dives with the highest overall scores).
Your updated program must use an array of doubles to hold the overall scores of each dive. Remember, the processDives method contains the loop for reading each dive from a line in the input file and then getting each calculated dive score. Therefore, you should declare the array in the processDives method but only after that method knows the number of lines in the DiveData.txt file.
In order to dimension your array, the processDives method will first need to know the number of elements in the array (which is the number of lines in the DiveData.txt file). Hint #1: A new method that is passed a parameter of name of the file as input and internally declares a file scanner and uses said scanner to loop through the file to figure out the number of lines and returns that number of lines would be very helpful; e.g. consider the following method prototype:
/**
* numLinesInFile - Get the number of lines in a specified file
* @param filename - The name of a data file in the current directory
* @return - (int) The number of lines in the file specified
* @throws FileNotFoundException
*/
public static int numLinesInFile(String filename) throws FileNotFoundException {
int numLines = 0;
// YOUR CODE GOES HERE (remove this comment and the two below):
// Declare the File scanner, loop through the lines in the file counting the
// number of lines (thing cummulative algo).
return numLines;
}
The method above will prove useful as "boiler plate code" in future projects and labs. YOU WILL REUSE THIS METHOD IN THE FUTURE.
The numLinesInFile method above will be called from processDives, which means that the processDives method will need to know the input file name. Currently that file name is only known to the main method. To address this need, the main method should be updated and the processDrives method should be changed so that its single input parameter is the name of the file to process (not a file Scanner for reading the file.) That, of course, will mean you have to update processDrives code within the method to now declare the file Scanner, just as you did in the main method prior (cut/paste from main). The new main method should be as follows:
public static void main(String[] args) throws FileNotFoundException {
printIntro();
processDives("DiveData.txt");
}
Your updated program should use array mechanisms (not brute force code) to figure out the top three dives.
Hint: Please note that the Arrays (plural) class and its .sort() method will likely come in handy for that purpose, as well the .length property of the array object holding the scores. Use Google to learn more about the Arrays class and the .length property of an array.
Other than the updates described above, your program should adhere to the original specification in the assignment writeup. Below are updated input and output specifications for your consideration and use.
Other than the completed numLinesInFile method and the updated main method, your updated program will only have approximately 6 additional lines of code and those lines will exclusively be in the processDives method, as follows:
Following are the instructions and you should follow them carefully:
Your program will once again read the DiveData.txt file from the current directory. The format of each line of the file is the same as the original specification. Your program can safely assume there will be at least three (3) lines of data in the file, but there could be any number.
For example, the following lines contain data for eight (8) dives:
1 3.5 5.5 6.0 7.0 6.5 6.5 5.5 7.5
2 2.0 8.0 8.5 8.5 9.0 8.0 8.5 8.0
3 3.5 6.5 7.5 8.0 6.5 7.0 6.5 7.5
4 3.0 5.5 6.5 8.5 6.0 6.0 5.5 6.0
5 3.5 5.0 7.5 9.0 7.0 5.0 4.5 5.0
6 4.0 7.5 8.0 9.5 8.5 6.5 5.5 8.5
7 3.5 6.5 9.0 8.5 8.0 7.5 6.5 9.0
8 2.5 7.0 6.5 7.5 7.0 8.0 6.5 7.0
Output an introductory message and then the scores for each dive.
Your program should output precisely according to the format below (wording, spacing, punctuation, precision of doubles, etc).
Welcome to the Diver Scoring program. This program will calculate an
overall score for a diver, based on individual dives.
The diver's score for dive 1 is 66.15.
The diver's score for dive 2 is 49.80.
The diver's score for dive 3 is 73.50.
The diver's score for dive 4 is 54.00.
The diver's score for dive 5 is 61.95.
The diver's score for dive 6 is 93.60.
The diver's score for dive 7 is 82.95.
The diver's score for dive 8 is 52.50.
The average score for these dives is 66.81.
The top three scores are: 93.60, 82.95, and 73.50.
This lab must be AutoLab submitted to the instructor by 11:59pm on the Monday a week after it was assigned. Check AutoLab for the specific due date.