From: Java Programming: From Problem Analysis to Program Design, 5th ed. Ch. 9,
ID: 3704951 • Letter: F
Question
From: Java Programming: From Problem Analysis to Program Design, 5th ed.
Ch. 9, page 636, Problem Exercise 12:
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.)
I need this as a Java program.
data.txt file contains
Sample Output
Thank you!
Explanation / Answer
ReadDataDisplayAvg.java
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class ReadDataDisplayAvg {
public static void main(String[] args) throws FileNotFoundException {
String names[] = new String[5];
int miles[][] = new int[5][7];
readData(names, miles);
displayData(names, miles);
}
public static void displayData(String names[], int miles[][]) {
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Average");
for(int i=0;i<miles.length;i++) {
System.out.println(names[i]+" "+miles[i][0]+" "+miles[i][1]+" "+miles[i][2]+" "+miles[i][3]+" "+miles[i][4]+" "+miles[i][5]+" "+miles[i][6]+" "+df.format(average(miles[i])));
}
}
public static double average(int miles[]) {
int sum = 0;
for(int i=0;i<miles.length;i++) {
sum+=miles[i];
}
return sum/(double)miles.length;
}
public static void readData(String names[], int miles[][]) throws FileNotFoundException {
File file = new File("D:\data.txt");
if(file.exists()) {
Scanner scan = new Scanner(file);
int i= 0;
while(scan.hasNextLine()) {
names[i]=scan.next();
miles[i][0]=scan.nextInt();
miles[i][1]=scan.nextInt();
miles[i][2]=scan.nextInt();
miles[i][3]=scan.nextInt();
miles[i][4]=scan.nextInt();
miles[i][5]=scan.nextInt();
miles[i][6]=scan.nextInt();
i++;
}
} else {
System.out.println("File does not exist");
}
}
}
Output:
Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Average
Jason 10 15 20 25 18 20 26 19.14
Samantha 15 18 29 16 26 20 23 21.00
Ravi 20 26 18 29 10 12 20 19.29
Sheila 17 20 15 26 18 25 12 19.00
Ankit 16 8 28 20 11 25 21 18.43
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.