10-points Due 2 Background Your employer, Fabulous Weight-loss Formula, has prov
ID: 3740135 • Letter: 1
Question
10-points Due 2 Background Your employer, Fabulous Weight-loss Formula, has provided you a data file for analysis. Your task is to write a program to compute weekly weight loss/gain and total weight loss for the 10-week program. Participants are required to weigh-in 5-days each week. The data file consists of a name, followed by 10 records of comma-separated data. Here is a partial example of the data: 108,106,110,111,106 111,106,111,112,106 111,110,112,107,110 106,111,108,106,106 109,111,108,106,111 108,107,107,111,107 108,112,108,106,107 108,110,107,112,107 106,109,109,107,108 106,109,111,112,106 180,181,183,178,182 184,180,182,178,184 182,180,181,182,183 178,181,178,179,181 184,183,178,184,181 181,182,184,180,183 182,182,182,178,182 181,183,180,180,184 179,184,180,182,181 181,184,184,179,183 he numeric data from the data7.csv file into a Create a Java class named fwf. The program will read all t three-dimensional array and the names intoa three-dimensional array is (person][week][day] Once the data is in the arrays, the program will do the following 1. For each participant: a. print the participant's name. b. Print the daily weigh-in weights c. Print the weekly gain/loss (last weight of the week-starting weight) d. After all weeks are printed for a participant, print the total weight gain/loss (final weight - starting weight) Page 1 of 3Explanation / Answer
Hello, I have created the required fwf class according to your needs. Loaded data from a file data7.csv into a 3d array and an array of names, then looped through it and displayed the stats. Check the comments for a better understanding of the working. Thanks
// fwf.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class fwf {
// 3d array for storing data
static int data[][][];
// array for storing names
static String names[];
// current number of persons in the array
static int countPerson;
// constants for number of weeks, days and maximum number of people
static final int WEEKS = 10, DAYS = 5, MAX_PERSONS = 100;
public static void main(String[] args) {
try {
// initializing scanner to read file
Scanner scanner = new Scanner(new File("data7.csv"));
/**
* Initializing arrays with maximum capacity
*/
data = new int[MAX_PERSONS][WEEKS][DAYS];
names = new String[MAX_PERSONS];
/**
* looping through all records in the file
*/
while (scanner.hasNext()) {
// fetching name
String name = scanner.nextLine();
names[countPerson] = name;
/**
* looping through 10 weeks
*/
for (int i = 0; i < WEEKS; i++) {
String line = scanner.nextLine();
/**
* Dividing each line by comma
*/
String values[] = line.split(",");
/**
* looping through each days and adding the weight to array
*/
for (int j = 0; j < values.length; j++) {
int weight = Integer.parseInt(values[j]);
data[countPerson][i][j] = weight;
}
}
/**
* incrementing number of persons recorded
*/
countPerson++;
}
/**
* Data is loaded, now its time to display the stats, looping
* through all person's records
*/
for (int i = 0; i < countPerson; i++) {
//displaying name
System.out.println(names[i]);
for (int j = 0; j < WEEKS; j++) {
for (int k = 0; k < DAYS; k++) {
//displaying weights
System.out.print(data[i][j][k] + " ");
}
//finding and printing weekly weight difference
int difference = data[i][j][DAYS - 1] - data[i][j][0];
System.out.printf("Week %d gain/loss %d lbs ", (j + 1),
difference);
}
//finding and printing final weight difference
int totalDifference = data[i][WEEKS - 1][DAYS - 1]
- data[i][0][0];
System.out.printf("Gain/loss for program: %d lbs ",
totalDifference);
}
} catch (FileNotFoundException e) {
System.out.println("Input file not found!");
} catch (Exception e) {
System.out.println(e);
}
}
}
//data7.csv
Johnson
108,106,110,111,106
111,106,111,112,106
111,110,112,107,110
106,111,108,106,106
109,111,108,106,111
108,107,107,111,107
108,112,108,106,107
108,110,107,112,107
106,109,109,107,108
106,109,111,112,106
Smith
180,186,180,181,186
181,186,181,182,186
171,170,177,177,170
177,172,170,171,172
169,168,169,168,168
168,165,164,163,160
162,167,169,172,179
174,170,165,160,159
166,160,165,162,160
165,160,162,158,159
/*OUTPUT*/
Johnson
108 106 110 111 106 Week 1 gain/loss -2 lbs
111 106 111 112 106 Week 2 gain/loss -5 lbs
111 110 112 107 110 Week 3 gain/loss -1 lbs
106 111 108 106 106 Week 4 gain/loss 0 lbs
109 111 108 106 111 Week 5 gain/loss 2 lbs
108 107 107 111 107 Week 6 gain/loss -1 lbs
108 112 108 106 107 Week 7 gain/loss -1 lbs
108 110 107 112 107 Week 8 gain/loss -1 lbs
106 109 109 107 108 Week 9 gain/loss 2 lbs
106 109 111 112 106 Week 10 gain/loss 0 lbs
Gain/loss for program: -2 lbs
Smith
180 186 180 181 186 Week 1 gain/loss 6 lbs
181 186 181 182 186 Week 2 gain/loss 5 lbs
171 170 177 177 170 Week 3 gain/loss -1 lbs
177 172 170 171 172 Week 4 gain/loss -5 lbs
169 168 169 168 168 Week 5 gain/loss -1 lbs
168 165 164 163 160 Week 6 gain/loss -8 lbs
162 167 169 172 179 Week 7 gain/loss 17 lbs
174 170 165 160 159 Week 8 gain/loss -15 lbs
166 160 165 162 160 Week 9 gain/loss -6 lbs
165 160 162 158 159 Week 10 gain/loss -6 lbs
Gain/loss for program: -21 lbs
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.