# Assignment #2 Solution

\$30.00

Category:

## Description

Objective: The goal of this assignment is to practice two-dimensional arrays.

Background: A nutritionist has contracted you to create a software that will help clients count calories consumed.

Assignment (Part 1: 75 points): The client will provide data in the form of a text file. Each line will contain three numbers separated by spaces. The numbers represent the number of calories consumed for breakfast, lunch and dinner, respectively. You may assume that the file includes data for exactly one week (i.e., seven lines represent seven days Monday to Sunday). Your program should read the data from the file into a 2-dimensional array that has seven rows and three columns, where each row will keep record of calories consumed for all the three meals of a day. Then, compute and print out the following information:

• a list of the total number of calories consumed each day from Monday to Sunday

• the average number of calories consumed each day

• the average number of calories (average over the week) consumed in each of the meals

• the maximum number of calories consumed each day

• the maximum number of calories consumed in each meal type

Your program must have separate methods for reading in the data and for printing each of the different quantities given above. Please do not just put all of your code into the main method.

Example input file: Your program must read the input from a file named input1.txt

200 1000 800

450 845 1200

800 250 400

0 1500 1800

600 500 1000

700 1400 1700

675 400 900

You should print an error message and terminate if there are not exactly three numbers on each line, and exactly 7 lines overall. A maximum of 3 points will be reduced if your code does not handle these file reading errors.

Assignment (Part 2: 25 points): The client comes back and would like you to make the program more general, so that it can handle any number of meals in any of the days of a week. That is, you no longer know that there will be exactly three columns in the data; however, the number of lines is still seven. Consider that the first meal of the day is “Meal 1”, the second meal is “Meal 2”, so and so forth.

You may assume that a person eats at least once a day. That is, each row will have at least one number.

Example input file: Your program must read the input from a file named input2.txt

200 1000

450 845 1200 800

800

400 1500 1800 200

1. 1000

1. 1400 170

1. 400 100 400 300

Use the concept of 2-dimensional Ragged Array to solve the second part of the assignment. In the second part, the client is only interested in the following information now.

• the average number of calories consumed each day

• the average number of calories consumed in every meal of the week. For the example input file above the list of averages for Meals 1 to 5 is: 532.1428571, 1024.166667, 817.5, 466.6666667, and 300.

Your program must have separate methods for reading in the data and for printing each of the different quantities given above.

You should print an error message and terminate if there are not exactly 7 lines in the input file. A maximum of 2 points will be reduced if your code does not handle the file reading error.

Deliverables: You are expected to submit two Java files (Calories1.java for part 1 and Calories2.java for part 2) via Blackboard. Notice that Calories1.java should not work if the input file has a ragged input set.

However, Calories2.java should be generic enough such that it would work for ragged or rectangular matrix in the input file.

Your TA will instruct you with further details.

error: Content is protected !!