Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Three runners are in a 40 meter race. Each runner travels at a constant speed. W

ID: 3881484 • 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

This is C#

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CountNumbers
{   
class Runner
{
private static object sync = new object();
public const int distance = 40;
private static int globalCount;

public static int Id
{
get;
set;
}

public int Speed
{
get;
set;
}

public Runner()
{
lock (sync)
{
Id = ++globalCount;
}
}

public double CalcualteTime(double speedOfRunner)
{
double timeTaken = distance / speedOfRunner;
return timeTaken;
}
}


class Program
{
static void Main(string[] args)
{
Dictionary<int, double> ListOfTimeTakenByRunners = new Dictionary<int, double>();

Runner runnerObj1 = new Runner(); //creating object
runnerObj1.Speed = 5; //assigning speed for runner
double timeTakenByRunner1 = runnerObj1.CalcualteTime(runnerObj1.Speed); // calculating the speed of runner   
Console.WriteLine("Time taken by the runner Id = " + Runner.Id + " is : " + Math.Round(timeTakenByRunner1, 2)); // display runner speed
ListOfTimeTakenByRunners.Add(Runner.Id, timeTakenByRunner1); //add it to dictionary which is required to calculate the fastest runner


//Repeat above step
Runner runnerObj2 = new Runner();
runnerObj2.Speed = 3;
double timeTakenByRunner2 = runnerObj2.CalcualteTime(runnerObj2.Speed);
Console.WriteLine("Time taken by the runner Id = " + Runner.Id + " is : " + Math.Round(timeTakenByRunner2, 2));
ListOfTimeTakenByRunners.Add(Runner.Id, timeTakenByRunner2);


//Repeat above step
Runner runnerObj3 = new Runner();
runnerObj3.Speed = 7;
double timeTakenByRunner3 = runnerObj3.CalcualteTime(runnerObj3.Speed);
Console.WriteLine("Time taken by the runner Id = " + Runner.Id + " is : " + Math.Round(timeTakenByRunner3, 2));
ListOfTimeTakenByRunners.Add(Runner.Id, timeTakenByRunner3);


//calculate fastest runner by iterating over all the elements
double max = 0; int runner = 0;
foreach (KeyValuePair<int, double> item in ListOfTimeTakenByRunners)
{
if (item.Value > max)
{
max = item.Value;
runner = item.Key;
}   
}

Console.WriteLine("Fastest runner is Runner " + runner);


//calculate average speed
double averageSpeed = (timeTakenByRunner1 + timeTakenByRunner2 + timeTakenByRunner3) / Runner.Id;
Console.WriteLine("Average speed = " + Math.Round(averageSpeed, 2));

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote