/* Diver.java - keep track of each diver's information: * a name and 7 scores. * Eventually we want to be able to find the average score. */ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Diver { // attributes private String name; private double [] score; //private static final int NK_POSITION = 3; // default constructor // Here we read in the input file containing the name & 7 #'s. public Diver() throws FileNotFoundException { // read the file Scanner inFile = new Scanner (new FileInputStream("score.txt")); name = inFile.next(); // allocate space for the array before reading it. score = new double[7]; for (int i = 0; i < 7; ++i) { score[i] = inFile.nextDouble(); } } public double findAverage() { double sum = 0.0; for (int i = 0; i < 7; ++i) { sum = sum + score[i]; } return sum / 7.0; } // findAverageWithoutHiLo - find the average of all the diver's // scores, but with the highest and lowest scores taken out. public double findAverageWithoutHiLo() { double sum = 0.0; for (int i = 0; i < 7; ++i) { sum = sum + score[i]; } // Subtract away the highest and lowest scores. sum -= findMaxScore(); sum -= findMinScore(); return sum / 5.0; } // findMaxScore - go thru the array and return the largest number // you find. public double findMaxScore() { double max = score[0]; for (int i = 1; i < 7; ++i) { if (score[i] > max) max = score[i]; } return max; } // findMinScore is analogous public double findMinScore() { double min = score[0]; for (int i = 1; i < 7; ++i) { if (score[i] < min) min = score[i]; } return min; } // findAverageWithout3 - find the average of all the diver's scores // but not counting the score from judge [3] public double findAverageWithout(int nkPosition) { double sum = 0.0; for (int i = 0; i < 7; ++i) { //if (i == 3) // continue; sum = sum + score[i]; } // I've decided instead to just subtact that score. sum -= score[nkPosition]; return sum / 6.0; } }