Question
In this lab you will write a program to find the gold, silver and bronze medal winners in a cross country ski race event at
In this lab you will write a program to find the gold, silver and bronze medal winners in a cross country ski race event at the Winter Olympics.
There are many instances in sports where racers do not all start a race at the same time. This happens in early rounds of sprints, when there are too many runners to fit on the track, or in swimming when there are not enough lanes in the pool. This happens sometimes at rowing races when the river is too narrow to hold all the boats. This also happens in ski races. To find the race time for a skier it is necessary to subtract the starting time from the ending time. Then the skier with the fastest time is the gold medal winner. The skier with the second fastest time is the silver medal winner and the skier with the third fastest time is the bronze medal winner.
We will use an arraylist to hold the skiers. The program will read the data for each skier from an input file to create a skier object. We will then add that skier object to the arraylist.
The program will print all the skiers, and then print out the gold, silver and bronze medal winners.
Input The input data will be in a file named "skiers.dat". The file will have the following format:
name
country
start
time
end time
where the start and end times are represented as hour (int), minutes (int), and seconds (double). For example, here is the input for one Skier:
Sara Pellegrini
Italy
09 25 31.2
11 02 14.45
In addition, we will need to provide exception handling. Do not assume that every line in the input file contains the expected data types. There may be input mismatch exceptions. If an input value causes an input mismatch exception, then catch and handle the exception. Handle the exception by skipping that skier and printing an error message. Then continue reading and processing the next skier. A skier with an error in the start time could look like:
Chunxue Chi
China
09 2s 44
11 32 10.88
A skier with an error in the end time could look like:
Rosie Frankowski
United States
11 12 00
ab 50 10.88
Since the name and country are Strings, reading them cannot result in an InputMismatchException. The exception can only occur when reading the hours, minutes, or seconds. If an exception occurs you need to read the rest of that input line. If an exception occurs on the start time you will also need to read the next input line, which has the end time. Then the next time you read it will be the name of the next skier. The incorrect input should not crash your program, and it should not end your reading of the input.
You are responsible for creating your own input file. Your file should contain at least 15 skiers.
To test your program thoroughly you need to think carefully about your input. Here are some tips:
You will need to check whether your code is calculating the race time correctly. Use start and end times where it will be easy for you to calculate the race time. But remember that you want to check for all possibilities, such as (start time minutes) > (end time minutes) and (start time seconds) > (end time seconds).
You can assume that the start time is before the end time.
You can assume that no input will be missing. For example, you do not have to worry about a time that only has two numbers, or a missing name or country.
Make sure all valid skiers are printed correctly.
Make sure your medal winners are calculated correctly.
Start with all valid input, and make sure that your code works correctly.
Once you know that your code works, include some skiers that will cause InputMismatchExceptions so that you can make sure your error file is correct and that you are processing all the valid skiers.
Output
You will create two output files.
The output file results.dat will contain a list of all the skiers (print a title first) and the gold, silver, and bronze medal winners.
Print a title for each medal winner. The output file errors.dat will contain a report on any InputMismatchExceptions which occurred. You do not have to identify the exact field which had the error. You need only identify the line number of the input file where the error occurred, and the name of the skier that was being processed. For each exception, print a message such as:
line xx: mismatch error for , skipping to next skier
For example, a message might look like:
line 11: mismatch error for Chunxue Chi, skipping to next skier
Your code needs to keep track of which line you are reading so it can print the line number in the message.
Print the total number of errors at the end of the error file.
Skier Class
To write this program you will need a class for skiers. Skier should have the following fields:
name (String)
country (String)
starting time (Time)
ending time (Time)
race time (Time)
Skier should have the following methods:
constructor with four parms: name, country, start time, and end time. It should also calculate the amount of time it took the Skier to complete the race.
raceTime: returns a Time object and has no parms. It returns the amount of time taken by the Skier in the race.
toString
compareTo: compare two Skier objects by comparing the race time. The Skier with the smaller race time is the one that's smaller.
Time Class
The Time class will store times using a 24 hour clock: midnight is hour 00, 3am is hour 03, noon is hour 12, 3pm is hour 15, and 11pm is hour 23. When you store hours, minutes and seconds which are less than 10 they will not have a leading zero. When you print them they should have a leading zero. The fields of the Time class are:
hour (int) holds the hour in 24 hour time minutes
(int) holds the minute
seconds (double) holds the second
The Time class has the following methods:
default constructor: initialize the Time to 0, which is midnight
initializing constructor: parms for the hour, minute, and second
toString: format the Time as hh:mm:ss.sss, in 24 hour format; be sure to use two digits if the hour, minutes, or seconds is less than 10
equals
compareTo: return -1 if the time in this is before (or shorter than) the time in the parm; return 0 if they are the same; return 1 if the time in this is after (or longer than) the time in the parm.
minus: parm is a Time; return a new Time object that is the amount of time between this and the parm, assuming that this is after (greater than) the parm. For example, if this is 18:23:45.5 and the parm is 16:30:24.75,
then minus returns a Time object that contains 01:53:20.75
Client/Driver/Demo Code
Read the Skiers from the input file and store them in the ArrayList.
Print all of the Skiers from the ArrayList.
Find the gold, silver, and bronze medal winners and print them. DO NOT USE THE SORT METHOD OF THE ARRAYLIST.
Assumptions:
To keep things from getting too complicated, you can assume there are no ties for the medals.
Since the ski race does not take place in the middle of the night, you can assume that the ending time will be later than the starting time the race starts and finishes in the same day, so you dont have to handle the case where the end time is earlier because the skier doesnt finish the race till after midnight.
Written in JAVA
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started