Guest User

Untitled

a guest
Apr 24th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Dive {
  5.  
  6.     public static void main(String[] args) throws FileNotFoundException {
  7.         printIntro();
  8.  
  9.         Scanner fileScanner = new Scanner(new File("DiveData.txt"));
  10.  
  11.         processDives(fileScanner);
  12.     }
  13.  
  14.     public static void printIntro() {
  15.         System.out
  16.                 .println("Welcome to the Diver Scoring program."
  17.                         + "\nThis program will calculate an overall score for a diver, based on individual dives.");
  18.     }
  19.  
  20.     public static void processDives(Scanner fileScanner) {
  21.         double avg = 0;
  22.         int count = 0;
  23.  
  24.         while (fileScanner.hasNext()) {
  25.             int diveNumber = fileScanner.nextInt();
  26.             String diveLine = fileScanner.nextLine();
  27.             double score = calculateDiveScore(diveLine);
  28.             avg += score;
  29.  
  30.             System.out.printf("The diver's score for dive " + diveNumber
  31.                     + " is " + "%.2f. \n", score);
  32.  
  33.             count++;
  34.             diveNumber = 0;
  35.         }
  36.         System.out.printf("\nThe average score for these dives is " + "%.2f.",
  37.                 avg / (double) count);
  38.     }
  39.  
  40.     public static double calculateDiveScore(String diveLine) {
  41.  
  42.         diveLine = diveLine.substring(1);
  43.         String[] fields = diveLine.split(" ");
  44.         double difficulty = Double.parseDouble(fields[0]);
  45.         double[] scores = new double[fields.length - 1];
  46.         double max = -500.0, min = 500.0, score = 0;
  47.  
  48.         for (int i = 0; i < fields.length - 1; i++) {
  49.             scores[i] = Double.parseDouble(fields[i + 1]);
  50.         }
  51.  
  52.         for (int i = 0; i < scores.length; i++) {
  53.             if (Math.max(scores[i], max) == scores[i])
  54.                 max = scores[i];
  55.             if (Math.min(scores[i], min) == scores[i])
  56.                 min = scores[i];
  57.             score += scores[i];
  58.         }
  59.  
  60.         score -= max;
  61.         score -= min;
  62.  
  63.         return score * difficulty * 0.6;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment