(10 pts) Suppose you\'re training for a footrace, and you\'ve been keeping track
ID: 3914103 • Letter: #
Question
(10 pts) Suppose you're training for a footrace, and you've been keeping track of your total miles run per week. You have data for several weeks now, and you want to determine whether you've been steadily increasing your weekly mileage. 2. Within your Lab6 folder, create a new file named RunAnalyzer.java. Write a program that allows the user to enter weekly miles run for a user-specified number of weeks. Your program should then determine and report whether the data is consistently increasing. (If the miles run for any week are less than or equal to the miles run for the previous week, the data is not consistently increasing.) The program should also compute and display the average mileage increase per week. This average mileage increase should be computed and displayed regardless of whether the data is consistently increasing. Here are two examples of what your program might look like when you run it. Underlined portions indicate user input. Example 1: How many weeks of data do you have? nter miles run for week 1:Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
RunAnalyzer.java
-------------
import java.util.Scanner;
public class RunAnalyzer {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double[] data;
int n;
System.out.println("How many weeks of data do you have?");
n = keyboard.nextInt();
data = new double[n]; //allocate array of size n
for(int i = 0; i < n; i++){
System.out.println("Enter miles run for week "+ (i+ 1) +": ");
data[i] = keyboard.nextDouble();
}
boolean increasing = true;
double average = 0;
double change;
for(int i = 1; i < n; i++){
if(data[i] <= data[i-1]) //current is more than previous
increasing = false;
change = data[i] - data[i-1];
average += change;
}
average /= (n-1);
if(increasing)
System.out.println("Your weekly mileage is consistently increasing!");
else
System.out.println("Your weekly mileage is NOT consistently increasing!");
System.out.println("Average weekly change: " + average);
}
}
output
------
How many weeks of data do you have?
5
Enter miles run for week 1:
3
Enter miles run for week 2:
3.2
Enter miles run for week 3:
3.8
Enter miles run for week 4:
4
Enter miles run for week 5:
4.5
Your weekly mileage is consistently increasing!
Average weekly change: 0.375
-------
How many weeks of data do you have?
5
Enter miles run for week 1:
11
Enter miles run for week 2:
10
Enter miles run for week 3:
12
Enter miles run for week 4:
12
Enter miles run for week 5:
15
Your weekly mileage is NOT consistently increasing!
Average weekly change: 1.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.