Using the scenario below, answer the following questions: You are preparing for
ID: 3536955 • Letter: U
Question
Using the scenario below, answer the following questions:
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.
For reference, here is a tutorial on arrays.
For additional details, refer to the Homework Rubric in the Assignment Guidelines and Rubrics folder.
Explanation / Answer
public class BostonMarathonTraining
{
public static void main(String argv[])
{
// 1.create and initialize the array
int array[]={2,5,9,11,13,17,20,22,24,26};
System.out.print("miles ran per week: ");
for(int i=0;i<10;i++)
System.out.print(array[i]+" ");
// 2.output the value ate index 2
System.out.println(" value at index 2: "+array[2]+" miles");
//3. display the list length
System.out.println("list length: "+array.length);
// 4. calculate and display the total miles pent running
int sum=0;
for(int i=0;i<10;i++)
sum=sum+array[i];
System.out.println("total miles ran: "+sum+" miles");
//4. calclate and display the average num of miles ran
double average=sum/10.0;
System.out.println("average no of miles ran= "+average+" miles");
//5. search to determine 4 is in the list or not
boolean exist=false;
for(int i=0;i<10;i++)
{
if(array[i]==4)
{
exist=true;
break;
}
}
if(exist)
System.out.println("4 exist in the list");
else
System.out.println("4 doesnot exists");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.