/* Lesley Hogan * June 30th 2006 * Triathlon.java * Calculate Ironman triathlon equivalents from normal triathlon times * CS 101 HW J1: * http://www.cs.virginia.edu/~asb/teaching/cs101-spring06/hws/hwj1/index.html */ package net.bunnie.uva.cs101; import java.util.*; public class Triathlon { public static void main (String[] args) { final double KILOMETERS_PER_MILE = 1.609344; final double OLYMPIC_SWIM_LENGTH_KM = 1.5; final double OLYMPIC_BIKE_LENGTH_KM = 40; final double OLYMPIC_RUN_LENGTH_KM = 10; final double IRONMAN_SWIM_LENGTH_KM = 3.8; final double IRONMAN_BIKE_LENGTH_KM = 180; final double IRONMAN_RUN_LENGTH_KM = 42.195; //legend System.out.println("Ironman Triathlon: how long will it take you?\n"); //gather data Scanner stdin = new Scanner (System.in); System.out.println("Your Olympic Triathlon times in hours, please:"); System.out.print("Swim: "); double olympicSwimTime = stdin.nextDouble(); System.out.print("Bike: "); double olympicBikeTime = stdin.nextDouble(); System.out.print("Run: "); double olympicRunTime = stdin.nextDouble(); System.out.print("Your Olympic Triathlon times: "); System.out.print("Swim, " + olympicSwimTime + "; "); System.out.print("Bike, " + olympicBikeTime + "; "); System.out.print("Run, " + olympicRunTime + "\n\n"); //displays 1.0, 2.0, 1.0 //computations double kilometerSwimRate = olympicSwimTime * 60 / OLYMPIC_SWIM_LENGTH_KM; double kilometerBikeRate = olympicBikeTime * 60 / OLYMPIC_BIKE_LENGTH_KM; double kilometerRunRate = olympicRunTime * 60 / OLYMPIC_RUN_LENGTH_KM; double mileSwimRate = kilometerSwimRate * KILOMETERS_PER_MILE; double mileBikeRate = kilometerBikeRate * KILOMETERS_PER_MILE; double mileRunRate = kilometerRunRate * KILOMETERS_PER_MILE; double ironmanSwimTime = kilometerSwimRate * IRONMAN_SWIM_LENGTH_KM / 60; double ironmanBikeTime = kilometerBikeRate * IRONMAN_BIKE_LENGTH_KM / 60; double ironmanRunTime = kilometerRunRate * IRONMAN_RUN_LENGTH_KM / 60; double ironmanTotalTime = ironmanSwimTime + ironmanBikeTime + ironmanRunTime; //display computations System.out.println("Your Olympic Triathlon rates: "); System.out.println(" Swim: " + kilometerSwimRate + " min/km (" + mileSwimRate + " min/mile)"); System.out.println(" Bike: " + kilometerBikeRate + " min/km (" + mileBikeRate + " min/mile)"); System.out.println(" Run: " + kilometerRunRate + " min/km (" + mileRunRate + " min/mile)\n"); // displays 40.0/64.--, 3.0/4.82--, 6.0/9.65-- System.out.println("Projected Ironman Triathlon times: "); System.out.println("Swim: " + ironmanSwimTime + "hrs"); System.out.println("Bike: " + ironmanBikeTime + "hrs"); System.out.println("Run: " + ironmanRunTime + "hrs"); System.out.println("Total: " + ironmanTotalTime + "hrs"); //oops forgot to divide by 60. now it displays the right answers // 2.53333333, 9.0, 4.2195, 15.75283333 //more efficient-looking code using arrays and loops //initialize final double[] OLYMPIC_LENGTH_KM = {1.5, 40, 10}; final double[] IRONMAN_LENGTH_KM = {3.8, 180, 42.195}; String[] events = {"Swim", "Bike", "Run"}; double[] olympicTime = new double[events.length]; double[] kilometerRate = new double[events.length]; double[] mileRate = new double[events.length]; double[] ironmanTime = new double[events.length + 1]; int i; String[] printable = {"Your Olympic Triathlon times: ", "Your Olympic Triathlon rates:\n", "Projected Ironman Triathlon times: \n"}; //gather System.out.println("Ironman Triathlon: how long will it take you?\n"); System.out.println("Your Olympic Triathlon times in hours, please:"); for (i = 0; i < events.length; i++) { System.out.print(events[i] + ": "); olympicTime[i] = stdin.nextDouble(); //create string to print after input is entered printable[0] += events[i] + ", " + olympicTime[i]; if (i == events.length - 1) { printable[0] += "\n"; } else { printable[0] += "; "; } //calculations kilometerRate[i] = olympicTime[i] * 60 / OLYMPIC_LENGTH_KM[i]; mileRate[i] = kilometerRate[i] * KILOMETERS_PER_MILE; ironmanTime[i] = kilometerRate[i] * IRONMAN_LENGTH_KM[i] / 60; ironmanTime[events.length] += ironmanTime[i]; //create final output strings printable[1] += " " + events[i] + ": " + kilometerRate[i]; printable[1] += " min/km (" + mileRate[i] + " min/mile)\n"; printable[2] += " " + events[i] + ": " + ironmanTime[i] + "\n"; } //print out what the user entered System.out.println(printable[0]); //print out the calculations System.out.println(printable[1]); System.out.print(printable[2]); System.out.println("Total: " + ironmanTime[events.length]);; } }