Create a Diver java class per the following Diver uml diagram: In a diving compe
ID: 3575548 • 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.
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
Explanation / Answer
package diver;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
public class Diver {
final static String filename = "C:\Users\ttechnocraft\Videos\FILENAME.txt";
private String diverName;
private float finalTotal;
public int calculateTotalScore(String k,float[] arr){
diverName=k;
finalTotal=0;
int n = arr.length;
float temp = 0;
//function for sorting the array so that smallest and lowest number can
//be removed from finalTotal
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
for(int i=1;i<n-1;i++){
finalTotal=finalTotal+arr[i];
}
System.out.println(""+diverName+" - "+String.format("%.2f", finalTotal));
return 0;
}
public static void main(String[] args) {
float[] arr=new float[72];
String name = null;
List nameList=new ArrayList();
Scanner scan = null;
File f = new File(filename);
try {
scan = new Scanner(f);
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
System.exit(0);
}
int total = 0;
boolean foundFloat = false; //flag to see if there are any integers
int i=0;
while (scan.hasNextLine()) {
//gets the complete line from file into currentLine
String currentLine = scan.nextLine();
//split into words
String words[] = currentLine.split(" ");
//For each word in the line
for(String str : words) {
try {
name = str.replaceAll("[^A-Za-z]+","");
if(name != null && !name.isEmpty()){
nameList.add(name);
}
float num = Float.parseFloat(str);
foundFloat = true;
if(foundFloat==true){
arr[i]=num;
i++;
}
}catch(NumberFormatException nfe) { };
}
}
if(!foundFloat)
System.out.println("No numbers found.");
scan.close();
Diver d1=new Diver();
Diver d2=new Diver();
Diver d3=new Diver();
Diver d4=new Diver();
Diver d5=new Diver();
Diver d6=new Diver();
Diver d7=new Diver();
Diver d8=new Diver();
Diver d9=new Diver();
String nm="";
float tempArr[]=new float[8];
nm = (String) nameList.get(0);
nm = nm +" " +(String) nameList.get(1);
for(int z=0;z<8;z++){
tempArr[z]=arr[z];
}
d1.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(2);
nm = nm +" " +(String) nameList.get(3);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+8];
}
d2.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(4);
nm = nm +" " +(String) nameList.get(5);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+16];
}
d3.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(6);
nm = nm +" " +(String) nameList.get(7);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+24];
}
d4.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(8);
nm = nm +" " +(String) nameList.get(9);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+32];
}
d5.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(10);
nm = nm +" " +(String) nameList.get(11);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+40];
}
d6.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(12);
nm = nm +" " +(String) nameList.get(13);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+48];
}
d7.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(14);
nm = nm +" " +(String) nameList.get(15);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+56];
}
d8.calculateTotalScore(nm,tempArr);
nm = (String) nameList.get(16);
nm = nm +" " +(String) nameList.get(17);
for(int z=0;z<8;z++){
tempArr[z]=arr[z+64];
}
d9.calculateTotalScore(nm,tempArr);
}
}
*************************************output********************************
Chen Ruolin - 56.90
Emilie Heymans - 56.70
Wang Xin - 56.70
Paola Espinosa - 56.10
Tatiana Ortiz - 56.10
Melissa Wu - 56.30
MarieEve Marleau - 56.20
Tonia Couch - 55.70
Laura Wilkinson - 56.40