JAVA Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming mara
ID: 3829298 • Letter: J
Question
JAVA
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.)
Explanation / Answer
namesMiles.txt (Save this file under D Drive.Then the path of the file pointing to it is D: amesMiles.txt)
Jason 5 6 3 4 8 3 4
Samantha 3 4 5 6 3 4 5
Ravi 3 4 5 6 5 5 5
Sheila 5 5 4 3 4 2 2
Ankit 7 6 5 7 6 5 6
_____________________
RunnerNamesMiles.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RunnerNamesMiles {
public static void main(String[] args) {
// Creating String array of size 5
String names[] = new String[5];
// Creating a 2-Dimensional array
int miles[][] = new int[5][7];
// Declaring variables
int i = 0, j = 0;
int totWeek = 0;
double average = 0.0;
try {
// Opening the file
Scanner sc = new Scanner(new File("D://namesMiles.txt"));
/*
* This while loop will read the data from the file and populate
* them into arrays
*/
while (sc.hasNext()) {
j = 0;
names[i] = sc.next();
while (sc.hasNextInt()) {
miles[i][j] = sc.nextInt();
j++;
}
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Displaying the Each Runner name and miles run in each day
System.out.println(" Day1 Day2 Day3 Day4 Day5 Day6 Day7");
for (i = 0; i < miles.length; i++) {
System.out.printf("%10s", names[i] + " ");
for (j = 0; j < miles[0].length; j++) {
System.out.print(miles[i][j] + " ");
}
System.out.println();
}
// Displaying total miles ran in each week by each runner and average
System.out.println(" Name Total Miles Average");
System.out.println(" ----- --------- -----");
for (i = 0; i < miles.length; i++) {
System.out.printf("%10s", names[i] + " ");
for (j = 0; j < miles[0].length; j++) {
totWeek += miles[i][j];
}
average = (double) totWeek / miles[0].length;
System.out.printf("%d %.2f ", totWeek, average);
totWeek = 0;
}
}
}
__________________
Output:
Day1 Day2 Day3 Day4 Day5 Day6 Day7
Jason 5 6 3 4 8 3 4
Samantha 3 4 5 6 3 4 5
Ravi 3 4 5 6 5 5 5
Sheila 5 5 4 3 4 2 2
Ankit 7 6 5 7 6 5 6
Name Total Miles Average
----- --------- -----
Jason 33 4.71
Samantha 30 4.29
Ravi 33 4.71
Sheila 25 3.57
Ankit 42 6.00
___________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.