You are preparing for the Boston Marathon. In order to prepare, you need to trai
ID: 649693 • Letter: Y
Question
You are preparing for the Boston Marathon. In order to prepare, you need to train for 10 weeks, running an increasing number of miles per week, starting at running at least 2 miles your first week up to 26 miles by week 10.
1.) Initialize the array with the appropriate number of values.
2.) What is the value in the array element when the index contains 2?
3.) What is your list length?
4.) Calculate the sum of the total miles you spent running over the 10 weeks. Calculate the average number of miles you ran.
5.) Write a search to determine whether 4 is on your list.
Explanation / Answer
/**
* The java program that creates a miles array
* and calculates the total of the miles
* in the array miles. Then finds the average of
* of total. Prints average value.
* Search for a element and print a message
* element is found.
* */
//TestProgram.java
public class TestProgram
{
public static void main(String[] args)
{
//1.Initiallize the array with miles
int miles[]={2,4,8,12,15,17,20,23,26};
int total=0;
int average;
boolean found=false;
for (int x=0;x<miles.length;x++)
{
//calculate the total of the miles
total=total+miles[x];
}
//2.Value at index=2
int index=2;
System.out.println("Value at "+index+" is "+miles[2]);
//3.Length of list miles
System.out.println("Length of list "+miles.length);
//4.Calculate the avererage of miles by dividivint total with length of the miles array
average = total/miles.length;
System.out.println ("The total numbers of miles ran during training is " + total);
System.out.println ("The average number of miles ran during training is " + average);
//5.searching for element 4 in the list
for (int i = 0; i < miles.length && !found; i++)
{
if(miles[i]==4)
//set found true if value equal to 4
found=true;
}
//check if found is true then print 4 is in the list
if(found)
System.out.println("4 is in the list");
else
//otherwiser print 4 is not in the list
System.out.println("4 is not in the list");
}
}
//Note:
//Array initialization is not as per order of incremment of two for each week
//upto 10 weeks
//int miles[]={2,4,6,8,10,12,14,16,18,20,22,24,26};
//For ten weeks , you get only
//int miles[]={2,4,6,8,10,12,14,16,18,20}
//If it is not your case, ignore it.
//Otherwise replace the miles array in the program
//with int miles[]={2,4,6,8,10,12,14,16,18,20,22,24,26};
sample output:
Value at 2 is 8
Length of list 9
The total numbers of miles ran during training is 127
The average number of miles ran during training is 14
4 is in the list
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.