write a java program. A group of runners run the Avrasya marathon. Their names a
ID: 3583843 • Letter: W
Question
write a java program.
A group of runners run the Avrasya marathon. Their names and running times (in minutes) are: Selena: 341 , Gomez: 273 , Hamilton: 278 ,Sue: 329 ,Phil: 445 ,Matt: 402 ,Alex: 388 ,Emma: 275 ,John: 243 James: 334 , Jane: 412 , Emily: 393 , Daniel: 299 ,Neda: 343 , Aaron: 317 ,Kate: 265 1)
Print results: Write a method named printResultsInAscOrder that takes as input an array of integers and an array of strings and prints the runners’ names and their running times in increasing order. Run this method on the arrays. 2) Find the fastest runner. Print the name and his/her time (in minutes).
Find the second fastest runner. Print the name and his/her time (in minutes): Write a method fastestRunner that takes as input an array of integers and returns the index corresponding to the person with the lowest time. Run this method on the array of times. Print out the name and time corresponding to the returned index. Write a second method named secondFastestRunner to find the second-best runner. Run this method on the array of times. Print out the name and time corresponding to the returned index.
Here is a program skeleton to get started: class Homework2 { public static void main (String[] arguments) { String[] names = { "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate" }; int[] times = { 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 }; for (int i = 0; i < names.length; i++) { System.out.println(names[i] + ": " + times[i]); } } }
Explanation / Answer
Question 1 :
here we will need to use the concept of parallel arrays, where the indexes of the 2 arrays will present the runners, that is, the i'th index will represent the i'th runner and names[i] its name and times[i] its time!
but if we sort the times array , we will loose this index wise runner information as it will now be a mismatch .
so to solve this we will take the two arrays as input and then encapsulate the data in a seperate class. So , when we sort the times array now, the names array will be automatically matched to it!.
code
------------------------------------------
}
now encapsulate and sort these arrays into a class like this
now to print the sorted array of runners in increasing order use the getter funciton in the Runner class like -
for(i=0;i < num; i++)
{
System.out.println(runners[i].getName() + ": " + runners[i].getTime());
}
the fastest runner is the one which is present in the last index . so runners[num-1] is last obejct in our code and hence the fastest runner! so to print its name and time simply do -
System.out.println(runners[num-1].getName() + ": " + runners[num-1].getTime());
-----
similarly, second fastest runner will be (num-2)th index !
to print its name , do same as above like -
System.out.println(runners[num-2].getName() + ": " + runners[num-2].getTime());
-----
index of the person with lowest time can be found out by matching the name of runners[0].getName() [since this is sorted so the lowest time will be for the runner with index 0.] with the original input array Names[i].
print the i'th index for which ( runners[0].getName() == Names[i] ) holds!
code -
for(int i = 0 ; i < num ; i++)
{
if(runners[0].getName == Names[i])
{
System.out.println(i + "is the index of the person with the lowest time");
break;
}
}
-----
do similarly for the second best runner ! its index will be 1 !
-----------------
thank you
---------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.