Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are asked to create program that stores running time for all competitors in G oteborgsvarvet (Gothenburg Half Marathon). Create the program according to the
You are asked to create program that stores running time for all competitors in G oteborgsvarvet (Gothenburg Half Marathon).
Create the program according to the following requirements:
Competitor class:
1. Create a class with the name Competitor which should have four instance variables:
(a) name (String) (b) age (int)
(c) gender (String)
(d) time (Time; a class you need to implement)
2. The constructor of the class should take in all four instance variables: name, age, gender, and time.
3. You should be able to store Competitor objects using Serialization, the Competitor object has a Time object which should also be able to store using Serialization.
4. Youll need to be able to sort the objects based on the running time. You should implement the Comparable interface (comparing two integer values) for this where:
Stotal =h3600+m60+s
5. You should Override the toString() to print out the result in the end in following format:
Hint: String.format()
Time class:
H : MM : SS > Name
1. Create a class with the name Time. This class should have three instance variables, hours (int), minutes (int) and seconds (int).
2. The constructor of the class should take in all three instance variables: hours, minutes and seconds.
3. Add getter and setter for all instance variables.
4. You should be able to store Time objects using Serialization.
Main class:
1. Youll get a text file, participants.txt, including all competitors in the G oteborgsvarvet. The file is structured as name,age,gender, where each competitor has its own line in the file:
Rickard,37,Male Anna,28,Female ...
This file should be stored in your project directory.
2. Create a method called letsRun(), taking the path (Path) for the participate.txt file as a param- eter: The method should return an ArrayList holding Competitor objects.
3. This method should read each line in the file, generate a random time object and create a competitor object for each participant in the file and store it in an ArrayList. The critera for the generated time objects are:
hours 2 [1, 3] minutes 2 [0, 59] seconds 2 [0, 59] 8{h,m,s} 2 Z
4. hen all competitors are created and stored in the ArrayList, you should sort the ArrayList so the competitor with the shortest running time is listed first and the longest running time last.hen all competitors are created and stored in the ArrayList, you should sort the ArrayList so the competitor with the shortest running time is listed first and the longest running time last. Hint: Collections.sort()
5. Then, you should print the running time results to the console:
Figure 8: Running results printed.
6. Finally, use serialization to store the sorted ArrayList to a file with the name RunningTimeRe- sults.ser. This functionality should be implemented in a method called writeToFile() taking the ArrayList with the sorted competitors and the path of RunningTimeResult.ser as a parameters.
The program must use at least:
Three classes (the main class, Competitor class and Time class) An ArrayList holding the competitor objects.
Comparator interface to sort the objects
Serializable interface (both in Competitor and Time class)
The Main class should have at least three methods; main, letsRun and writeToFile. The Competitor class should have at least constructor and compareTo().
The Time class should constructor and getters for all instance variables.
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