Answer:
Here is code in java.
import java.util.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
try{
boolean flag=false;
int total_mile=0;
double avg_mile;
// part 1, create and initialize array
int inp[]={2,5,9,11,13,17,20,22,24,26};
System.out.print("miles ran per week: ");
// print the miles per week of train
for(int x=0;x<10;x++)
{
System.out.print(inp[x]+" ");
}
System.out.println();
//part 2, print the value of index 2 in the list
System.out.println("value at index 2: "+inp[2]+" miles");
//part 3, find length of array
System.out.println("list length: "+inp.length);
// part 4, calculate total mile and average
for(int y=0;y<10;y++)
{
total_mile=total_mile+inp[y];
}
System.out.println("total miles ran: "+total_mile+" miles");
avg_mile=total_mile/10.0;
System.out.println("average no of miles ran= "+avg_mile+" miles");
// part 5, check 4 exist in the list or not
for(int k=0;k<10;k++)
{
if(inp[k]==4)
{
flag=true;
break;
}
}
if(flag)
System.out.println("4 exist in the list.");
else
System.out.println("4 doesn't exists in the list.");
}catch(Exception ex){
return;}
}
}
Explanation:
Create and initialize array with list value equals to 2 and maximum value 26. Print the miles of each week.Then find the value of miles at index 2 in the list.Next find the length of the array and print it.Calculate total miles ran of 10 weeks.Then calculate the average miles by dividing the total with 10. In the array, check 4 exist or not.
Output:
miles ran per week: 2 5 9 11 13 17 20 22 24 26
value at index 2: 9 miles
list length: 10
total miles ran: 149 miles
average no of miles ran= 14.9 miles
4 doesn't exists