Write a program that reads four triathlon race time records from a file. The nam
ID: 3800556 • Letter: W
Question
Write a program that reads four triathlon race time records from a file. The name of the file is racedat.txt. Each line of the file represents one record and contains an athlete identifier (a single character) and three numeric values. The three values are the completion times (in minutes) for the running, swimming, and biking stages of a triathlon. You will produce a nicely formatted report that displays these values, as well as the total time for each athlete. Finally, your report should calculate the average total time for the race.
Hint: You may create your own racedat.txt file using Windows Notepad or other text editor. Your input file should have the format:
A 31 27 52
B 29 31 58
C 35 36 51
D 34 36 62
Explanation / Answer
/* TraithlonRace.java : */
_____________________
package org.chegg;
import java.io.BufferedReader;
import java.io.FileReader;
public class TraithlonRace {
public static void main(String[] args) {
try{
FileReader fr = new FileReader("F: acedat.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
System.out.println("AthleteIdentifier RunninngTime SwimmingTime and BikingTime(mins) TotalRaceTime AverageTotalTime");
System.out.println("_______________________________________________________________________________________________");
while((line=br.readLine())!=null){
String det[] = line.split(" ");
System.out.print(" "+det[0]+" ");
float total_race_time = 0;
for(int i=1;i<det.length;i++){
System.out.print(det[i]+" ");
total_race_time +=Integer.parseInt(det[i]);
}
float avg_time = (total_race_time)/3;
System.out.print(total_race_time+" "+avg_time);
}
br.close();
fr.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
racedat.txt :
__________
A 31 27 52
B 29 31 58
C 35 36 51
D 34 36 62
P 32 39 63
S 28 37 56
G 33 38 57
E 34 36 64
F 26 38 59
R 30 29 62
Sample Input & Output :
_____________________
AthleteIdentifier RunninngTime SwimmingTime and BikingTime(mins) TotalRaceTime AverageTotalTime
___________________________________________________________________________________________________
A 31 27 52 110.0 36.666668
B 29 31 58 118.0 39.333332
C 35 36 51 122.0 40.666668
D 34 36 62 132.0 44.0
P 32 39 63 134.0 44.666668
S 28 37 56 121.0 40.333332
G 33 38 57 128.0 42.666668
E 34 36 64 134.0 44.666668
F 26 38 59 123.0 41.0
R 30 29 62 121.0 40.333332
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.