I need this program to run, but I am unable to run it. It states that is missing
ID: 3586543 • Letter: I
Question
I need this program to run, but I am unable to run it.
It states that is missing the main method.
Please Help with how to properly place it in the compiler and run it successfully.
CalorieTracker.java
import java.util.*;
public class CalorieTracker
{
int calories[][] = new int [7][4];
public void setCalories()
{
Scanner sc = new Scanner(System.in);
int input;
for(int i=0;i<7;i++)
for(int j=0;j<4;j++)
{
System.out.println("Enter calories consumed in Day "+(i+1)+" meal "+(j+1)+":");
input= sc.nextInt();
if(input>0)
calories[i][j]=input;
else
{
j--;
System.out.println("INVALID INPUT.ENTER AGAIN");
}
}
}
public void getCalories()
{
int dayTotal=0,weekTotal=0,biggest=0,smallest=5000;
for(int i=0;i<7;i++)
{
System.out.println("Day "+(i+1)+":");
for(int j=0;j<4;j++)
{
System.out.print(" Meal "+(j+1)+": "+calories[i][j]);
weekTotal+=calories[i][j];
dayTotal+=calories[i][j];
if(calories[i][j]>biggest)
biggest=calories[i][j];
if(calories[i][j]<smallest)
smallest=calories[i][j];
}
System.out.println(" Total Calories on day "+(i+1)+": "+dayTotal);
dayTotal=0;
}
System.out.println("Total Calories in the week: "+weekTotal);
System.out.println("Smallest calories consumed in a single meal: "+smallest);
System.out.println("Largest calories consumed in a single meal: "+biggest);
}
}
Driver.java
public class Driver {
public static void main(String[] args)
{
CalorieTracker ct = new CalorieTracker();
ct.setCalories();
ct.getCalories();
}
}
Explanation / Answer
Hi,
I dont see any issue with the code. Please save the file names as below.
Place both the files in same folder and run the Driver class.
In you are running in cmd, Execute below statements first.
> javac *.java
> java Driver
Driver.java
public class Driver {
public static void main(String[] args)
{
CalorieTracker ct = new CalorieTracker();
ct.setCalories();
ct.getCalories();
}
}
CalorieTracker.java
import java.util.*;
public class CalorieTracker
{
int calories[][] = new int [7][4];
public void setCalories()
{
Scanner sc = new Scanner(System.in);
int input;
for(int i=0;i<7;i++)
for(int j=0;j<4;j++)
{
System.out.println("Enter calories consumed in Day "+(i+1)+" meal "+(j+1)+":");
input= sc.nextInt();
if(input>0)
calories[i][j]=input;
else
{
j--;
System.out.println("INVALID INPUT.ENTER AGAIN");
}
}
}
public void getCalories()
{
int dayTotal=0,weekTotal=0,biggest=0,smallest=5000;
for(int i=0;i<7;i++)
{
System.out.println("Day "+(i+1)+":");
for(int j=0;j<4;j++)
{
System.out.print(" Meal "+(j+1)+": "+calories[i][j]);
weekTotal+=calories[i][j];
dayTotal+=calories[i][j];
if(calories[i][j]>biggest)
biggest=calories[i][j];
if(calories[i][j]<smallest)
smallest=calories[i][j];
}
System.out.println(" Total Calories on day "+(i+1)+": "+dayTotal);
dayTotal=0;
}
System.out.println("Total Calories in the week: "+weekTotal);
System.out.println("Smallest calories consumed in a single meal: "+smallest);
System.out.println("Largest calories consumed in a single meal: "+biggest);
}
}
Output:
Enter calories consumed in Day 1 meal 1:
22
Enter calories consumed in Day 1 meal 2:
33
Enter calories consumed in Day 1 meal 3:
44
Enter calories consumed in Day 1 meal 4:
55
Enter calories consumed in Day 2 meal 1:
66
Enter calories consumed in Day 2 meal 2:
77
Enter calories consumed in Day 2 meal 3:
88
Enter calories consumed in Day 2 meal 4:
99
Enter calories consumed in Day 3 meal 1:
11
Enter calories consumed in Day 3 meal 2:
12
Enter calories consumed in Day 3 meal 3:
13
Enter calories consumed in Day 3 meal 4:
14
Enter calories consumed in Day 4 meal 1:
15
Enter calories consumed in Day 4 meal 2:
16
Enter calories consumed in Day 4 meal 3:
17
Enter calories consumed in Day 4 meal 4:
18
Enter calories consumed in Day 5 meal 1:
21
Enter calories consumed in Day 5 meal 2:
22
Enter calories consumed in Day 5 meal 3:
23
Enter calories consumed in Day 5 meal 4:
24
Enter calories consumed in Day 6 meal 1:
25
Enter calories consumed in Day 6 meal 2:
26
Enter calories consumed in Day 6 meal 3:
27
Enter calories consumed in Day 6 meal 4:
28
Enter calories consumed in Day 7 meal 1:
22
Enter calories consumed in Day 7 meal 2:
33
Enter calories consumed in Day 7 meal 3:
44
Enter calories consumed in Day 7 meal 4:
55
Day 1:
Meal 1: 22 Meal 2: 33 Meal 3: 44 Meal 4: 55 Total Calories on day 1: 154
Day 2:
Meal 1: 66 Meal 2: 77 Meal 3: 88 Meal 4: 99 Total Calories on day 2: 330
Day 3:
Meal 1: 11 Meal 2: 12 Meal 3: 13 Meal 4: 14 Total Calories on day 3: 50
Day 4:
Meal 1: 15 Meal 2: 16 Meal 3: 17 Meal 4: 18 Total Calories on day 4: 66
Day 5:
Meal 1: 21 Meal 2: 22 Meal 3: 23 Meal 4: 24 Total Calories on day 5: 90
Day 6:
Meal 1: 25 Meal 2: 26 Meal 3: 27 Meal 4: 28 Total Calories on day 6: 106
Day 7:
Meal 1: 22 Meal 2: 33 Meal 3: 44 Meal 4: 55 Total Calories on day 7: 154
Total Calories in the week: 950
Smallest calories consumed in a single meal: 11
Largest calories consumed in a single meal: 99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.