Answered Essay: Monday: 2600.0

Totals:

Monday: 2600.0
Tuesday: 2495.0
Wednesday: 2450.0
Thursday: 3300.0
Friday: 2100.0
Saturday: 3800.0
Sunday: 1975.0

Days with more calories than needed:

Monday: 2600.0
Tuesday: 2495.0
Wednesday: 2450.0
Thursday: 3300.0
Saturday: 3800.0

Average calories consumed during each meal:

Breakfast: 717.8571428571429
Lunch: 842.1428571428571
Dinner: 1114.2857142857142

——————————————

However, I’m trying to change the parameters of my printExceededDays() method to recevie the arrays:double[] breakfast, double[] lunch, double[] dinner just as how they are in the other methods. When I try to do so, it won’t print “Days with more calories than needed: “
Please help! I need to fix this ASAP. Thanks in advance

—————————————————————–

input file:

800 1000 800
450 845 1200
1800 250 400
0 1500 1800
600 500 1000
700 1400 1700
675 400 900

—————————————–

import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Calories {

Scanner s;

//Arrays in which the corresponding numbers will be stored
double[] breakfast = new double[7];
double[] lunch = new double[7];
double[] dinner = new double[7];

String[] days = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

int countLines = 0;

public void readFile(){
try{
s = new Scanner(new File(“input.txt”));
while(s.hasNextLine()){
String line = s.nextLine();
Scanner lineScanner = new Scanner(line);
//Will check if there are exactly 3 numbers per line
//and split them into three arrays
String[] numbers = line.split(” “);

if(numbers.length == 3){
fillArrays(lineScanner);
} else{
System.out.println(“One line does not have exactly 3 numbers”);
System.exit(0); //Will terminate the code if error found

}
}
//
calculateLines();//method that cointains the operational methods

//exceptions
} catch (FileNotFoundException ex){
System.out.println(“This file does not exist”);

} catch (IndexOutOfBoundsException e){
System.out.println(“Thie file has more than 7 lines”);

} catch (InputMismatchException e){
System.out.println(“There’s an invalid value in the file, please check that there are only numbers in this file”);

}
}
//Method that will store each meal to its own array
//receives such parameter that contains the file’s info
public void fillArrays(Scanner lineScanner){
for(int i = 0; i < 3; i++){

double d = lineScanner.nextDouble();

if ( i == 0){
breakfast[countLines] =d;
} else if ( i == 1){
lunch[countLines]=d;
} else{
dinner[countLines]=d;

}

}
countLines++;

}

public void calculateLines(){
if(countLines == 7){
double [] totals = calculateTotals(breakfast, lunch, dinner);
printExceededDays(totals, days);
printAverages(breakfast, lunch, dinner);

} else
System.out.println(“The file has less than 7 lines”);
}

static double[] calculateTotals(double[] breakfast, double[] lunch, double[] dinner){

double[] totals = new double[7];
String[] days = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

System.out.println(“Totals: “);
System.out.println();
for(int i = 0; i < totals.length; i++){
totals[i] = breakfast[i] + lunch[i] + dinner[i];
System.out.println(days[i] + “: ” + totals[i]);

}
return totals;
}

static void printExceededDays(double[] totals, String[] days){
System.out.println();
System.out.println(“Days with more calories than needed: “);
System.out.println();

for(int i = 0; i if(totals[i] > 2250)
System.out.println(days[i] + “: ” + totals[i]);
}
}

static double[] printAverages(double[] breakfast, double[] lunch, double[] dinner){
double[] totals = new double[7];
//Variables to calculate average and sum of each meal
double avgBreakfast, avgLunch, avgDinner;
double sumBreakfast = 0, sumLunch = 0, sumDinner = 0;

for(int i = 0; i < totals.length; i++){
sumBreakfast += breakfast[i];
sumLunch += lunch[i];
sumDinner += dinner[i];

}

avgBreakfast = sumBreakfast / 7;
avgLunch = sumLunch / 7;
avgDinner = sumDinner / 7;

System.out.println();
System.out.println(“Average calories consumed during each meal: “);
System.out.println();
System.out.println(“Breakfast: ” + avgBreakfast);
System.out.println(“Lunch: ” + avgLunch);
System.out.println(“Dinner: ” + avgDinner);

return null;
}

public static void main(String[] args) {
Calories calories = new Calories();
calories.readFile();
}

}

Expert Answer

 

import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Calories {

Scanner s;

//Arrays in which the corresponding numbers will be stored
double[] breakfast = new double[7];
double[] lunch = new double[7];
double[] dinner = new double[7];

String[] days = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

int countLines = 0;

public void readFile(){
try{
s = new Scanner(new File(“input.txt”));
while(s.hasNextLine()){
String line = s.nextLine();
Scanner lineScanner = new Scanner(line);
//Will check if there are exactly 3 numbers per line
//and split them into three arrays
String[] numbers = line.split(” “);

if(numbers.length == 3){
fillArrays(lineScanner);
} else{
System.out.println(“One line does not have exactly 3 numbers”);
System.exit(0); //Will terminate the code if error found

}
}
//
calculateLines();//method that cointains the operational methods

//exceptions
} catch (FileNotFoundException ex){
System.out.println(“This file does not exist”);

} catch (IndexOutOfBoundsException e){
System.out.println(“Thie file has more than 7 lines”);

} catch (InputMismatchException e){
System.out.println(“There’s an invalid value in the file, please check that there are only numbers in this file”);

}
}
//Method that will store each meal to its own array
//receives such parameter that contains the file’s info
public void fillArrays(Scanner lineScanner){
for(int i = 0; i < 3; i++){

double d = lineScanner.nextDouble();

if ( i == 0){
breakfast[countLines] =d;
} else if ( i == 1){
lunch[countLines]=d;
} else{
dinner[countLines]=d;

}

}
countLines++;

}

public void calculateLines(){
if(countLines == 7){
double [] totals = calculateTotals(breakfast, lunch, dinner);
printExceededDays(totals, days);
printAverages(breakfast, lunch, dinner);

} else
System.out.println(“The file has less than 7 lines”);
}

static double[] calculateTotals(double[] breakfast, double[] lunch, double[] dinner){

double[] totals = new double[7];
String[] days = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

System.out.println(“Totals: “);
System.out.println();
for(int i = 0; i < totals.length; i++){
totals[i] = breakfast[i] + lunch[i] + dinner[i];
System.out.println(days[i] + “: ” + totals[i]);

}
return totals;
}

static void printExceededDays(double[] totals, String[] days){
System.out.println();
System.out.println(“Days with more calories than needed: “);
System.out.println();

for(int i = 0; i<totals.length;i++){
if(totals[i] > 2250)
System.out.println(days[i] + “: ” + totals[i]);
}
}

static double[] printAverages(double[] breakfast, double[] lunch, double[] dinner){
double[] totals = new double[7];
//Variables to calculate average and sum of each meal
double avgBreakfast, avgLunch, avgDinner;
double sumBreakfast = 0, sumLunch = 0, sumDinner = 0;

for(int i = 0; i < totals.length; i++){
sumBreakfast += breakfast[i];
sumLunch += lunch[i];
sumDinner += dinner[i];

}

avgBreakfast = sumBreakfast / 7;
avgLunch = sumLunch / 7;
avgDinner = sumDinner / 7;

System.out.println();
System.out.println(“Average calories consumed during each meal: “);
System.out.println();
System.out.println(“Breakfast: ” + avgBreakfast);
System.out.println(“Lunch: ” + avgLunch);
System.out.println(“Dinner: ” + avgDinner);

return null;
}

public static void main(String[] args) {
Calories calories = new Calories();
calories.readFile();
}

}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?