Use this utility file to generate a list of runner and run times. Objective You
ID: 3670982 • Letter: U
Question
Use this utility file to generate a list of runner and run times.
Objective
You are the coach for a track team, and you are trying to create a program that finds the player with the lowest run time.
Your task is to find the player with the lowest run time, as well as the player with the next best score.
Specification:
Your class must have the following methods:
fastestRunner - Takes an array of run times and returns the index of the smallest item
parameters
- runTimes An array of (integer) run times
return The index of the lowest run time
secondFastest - Takes an array of run times and returns the index of the second to the smallest item
parameters
- runTimes An array of (integer) run times
return The index of the second fastest run time
Hints
You should use the Utils class to access the names and runTimes arrays, like so:
The ith entry in names corresponds to the ith entry in salesranks
You should call fastestRunner in your secondFastest method
Example Dialog
Explanation / Answer
public class HelloWorld {
/**
* @param args
*/
public static int fastestRunner(int[] times){
//int min = 10000;
int min = Integer.MAX_VALUE;
//int index = 0;
int index = -1;
//find fastest time
for(int i = 0; i < times.length; i++){
if(times[i] < min){
min = times[i];
index = i;
}
}
return index;
}
public static int secondFastest(int[] times){
//int index = 0;
int index = -1;
//int min = 10000;
int min = Integer.MAX_VALUE;
int fastestIndex = fastestRunner(times);
for(int i = 0; i < times.length; i++){
//jump over fastest runner
if(i == fastestIndex)
continue;
if(times[i] < min){
min = times[i];
index = i;
}
}
/*for(; index < times.length; index++){
if(times[index] == min){
return index;
}
}
*/
return index;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] names = {
"Elene", "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]);
}
*/
System.out.println("The fastest runner is " + names[fastestRunner(times)]);
System.out.println("The second fasest runner is " + names[secondFastest(times)]);
}
}
output
The fastest runner is John
The second fasest runner is Kate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.