In: Computer Science
Using Java,
Ask for the runner’s name
Ask the runner to enter a floating point number for the number of miles ran, like 3.6 or 9.5
Then ask for the number of hours, minutes, and seconds it took to run
Format the numbers with leading 0's if the pace is 8 minutes and 3 second, it should be 8:03 and for marathon time, if its 1 hour and 15 minutes and 9 seconds, it should be 1:15:09
Please read through the whole thing, as the pace table example and fastest man time is listed below...
A marathon is 26.219 miles
Pace is how long it takes in minutes and seconds to run
1 mile.
Example Input:
What is your first name? // user enters Pheidippides
How far did you run today? 10.6 // user enters 10.6
miles
How long did it take? Hours: 1 // user enters 1 hours
Minutes: 34 // user enters 34 minutes
Seconds: 17 // user enters 17 seconds
Example Output:
Hi Pheidippides
Your pace is 8:53 (minutes: seconds)
At this rate your marathon time would be 3:53:12
Good luck with your training!
After your program tells the user what their pace is,
your program will build a table showing the following columns. The
pace table should start with the fastest man time which is Eliud
Kipchoge with a pace of 4:37 and 2:01:04 and continue in 17 minute
and 37 second intervals until you reach the marathon time of the
user.
Example: (THE PACE TABLE EXAMPLE)
Pace Table
Pace Marathon
4:37 2:01:04 ←- Eliud Kipchoge
5:17 2:18:41
5:57 2:36:18
6:37 2:53:55
7:18 3:11:32
7:58 3:29:09
8:38 3:46:46
8:53 3:53:12 ← Pheidippides
HINTS
The table should start with the World Record pace and time which is 4:37, 2:01:04.
Then continues in 17 minute and 37 second intervals until you reach the marathon time of the user.
Use a static function to print the pace table, introduce a while loop.
For the first person it should call a printTable function
Example : printTable (pace, “<--- Eliud Kipchoge”)
The pace table continues until it reaches the user
printTable (myPace, name) something like that
For the marathon and pace time, make sure the format has 0’s if the time is 9 seconds, it should be 09.
Use the printf statement for formatting output (“02d %f%s”)
// Java program to calculate the pace and marathon time and display a pace table
import java.util.Scanner;
public class MarathonPaceTable {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name;
int hours, mins, secs;
double distance;
double marathonDistance = 26.219;
// input of runner's name
System.out.print("What is your first name? ");
name = scan.nextLine();
// input of miles ran
System.out.print("How far did you run today? ");
distance = scan.nextDouble();
// input of time taken to run the given miles in hours, mins and secs
System.out.print("How long did it take?\nHours: ");
hours = scan.nextInt();
System.out.print("Minutes: ");
mins = scan.nextInt();
System.out.print("Seconds: ");
secs = scan.nextInt();
int paceMins, paceSecs;
// calculate the total time in seconds
int totalSecs = (hours*3600) + (mins*60) + secs;
double pace = totalSecs/distance; // calculate pace in seconds
// convert pace to mins:secs
paceMins = (int)(pace/60);
pace -= (paceMins*60);
paceSecs = (int)pace;
// output the name and pace
System.out.println("Hi "+name);
System.out.printf("Your pace is %d:%02d (minutes: seconds)",paceMins,paceSecs);
// calculate marathon time
double totalSecsPerMile = totalSecs/distance;
double marathonTime = marathonDistance*totalSecsPerMile;
int marathonHours = (int)(marathonTime/3600);
marathonTime -= (marathonHours*3600);
int marathonMins = (int)(marathonTime/60);
marathonTime -= (marathonMins*60);
int marathonSecs = (int)marathonTime;
// output the marathon time
System.out.printf("\nAt this rate your marathon time would be %d:%02d:%02d",marathonHours,marathonMins,marathonSecs);
System.out.println("\nGood luck with your training!");
// set the initial variables for the pace table
System.out.printf("\n%-5s %-15s","Pace","Marathon");
int startPaceMin = 4, startPaceSec = 37;
int startMarathonHr = 2, startMarathonMin = 1, startMarathonSec = 4;
String paceStr = String.format("%d:%02d %d:%02d:%02d", startPaceMin,startPaceSec,startMarathonHr,startMarathonMin,startMarathonSec);
printTable (paceStr, "<--- Eliud Kipchoge");
// loop that continues till we reach the marathon time of user
while(true)
{
// get the next marathon time
startMarathonSec += 37;
if(startMarathonSec >= 60)
{
startMarathonMin++;
startMarathonSec = startMarathonSec-60;
}
startMarathonMin += 17;
if(startMarathonMin >= 60)
{
startMarathonHr++;
startMarathonMin = startMarathonMin - 60;
}
if(startMarathonHr > marathonHours )
break;
else if(startMarathonHr == marathonHours)
{
if(startMarathonMin > marathonMins)
break;
else if(startMarathonMin == marathonMins)
if(startMarathonSec >= marathonSecs)
break;
}
// calculate the pace
double total = (double)(startMarathonHr*3600 + startMarathonMin*60 + startMarathonSec);
double paceTime = total/marathonDistance;
startPaceMin = (int)(paceTime/60);
paceTime -= (startPaceMin*60);
startPaceSec = (int)paceTime;
// output the pace
paceStr = String.format("%d:%02d %d:%02d:%02d", startPaceMin,startPaceSec,startMarathonHr,startMarathonMin,startMarathonSec);
printTable (paceStr, "");
}
// calculate the pace for the user
double total = (double)(startMarathonHr*3600 + startMarathonMin*60 + startMarathonSec);
double paceTime = total/marathonDistance;
startPaceMin = (int)(paceTime/60);
paceTime -= (startPaceMin*60);
startPaceSec = (int)paceTime;
// output the pace for the user
paceStr = String.format("%d:%02d %d:%02d:%02d", paceMins,paceSecs,marathonHours,marathonMins,marathonSecs);
printTable (paceStr, "<- "+name);
scan.close();
}
// method to display the pace and user name in tabular format
public static void printTable(String pace, String name)
{
System.out.printf("\n%-20s%s",pace,name);
}
}
//end of program
Output: