Using Java: You will write a program that will ask the runner\'s name, how far h
ID: 3784053 • Letter: U
Question
Using Java:
You will write a program that will ask the runner's name, how far he/she ran, and how long it took. You will then calculate how fast the runner actually ran. Then you will calculate how long it would take to run a 10K, and how long it would take to run a marathon if the runner were able to maintain this pace. You will also show the runner what his/her training pace is Ask for the runner's name Ask She runner to enter a floating point number for the number of miles ran, like 3.6, or 9.5 3 Then ask for the number of hours, minutes, and seconds it took to run. What is your first name?//user enters Pheidippides How far did you run today? 1.5//user enters 1.5 miles user enters 0 hours How long did it take? hours: 0//user enters 12 minutes seconds:0//user enters 0 seconds Hi Pheidippides, Your pace is 8:0 (minutes seconds) At this rate your marathon time would be 3:29:36 and your 10K time would be 0:49:41 Good luck with your training! Definitions: 10K - a 10 kilometer race. marathon a 26.2 mile long race. 1 mile- 1.61 kilometres pace - how long it takes (in minutes and seconds to run 1 mile. Example: if I can run 1 mile in 5 minutes and 23 seconds, then my pace is 5:23.Explanation / Answer
Runner.java:
import java.util.Scanner;
public class Runner {
public static void main(String args[]){
String name;
float distance;
int hours,minutes,seconds;
Scanner scanner = new Scanner(System.in);
System.out.println("What is your first name?");
name=scanner.nextLine();
System.out.println("How far did you run today?");
distance=(scanner.nextFloat());
System.out.println("How long did it take? hours:");
hours=(scanner.nextInt());
System.out.println("minutes:");
minutes=(scanner.nextInt());
System.out.println("seconds:");
seconds=(scanner.nextInt());
int timeInSeconds=hours*3600 + minutes*60 + seconds;
System.out.println("Hi "+ name +",");
float pace=timeInSeconds/distance;
printPace(pace); //method to print pace
printMarathon(pace); //method to print Marathon time
print10k(pace); //method to print 10K time
}
static void printPace(float pace){
int h=0,m=0,s=0;
if(pace/3600>=1){
h=(int) pace/3600;
pace=pace%3600;
}
if(pace/60>=1){
m=(int) pace/60;
pace=pace%60;
}
s=(int)pace;
if(h>0){
System.out.println("Your pace is " + h + ":" + m + ":" + s +" (hours:minutes:seconds)");
}
else{
System.out.println("Your marathon time is " + m + ":" + s +" (minutes:seconds)");
}
}
static void printMarathon(float pace){
int h=0,m=0,s=0;
float marathonTime= (float) pace * 26.2f ; //marathon Time is calculated
if(marathonTime/3600>=1){
h=(int) marathonTime/3600;
marathonTime=marathonTime%3600;
}
if(marathonTime/60>=1){
m=(int) marathonTime/60;
marathonTime=marathonTime%60;
}
s=(int)marathonTime;
System.out.println("At this rate your marathon time would be " + h + ":" + m + ":" + s);
}
static void print10k(float pace){
int h=0,m=0,s=0;
float ten_k_time= (float) pace * (10f/1.61f) ; //10k Time is calculated
if(ten_k_time/3600>=1){
h=(int) ten_k_time/3600;
ten_k_time=ten_k_time%3600;
}
if(ten_k_time/60>=1){
m=(int) ten_k_time/60;
ten_k_time=ten_k_time%60;
}
s=(int)ten_k_time;
System.out.println("and your 10k time would be " + h + ":" + m + ":" + s +" Good luck with your training!");
}
}
Sample run:
What is your first name?
Pheidippides
How far did you run today?
1.5
How long did it take? hours:
0
minutes:
12
seconds:
0
Hi Pheidippides,
Your marathon time is 8:0 (minutes:seconds)
At this rate your marathon time would be 3:29:36
and your 10k time would be 0:49:41
Good luck with your training!
Sample run 2:
What is your first name?
kanika
How far did you run today?
5
How long did it take? hours:
1
minutes:
0
seconds:
0
Hi kanika,
Your marathon time is 12:0 (minutes:seconds)
At this rate your marathon time would be 5:14:24
and your 10k time would be 1:14:32
Good luck with your training!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.