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

This is Java Assignment (Part 1: 75 points): The client will provide data in the

ID: 3746740 • Letter: T

Question

This is Java 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 inputl.txt 200 1000 800 450 845 1200 800 250 400 0 1500 1800 600 500 1000 700 1400 1700 675 400 900

Explanation / Answer

package com.org.calorie;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class CalorieCalculator {

public static void main(String[] args) throws IOException {

String location = "C:\Downloads\input1.txt"; // Full Path to input file

int[][] weeklyData = readDataFromFile(location);

if(weeklyData != null) {

listCaloriesForEachDay(weeklyData);

listAverageCaloriesEachDay(weeklyData);

listAverageCaloriesPerMeal(weeklyData);

listMaximumCaloriesConsumedEachDay(weeklyData);

listMaximumCaloriesConsumedInEachMeal(weeklyData);

}

else {

System.out.println("Input file not read correctly!! Please Check the location specified.");

}

}

private static void listMaximumCaloriesConsumedInEachMeal(int[][] weeklyData) {

int breakfastMaximum = Integer.MIN_VALUE;

int lunchMaximum = Integer.MIN_VALUE;

int dinnerMaximum = Integer.MIN_VALUE;

for (int i = 0; i < 7; i++) {

if (breakfastMaximum < weeklyData[i][0]) {

breakfastMaximum = weeklyData[i][0];

}

if (lunchMaximum < weeklyData[i][1]) {

lunchMaximum = weeklyData[i][1];

}

if (dinnerMaximum < weeklyData[i][2]) {

dinnerMaximum = weeklyData[i][2];

}

}

System.out.println("Maximum calories for breakfast over the week = " + breakfastMaximum);

System.out.println("Maximum calories for lunch over the week = " + lunchMaximum);

System.out.println("Maximum calories for dinner over the week = " + dinnerMaximum);

}

private static void listMaximumCaloriesConsumedEachDay(int[][] weeklyData) {

int dailyTotal;

int maxCalories = Integer.MIN_VALUE;

for (int i = 0; i < 7; i++) {

dailyTotal = 0;

for (int j = 0; j < 3; j++) {

dailyTotal += weeklyData[i][j];

}

if (dailyTotal > maxCalories) {

maxCalories = dailyTotal;

}

}

System.out.println("Maximum calories consumed ina day over the week are = " + maxCalories);

}

private static void listAverageCaloriesPerMeal(int[][] weeklyData) {

int breakfastTotal = 0;

int lunchTotal = 0;

int dinnerTotal = 0;

for (int i = 0; i < 7; i++) {

breakfastTotal += weeklyData[i][0];

lunchTotal += weeklyData[i][1];

dinnerTotal += weeklyData[i][2];

}

System.out.println("Average calories over the week for breakfast = " + (float) breakfastTotal / 7);

System.out.println("Average calories over the week for lunch = " + (float) lunchTotal / 7);

System.out.println("Average calories over the week for dinner = " + (float) dinnerTotal / 7);

}

private static void listAverageCaloriesEachDay(int[][] weeklyData) {

int weeklyTotal = 0;

for (int i = 0; i < 7; i++) {

for (int j = 0; j < 3; j++) {

weeklyTotal += weeklyData[i][j];

}

}

System.out.println("Average calories per day = " + (float) weeklyTotal / 7);

}

private static void listCaloriesForEachDay(int[][] weeklyData) {

int dailyTotal;

for (int i = 0; i < 7; i++) {

dailyTotal = 0;

for (int j = 0; j < 3; j++) {

dailyTotal += weeklyData[i][j];

}

System.out.println("Daily total for day " + i + " = " + dailyTotal);

}

}

private static int[][] readDataFromFile(String location) throws IOException {

int[][] input = new int[7][3];

String line = null;

int i = 0;

try (BufferedReader br = new BufferedReader(new FileReader(new File(location)))) {

while ((line = br.readLine()) != null) {

for (int j = 0; j < 3; j++) {

String[] dailyValues = line.split(" "); // Split the calories of each meal on the basis of a space

input[i][j] = Integer.parseInt(dailyValues[j]);

}

i++;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

}

return input;

}

}

package com.org.calorie;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class CalorieCalculator {

public static void main(String[] args) throws IOException {

String location = "C:\Downloads\input1.txt"; // Full Path to input file

int[][] weeklyData = readDataFromFile(location);

if(weeklyData != null) {

listCaloriesForEachDay(weeklyData);

listAverageCaloriesEachDay(weeklyData);

listAverageCaloriesPerMeal(weeklyData);

listMaximumCaloriesConsumedEachDay(weeklyData);

listMaximumCaloriesConsumedInEachMeal(weeklyData);

}

else {

System.out.println("Input file not read correctly!! Please Check the location specified.");

}

}

private static void listMaximumCaloriesConsumedInEachMeal(int[][] weeklyData) {

int breakfastMaximum = Integer.MIN_VALUE;

int lunchMaximum = Integer.MIN_VALUE;

int dinnerMaximum = Integer.MIN_VALUE;

for (int i = 0; i < 7; i++) {

if (breakfastMaximum < weeklyData[i][0]) {

breakfastMaximum = weeklyData[i][0];

}

if (lunchMaximum < weeklyData[i][1]) {

lunchMaximum = weeklyData[i][1];

}

if (dinnerMaximum < weeklyData[i][2]) {

dinnerMaximum = weeklyData[i][2];

}

}

System.out.println("Maximum calories for breakfast over the week = " + breakfastMaximum);

System.out.println("Maximum calories for lunch over the week = " + lunchMaximum);

System.out.println("Maximum calories for dinner over the week = " + dinnerMaximum);

}

private static void listMaximumCaloriesConsumedEachDay(int[][] weeklyData) {

int dailyTotal;

int maxCalories = Integer.MIN_VALUE;

for (int i = 0; i < 7; i++) {

dailyTotal = 0;

for (int j = 0; j < 3; j++) {

dailyTotal += weeklyData[i][j];

}

if (dailyTotal > maxCalories) {

maxCalories = dailyTotal;

}

}

System.out.println("Maximum calories consumed ina day over the week are = " + maxCalories);

}

private static void listAverageCaloriesPerMeal(int[][] weeklyData) {

int breakfastTotal = 0;

int lunchTotal = 0;

int dinnerTotal = 0;

for (int i = 0; i < 7; i++) {

breakfastTotal += weeklyData[i][0];

lunchTotal += weeklyData[i][1];

dinnerTotal += weeklyData[i][2];

}

System.out.println("Average calories over the week for breakfast = " + (float) breakfastTotal / 7);

System.out.println("Average calories over the week for lunch = " + (float) lunchTotal / 7);

System.out.println("Average calories over the week for dinner = " + (float) dinnerTotal / 7);

}

private static void listAverageCaloriesEachDay(int[][] weeklyData) {

int weeklyTotal = 0;

for (int i = 0; i < 7; i++) {

for (int j = 0; j < 3; j++) {

weeklyTotal += weeklyData[i][j];

}

}

System.out.println("Average calories per day = " + (float) weeklyTotal / 7);

}

private static void listCaloriesForEachDay(int[][] weeklyData) {

int dailyTotal;

for (int i = 0; i < 7; i++) {

dailyTotal = 0;

for (int j = 0; j < 3; j++) {

dailyTotal += weeklyData[i][j];

}

System.out.println("Daily total for day " + i + " = " + dailyTotal);

}

}

private static int[][] readDataFromFile(String location) throws IOException {

int[][] input = new int[7][3];

String line = null;

int i = 0;

try (BufferedReader br = new BufferedReader(new FileReader(new File(location)))) {

while ((line = br.readLine()) != null) {

for (int j = 0; j < 3; j++) {

String[] dailyValues = line.split(" "); // Split the calories of each meal on the basis of a space

input[i][j] = Integer.parseInt(dailyValues[j]);

}

i++;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

}

return input;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote