In Java Calorie intake data from a person is provided in a text file named food.
ID: 3875638 • Letter: I
Question
In Java
Calorie intake data from a person is provided in a text file named food.txt. There are three numbers on each line, separated by spaces. The numbers represent the number of calories consumed for breakfast, lunch and dinner. The file includes data for exactly one week starting from Monday. That is, the file contains seven lines of text. The topmost line is for Monday and the line at the bottom is for Sunday. Your program should read the data from the file into three separate arrays, one for breakfast, one for lunch, and one for dinner. Then, compute and print out the following information: 1. a list of total calories consumed each day 2. a list of days when more than 2250 calories are consumed 3. average calories consumed during each of the three meals. YOU MUST WRITE A METHOD WITH EXACTLY THREE ARRAY PARAMETERS TO COMPUTE EACH OF THE THREE LISTED ITEMS ABOVE AND DISPLAY THE RESULT. YOU SHOULD PRINT AN ERROR MESSAGE AND TERMINATE IF THERE ARE NOT EXACLTY THREE NUMBERS ON EACH LINE AND EXACTLY 7 LINES OVERALL.
Explanation / Answer
package thread;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Calorie {
public static void totalCalariesPerDay(int[] breakFast, int[] lunch, int[] dinner)
{
for(int i=0;i<7;i++)
{
int sum=breakFast[i]+lunch[i]+dinner[i];
System.out.println((i+1)+" day the calorie consumed is "+sum);
}
}
public static void moreCaloriesDay(int[] breakFast,int[] lunch,int[] dinner)
{
for(int i=0;i<7;i++)
{
int sum=breakFast[i]+lunch[i]+dinner[i];
if(sum>2250)
System.out.println((i+1)+" day the calories consumed more than 2250 ");
}
}
public static void avgCalories(int[] breakFast,int[] lunch,int[] dinner)
{
for(int i=0;i<7;i++)
{
int sum=breakFast[i]+lunch[i]+dinner[i];
double avg=sum/3.0;
System.out.println((i+1)+ " the avg calories consumed is "+avg);
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("food.txt");
int breakFast[]=new int[7];
int lunch[]=new int[7];
int dinner[]=new int[7];
int lineCount=0;
try {
BufferedReader b = new BufferedReader(new FileReader(file));
String Line = "";
while ((Line = b.readLine()) != null) {
String temp[]=Line.split(" ");
breakFast[lineCount]=Integer.parseInt(temp[0]);
lunch[lineCount]=Integer.parseInt(temp[1]);
dinner[lineCount]=Integer.parseInt(temp[2]);
lineCount++;
if(temp.length!=3)
{
System.out.println("There are less than 3 numbers in line");
System.exit(1);
}
}
if(lineCount!=7)
{
System.out.println("There are more than 7 lines in this file");
System.exit(1);
}
totalCalariesPerDay(breakFast,lunch,dinner);
avgCalories(breakFast, lunch, dinner);
moreCaloriesDay(breakFast, lunch, dinner);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.