Guest User

Untitled

a guest
Apr 24th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  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 "
  37.                 + "%.2f. \n", avg / (double) count);
  38.     }
  39.  
  40.     public static double calculateDiveScore(String diveLine) {
  41.         double score = 0.0;
  42.         String subNumbers = "";
  43.         double difficulty = Double.parseDouble(diveLine.substring(1, 4));
  44.         diveLine = diveLine.substring(5);
  45.         double max = -500, min = 500;
  46.  
  47.         for (int i = 0; i < diveLine.length(); i++) {
  48.  
  49.             // if we're getting something other than a space, add it to the
  50.             // string. We'll do the calculations while we have spaces.
  51.             if (diveLine.charAt(i) != ' ') {
  52.                 subNumbers += diveLine.charAt(i);
  53.             } else {
  54.                 double val = Double.parseDouble(subNumbers);
  55.                 // if the score is neither largest nor smallest, add it to total
  56.                 // score.
  57.                 if (Math.max(val, max) == max && Math.min(val, min) == min) {
  58.                     score += val;
  59.                 }
  60.  
  61.                 // if it is either largest or smallest, add previous largest or
  62.                 // smallest and set current to largest or smallest.
  63.                 if (Math.max(val, max) == val) {
  64.                     if (max != -500)
  65.                         score += max;
  66.                     max = val;
  67.                 } else if (Math.min(val, min) == val) {
  68.                     if (min != 500)
  69.                         score += min;
  70.                     min = val;
  71.                 }
  72.                 subNumbers = "";
  73.             }
  74.         }
  75.  
  76.         //check the last number to see if it's a max or a min
  77.         if (Math.max(Double.parseDouble(subNumbers), max) == Double
  78.                 .parseDouble(subNumbers)) {
  79.             score += max;
  80.         } else if (Math.min(Double.parseDouble(subNumbers), min) == Double
  81.                 .parseDouble(subNumbers)) {
  82.             score += min;
  83.         }
  84.  
  85.         return score * difficulty * 0.6;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment