Use this utility file to generate a list of runner and run times. Objective You
ID: 3670983 • 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
Utils.java (fixed errors)
import java.util.LinkedList;
import java.util.Random;
public class Utils {
public static int[] runTimes;
public static String[] names;
public static void generate() {
String[] names = {
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron",
"Kate"
};
shuffle(names);
Utils.names = names;
runTimes = getRunTimes();
printTable();
}
private static void printTable() {
int i = 0;
System.out.printf("%-8s | %s ", "Name", "Rank");
System.out.println("---------+-----");
for (String name : names) {
System.out.printf("%-8s | %3d", name, runTimes[i]);
i++;
System.out.println();
}
System.out.println("---------+-----");
}
private static int[] getRunTimes() {
int length = names.length;
int[] out = new int[length];
Random rand = new Random();
for (int i = 0; i < length; i++) {
out[i] = 200 + rand.nextInt(300);
}
return out;
}
private static void shuffle(String[] array) {
LinkedList<String> input = new LinkedList<>();
for (String s : array) {
input.add(s);
}
Random rand = new Random();
int index = 0;
while (input.size() > 0) {
int randindex = rand.nextInt(input.size());
array[index] = input.remove(randindex);
index++;
}
}
}
Coach.java
public class Coach {
public static int fastestRunner(int[] runTimes) {
int minRuntime = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < runTimes.length; ++i) {
if (minRuntime > runTimes[i]) {
minIndex = i;
minRuntime = runTimes[i];
}
}
return minIndex;
}
public static int secondFastest(int[] runTimes) {
int minRuntime = Integer.MAX_VALUE, secondMinRuntime = Integer.MAX_VALUE;
int minIndex = -1, secondMinIndex = -1;
for (int i = 0; i < runTimes.length; ++i) {
if (minRuntime > runTimes[i]) {
// update the second to be first
secondMinRuntime = minRuntime;
secondMinIndex = minIndex;
// update the first
minRuntime = runTimes[i];
minIndex = i;
} else if (secondMinRuntime > runTimes[i]) {
secondMinRuntime = runTimes[i];
secondMinIndex = i;
}
}
return secondMinIndex;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Utils.generate();
String[] names = Utils.names;
int[] runTimes = Utils.runTimes;
int fastestRunnerIndex = Coach.fastestRunner(runTimes);
int secondFastestRunnerIndex = Coach.secondFastest(runTimes);
System.out.println("Fastest Runners");
System.out.println(names[fastestRunnerIndex] + " " + runTimes[fastestRunnerIndex]);
System.out.println(names[secondFastestRunnerIndex] + " " + runTimes[secondFastestRunnerIndex]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.