Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Diver java class per the following Diver uml diagram: In a diving compe

ID: 3834004 • Letter: C

Question

Create a Diver java class per the following Diver uml diagram:

In a diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that reads the provided data file formatted as depicted in the following table. For each row of data create an instance of a Diver object from that row of data. Output each diver's name, all of her scores and a total score using the above scoring rules. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen Ruolin – 56.90 points.

Diver

Score 1

Score 2

Score 3

Score 4

Score 5

Score 6

Score 7

Score 8

Chen Ruolin

9.2

9.3

9

9.9

9.5

9.5

9.6

9.8

Emilie Heymans

9.2

9.2

9

9.9

9.5

9.5

9.7

9.6

Wang Xin

9.2

9.2

9.1

9.9

9.5

9.6

9.4

9.8

Paola Espinosa

9.2

9.3

9.2

9

9.5

9.3

9.6

9.8

Tatiana Ortiz

9.2

9.3

9

9.4

9.1

9.5

9.6

9.8

Melissa Wu

9.2

9.3

9.3

9.7

9.2

9.2

9.6

9.8

Marie-Eve Marleau

9.2

9.2

9.2

9.9

9.5

9.2

9.3

9.8

Tonia Couch

9.2

9

9.1

9.5

9.2

9.3

9.4

9.6

Laura Wilkinson

9.7

9.1

9.3

9.4

9.5

9.4

9.6

9.2

Your program must read the data in from the provided data file, the attached ReadFile.java file shows the logic for reading each row of data. Once all the data has been read and the Diver instances created (and saved). Output each Diver's name, all her scores and the calculated total score for that Diver. Where total points is calculated based on the scoring rule defined above.

A couple of points regarding the above uml diagram:

addScore(double pScr) // is a convenience method that adds a single score to the scores ArrayList as a time.

calculateTotalScore() //is another convenience method that calculates a Diver's total score according to the rules below regarding how the total score is arrived at.

toString() //build's a string consisting of the Diver's first and last name and all the Diver's individual scores contained in the scores ArrayList and the Diver's resulting total score.

Either a main method may be added to the Diver class for testing or a separate program may be written. As a row of data is read an Instance of a Diver object needs to be created, it's data fields populated and the Diver object needs to be added to a separate ArrayList. For each Diver output the diver's name and total score using the provided scoring rules. Each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen Ruolin – 56.90 points.

import java.io.*;
import java.util.*;

public class readFile {
static Scanner infile = null;

public static void main(String[] args) throws IOException {

infile = new Scanner(new FileReader("diving_data.txt"));

        String str = null;
        double score = 0.0;
        //I know there are 9 lines of data
        for(int l=0; l<9; l++) {
   //System.out.println("The data from line " + l);
   str = infile.next();
   System.out.print(str + " ");
   str = infile.next();
   System.out.print(str + " ");
   //I know there are 8 scores per diver
   for(int s=0; s<8; s++) {
    score = infile.nextDouble();
       System.out.print(score + " ");
   }
   System.out.println();
}

}

}

Divingdata.txt

Diver

Score 1

Score 2

Score 3

Score 4

Score 5

Score 6

Score 7

Score 8

Chen Ruolin

9.2

9.3

9

9.9

9.5

9.5

9.6

9.8

Emilie Heymans

9.2

9.2

9

9.9

9.5

9.5

9.7

9.6

Wang Xin

9.2

9.2

9.1

9.9

9.5

9.6

9.4

9.8

Paola Espinosa

9.2

9.3

9.2

9

9.5

9.3

9.6

9.8

Tatiana Ortiz

9.2

9.3

9

9.4

9.1

9.5

9.6

9.8

Melissa Wu

9.2

9.3

9.3

9.7

9.2

9.2

9.6

9.8

Marie-Eve Marleau

9.2

9.2

9.2

9.9

9.5

9.2

9.3

9.8

Tonia Couch

9.2

9

9.1

9.5

9.2

9.3

9.4

9.6

Laura Wilkinson

9.7

9.1

9.3

9.4

9.5

9.4

9.6

9.2

Diver first Name: String lastName: String scores ArrayList Diver Diver(fNme String, INme String) set FirstName(fNme String) void getFirstName() String setLastName (INme String) void get LastName(): String setScores(scrs ArrayList) void getScores ArrayList addScore(scr double) void toString() String

Explanation / Answer

Driver.java

package driver;

import java.text.DecimalFormat;
import java.util.List;

public class Driver {

   private String firstName;
   private String lastName;
   private List<Double> scores;

   public Driver() {

   }

   public Driver(String firstName, String lastName, List<Double> scores) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.scores = scores;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public List<Double> getScores() {
       return scores;
   }

   public void setScores(List<Double> scores) {
       this.scores = scores;
   }

   public void addScore(double score) {
       scores.add(score);
   }

   public String toString() {
       return firstName + " " + lastName + " " + scores;
   }

   //calculates the total score excluding highest and lowest
   public double calculateTotalScore() {
       double total = 0;
       // To exclude first and last from the calculation as the scores are
       // already sorted consider i as 1 which excludes highest and upto
       // size-1 which excludes lowest
       for (int i = 1; i < scores.size() - 1; i++) {
           total += scores.get(i);
       }
       DecimalFormat df = new DecimalFormat("0.00");
       return Double.parseDouble(df.format(total));
   }

}

ReadFile.java---Contains main method to execute

package driver;

import java.io.*;
import java.util.*;

public class ReadFile {

   public static void main(String[] args) throws IOException {
       //create file by passing file file path
       File dataFile = new File("drivingdata.txt");
       //list of drivers with sorted scores
       List<Driver> drivers = getDriverDetails(dataFile);
       //Print the details of driver name and total score excluding highest and lowest scores
       for (Driver driver : drivers) {
           System.out.println(driver.getFirstName() + " " + driver.getLastName() + " " + driver.calculateTotalScore());
       }

   }
   //This method reads file data and saves to Driver objects
   public static List<Driver> getDriverDetails(File file) {
       //list of driver objects to store data from the file
       List<Driver> drivers = new ArrayList<Driver>();
       try {
           //Scanner object to read the data from the file
           Scanner infile = new Scanner(file);

           // (I know there are 9 lines of data)
           // Making generic, it works based on number of lines available

           while (infile.hasNextLine()) {
               //scores list to store the list of scores for each driver
               List<Double> scores = new ArrayList<Double>();
               //First string in the line of the file is considered as firstName
               String firstName = infile.next();
               //Second string in the file is considered as lastName
               String lastName = infile.next();

               // I know there are 8 scores per diver
               for (int s = 0; s < 8; s++) {
                   double val = infile.nextDouble();
                   scores.add(val);
               }
               //sort the scores
               Collections.sort(scores);
               //create driver object for each line using the data in each line
               Driver driver = new Driver(firstName, lastName, scores);
               //add driver object to the list
               drivers.add(driver);
           }
           //close the scanner
           infile.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }

       return drivers;
   }
}

Sample Output

Chen Ruolin 56.9
Emilie Heymans 56.7
Wang Xin 56.7
Paola Espinosa 56.1
Tatiana Ortiz 56.1
Melissa Wu 56.3
Marie-Eve Marleau 56.2
Tonia Couch 55.7
Laura Wilkinson 56.4