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

The following code asks for entry of calories values: System.out.print(\"Please

ID: 3532097 • Letter: T

Question

The following code asks for entry of calories values:

System.out.print("Please enter total calories for ");

System.out.print (c.getDayName(i) + "==>");

calories = inputReader.nextInt();

c.setDailyValue(i,calories);

I am of the opinion the contructor in the 2nd program called should get the data entered. However. I get only 0 values in all fields. The following is my contructor code:

public CalorieCounter ()

{

int CalorieCounter;

for (int i = 0; i < WeekDays; i++)

CalorieCounter = dailyValues[i];

The following should access the object data.

public int getDailyValue(int index)

{

if (index < 0 || index >= WeekDays)

return -1;

return dailyValues[index];

I only get zero's when the main program is run. Both programs compile correctly and instructor provided part of the code. I am desparete to get rid of the 0 entries. What can be wrong?

Explanation / Answer

/* problem:-- actually you are using default constructor. For entering data through constructor ,you have to define a parameterize constructor ,means constructor should have a parameter .Now for this problem the paramter can be an array of integers which stores the daily calories values and pass this array to the constructor . i have created a code for your in which i have shown how you can use the concept explained above*/


import java.util.Scanner;


public class CalorieCounter

{

int dailyValues[];

static String dayName[]={"sunday","Monday","Tuesday","Wednesday","thrusday","Friday","Saturday"};


// default constructor


CalorieCounter()

{

dailyValues=new int[7];

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

dailyValues[i]=0;


}

// parameterized constructor taking an array dailyCaloriesValues[] as a parameter


CalorieCounter(int dailyCalorieValues[])

{ dailyValues=new int[7];

for(int index=0;index<7;index++)

dailyValues[index]=dailyCalorieValues[index];

}


public String getDayName(int index)

{

if(index>=0 && index<=6)

return dayName[index];

else

return null;

}


public void setDailyVale(int index,int value)

{

if(index>=0 && index<=6)

dailyValues[index]=value;

}

public int getDailyValue(int index)

{ if(index>=0 && index<=6)

return dailyValues[index];

return -1;

}


public int getTotalCalories()

{

int calorieCounter=0;

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

calorieCounter=calorieCounter+dailyValues[i];

return calorieCounter;

}




public static void main(String args[])

{

// create an array which will contain daily calories values


int dailyCalorieValues[]={100,60,70,50,30,20,40};


Scanner in= new Scanner(System.in);

//pass the array as a parameter to constructor


CalorieCounter obj=new CalorieCounter(dailyCalorieValues);


//display calories at day basis


System.out.println(" daily calorie values are");

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

System.out.println(obj.getDayName(i)+"==>"+obj.getDailyValue(i));


//dispaly total calories

System.out.println("total calories="+obj.getTotalCalories());





}

}

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