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

Totals: Monday: 2600.0 Tuesday: 2495.0 Wednesday: 2450.0 Thursday: 3300.0 Friday

ID: 3881738 • Letter: T

Question

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();
}
  

}

Explanation / 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();
}

}

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