CSCI 161 - Lab 7: Deeper Dives

Objectives

The objectives of this lab are to reinforce and help teach:

Overview

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:

  1. One line of code to declare the file scanner for reading the file as processDives should be updated to take as a parameter a string which is the name of the file, not a Scanner object as in lab 6.
  2. One line of code to call the new numLinesInFile method to get the number of lines and save as a int variable (before the while loop).
  3. One line of code to declare the array with the size from the instruction above (again, before the while loop).
  4. One line of code within the while loop to assign the dive score returned from calculateDiveScores to the approrpriate (next) array element.
  5. One line of code after the while loop to sort the array.
  6. One line of code (printf) after the while loop to print out the additional line of output with the top 3 scores.

Instructions

Following are the instructions and you should follow them carefully:

  1. Using Eclipse create a new Java Project and name it: Lab7
  2. As with all your labs and assignments, add a class of the same name, Lab7, and include in that class a main method.
  3. Your program should be based on (start with the code from) your Lab6 submission (see the "Overview" section above). If you did not properly complete Lab6, here is a solution.
  4. Your program must read a file according to the "Input Specification" below.
  5. Your program must output according to the "Output Specification" below.
  6. Hint: Your program will need to know how many lines are in the DiveData.txt file in order to size the double array accordingly when it is declared. For this, I suggest a method that is passed in the filename, returns an integer which is the number of lines of the file, and within the method declares a file scanner to read the file one line at a time while counting the lines within.
  7. Iteratively develop the assignment (get the original version working, test it, add code to one method, test, fix if needed, continue).
  8. When the lab is complete and working submit your Lab7.java file via AutoLab.

Input Specification

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 Specification

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.
        
        

Deadline

This lab must be submitted to the instructor by 11:59:59pm on Monday, November 6th.