This is IntelliJ java programming Marathon You have a friend

This is IntelliJ java programming.

Marathon You have a friend who wants to run a marathon race, which is 26.2 miles. Currently, your friend can run exactly 1 mile. Your friend\'s plan is to try to run 10% further every week, until they can run the entire marathon distance.

So in the first week of training, they will run 1 mile; second week 1.1 miles, third week 1.21 miles… Use a loop to calculate the distance they need to run every week until they can run the full 26.2 miles. How many weeks until your friend can run an entire marathon?

Can you use string formatting to display the weekly distances to 2 decimal places?

Once you\'ve written this program, perhaps your friend thinks this schedule is too slow. Your friend wants to see if they can get to the marathon distance faster by increasing their distance by 5% every week? Or even 1% every week? How does that affect the number of weeks needed?

Extract the weekly schedule part of the code into a method. Use your method to calculate and display the schedules and total weeks for 1% 5% and 10%.

Extra question: what if your friend decides they want to train for a 10 mile race? Or a 100 mile race? Also extract the goal distance, so now your method takes two arguments: the percentage increase every week, and the goal distance.

Solution

package snippet;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Marathon {
   static int Getweeks(int increase_per_week,double d)
   {
       double dis=1;
       int weeks=0;
       DecimalFormat dc=new DecimalFormat(\"#0.00\");

       while(d>=dis)
       {
           System.out.println(\"distance in week \"+(weeks+1)+\" is \"+dc.format(dis));
           dis=dis+dis*increase_per_week/100;
           weeks++;
       }
       dis=dis+dis*increase_per_week/100;
       System.out.println(\"distance in week \"+(weeks+1)+\" is \"+dc.format(dis));
       weeks++;
       return weeks;
      
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println(\"Enter Goal distance \");
       Scanner sc=new Scanner(System.in);
       double d=sc.nextDouble();
       System.out.println(\"Enter Percentage increas per week \");
       int per=sc.nextInt();
      
       System.out.println(\"Number of weeks to reach goal is \"+Getweeks(per, d));
   }

}

===============================================

Output:

Enter Goal distance
26.2
Enter Percentage increas per week
10
distance in week 1 is 1.00
distance in week 2 is 1.10
distance in week 3 is 1.21
distance in week 4 is 1.33
distance in week 5 is 1.46
distance in week 6 is 1.61
distance in week 7 is 1.77
distance in week 8 is 1.95
distance in week 9 is 2.14
distance in week 10 is 2.36
distance in week 11 is 2.59
distance in week 12 is 2.85
distance in week 13 is 3.14
distance in week 14 is 3.45
distance in week 15 is 3.80
distance in week 16 is 4.18
distance in week 17 is 4.59
distance in week 18 is 5.05
distance in week 19 is 5.56
distance in week 20 is 6.12
distance in week 21 is 6.73
distance in week 22 is 7.40
distance in week 23 is 8.14
distance in week 24 is 8.95
distance in week 25 is 9.85
distance in week 26 is 10.83
distance in week 27 is 11.92
distance in week 28 is 13.11
distance in week 29 is 14.42
distance in week 30 is 15.86
distance in week 31 is 17.45
distance in week 32 is 19.19
distance in week 33 is 21.11
distance in week 34 is 23.23
distance in week 35 is 25.55
distance in week 36 is 30.91
Number of weeks to reach goal is 36

This is IntelliJ java programming. Marathon You have a friend who wants to run a marathon race, which is 26.2 miles. Currently, your friend can run exactly 1 mi
This is IntelliJ java programming. Marathon You have a friend who wants to run a marathon race, which is 26.2 miles. Currently, your friend can run exactly 1 mi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site