Three runners are in a 40 meter race. Each runner travels at a constant speed. W
ID: 3881411 • Letter: T
Question
Three runners are in a 40 meter race. Each runner travels at a constant speed.
Write a program that creates a runner class containing runner ID and speed.
Allow the user to enter the speed for each runner. Call the constructor to create an instance of the runner class. Include appropriate properties. Do not allow the ID to be changed after an object has been constructed. Provide a method in the runner class to compute the time it takes for each runner to cross the finish line and return the time. Add code to show which runner was the fastest. Add a three element array and assign the runners speeds into the array. Use a loop to add up all of the runners speeds (make use of the += operator). Then average the speeds and output the result.
Sample Output:
Runner 1 took 5.3 seconds
Runner 2 took 4.5 seconds
Runner 3 took 5.5 seconds
Runner 1 was the fastest.
The average speed for the three runners is: 15.3
Explanation / Answer
Please go through the below java program and where ever required the program was explained with necessary comments at each line.
import java.io.*;
import java.util.*;
class runners{
public static void main(String args[]) {
int fastest;
float arr[];
float avg; //This variable will be used to fine the average of the speeds
float sum = 0; //This is a sum variable which stores the sum of the speeds of Runners
int i = 0;
arr = new float[3]; // This will be used to store the speeds of all three runners
Scanner in = new Scanner(System.in); // Scanner is a class which we used here to read values from keyboard
runner1 r1 = new runner1(); // creating an object for first runner
runner1 r2 = new runner1();
runner1 r3 = new runner1();
r1.runnerID = 1; // Initailising the runners with ID's
r2.runnerID = 2;
r3.runnerID = 3;
r1.speed = in.nextFloat(); //reading the speed from keyboard
r2.speed = in.nextFloat();
r3.speed = in.nextFloat();
r1.timeTaken(); //Finding the time taken by each runner individually
r2.timeTaken();
r3.timeTaken();
if((r1.time < r2.time) && (r1.time < r3.time)) // Here we are finding the fastest runner among three
fastest = r1.runnerID;
else if(r2.time < r3.time)
fastest = r2.runnerID;
else
fastest = r3.runnerID;
System.out.println("Runner 1 took"+r1.time+"seconds");
System.out.println("Runner 2 took"+r2.time+"seconds");
System.out.println("Runner 3 took"+r3.time+"seconds");
System.out.println("Runner "+fastest+" was the fastest");
arr[0] = r1.time;
arr[1] = r2.time;
arr[2] = r3.time;
for(i=0; i<3; i++){
sum = sum + arr[i]; // Finding the sum of the speeds to find the average atlast.
}
avg = sum/3; //finding the average
System.out.println("The average speed for three runners is "+avg);
}
}
class runner1{ //runner1 class with three variables and one function
float speed;
int runnerID;
float time;
void timeTaken(){
time = 40/speed; // We have used distance = speed * time
}
}
Sample Input :3
4
5
Runner 1 took13.333333seconds
Runner 2 took10.0seconds
Runner 3 took8.0seconds
Runner 3 was the fastest
The average speed for three runners is 10.444444
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.