Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Dive {
- public static void main(String[] args) throws FileNotFoundException {
- printIntro();
- Scanner fileScanner = new Scanner(new File("DiveData.txt"));
- processDives(fileScanner);
- }
- public static void printIntro() {
- System.out
- .println("Welcome to the Diver Scoring program."
- + "\nThis program will calculate an overall score for a diver, based on individual dives.");
- }
- public static void processDives(Scanner fileScanner) {
- double avg = 0;
- int count = 0;
- while (fileScanner.hasNext()) {
- int diveNumber = fileScanner.nextInt();
- String diveLine = fileScanner.nextLine();
- double score = calculateDiveScore(diveLine);
- avg += score;
- System.out.printf("The diver's score for dive " + diveNumber
- + " is " + "%.2f. \n", score);
- count++;
- diveNumber = 0;
- }
- System.out.printf("\nThe average score for these dives is " + "%.2f.",
- avg / (double) count);
- }
- public static double calculateDiveScore(String diveLine) {
- diveLine = diveLine.substring(1);
- String[] fields = diveLine.split(" ");
- double difficulty = Double.parseDouble(fields[0]);
- double[] scores = new double[fields.length - 1];
- double max = -500.0, min = 500.0, score = 0;
- for (int i = 0; i < fields.length - 1; i++) {
- scores[i] = Double.parseDouble(fields[i + 1]);
- }
- for (int i = 0; i < scores.length; i++) {
- if (Math.max(scores[i], max) == scores[i])
- max = scores[i];
- if (Math.min(scores[i], min) == scores[i])
- min = scores[i];
- score += scores[i];
- }
- score -= max;
- score -= min;
- return score * difficulty * 0.6;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment