Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA -- needs IO Throws Exception Jason, Samantha, Ravi, Sheila, and Ankit are p

ID: 3829449 • Letter: J

Question

JAVA -- needs IO Throws Exception

Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming

marathon. Each day of the week they run certain miles and write them into

a notebook. At the end of the week, they would like to know the number

of miles run each day, the total miles for the week, and average miles run

each day. Write a program to help them analyze their data. Your program

must contain parallel arrays: An array to store the names of the runners and a

two-dimensional array of 5 rows and 7 columns to store the number of

miles run by each runner each day. Furthermore, your program must

contain at least the following methods: a method to read and store the

runners name and the number of miles run each day; a method to find the

total miles run by each runner and the average number of miles run each

day; and a method to output the results. (You may assume that the input

data is stored in a file and each line of data is in the following form:

runnerName milesDay1 milesDay2 milesDay3 milesDay4 milesDay5

milesDay6 milesDay7.)

Marathon_Data.txt ((file))

Jason 10 15 20 25 18 20 26
Samantha 15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
Sheila 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21

Explanation / Answer

MilesTest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;


public class MilesTest {

  
   public static void main(String[] args) throws FileNotFoundException {
       File file = new File("D:\Marathon_Data.txt");
       Scanner scan = new Scanner(file);
       int miles[][] = new int[5][7];
       String names[] = new String[5];
       readData(scan, miles, names);
       DecimalFormat df = new DecimalFormat("0.00");
       for(int i=0; i<miles.length; i++) {
           System.out.println(names[i]+" Average Miles: "+df.format(getMilesAverage(miles[i])));
       }
   }
   public static void readData(Scanner scan, int miles[][], String names[]) {
       for(int i=0; i<miles.length; i++){
           names[i] = scan.next();
           for(int j=0;j<miles[i].length; j++){
               miles[i][j] = scan.nextInt();
           }
       }
   }
   public static double getMilesAverage(int miles[]) {
       int total = 0;
       for(int i=0; i<miles.length; i++) {
           total = total + miles[i];
       }
       return total/(double)miles.length;
   }

}

Output:

Jason Average Miles: 19.14
Samantha Average Miles: 21.00
Ravi Average Miles: 19.29
Sheila Average Miles: 19.00
Ankit Average Miles: 18.43