Re: Java constructor/object
So, what did you expect or intend it to print?
I notice that getAnnualDis() - the thing that is printed - returns distanceAnnual and that there are exactly two places where this variable is given a value: the constructor and the annualDis() method. Use System.out.println() to see when or if the value of distanceAnnual changes.
Code :
public class AnnualFuelUse
{
//Variables
private int distanceDay;
private double amountAYear , distanceAnnual ;
//Constructor
AnnualFuelUse(int distance20Day, double timePerYear)
{
distanceDay = distance20Day;
amountAYear = timePerYear;
System.out.println("Constructor setting distanceAnnual to 0.0");
distanceAnnual= 0.00 ;
}
//Distance annual projection
public void annualDis()
{
System.out.println("annualDis setting distanceAnnual to " + (distanceDay + amountAYear));
distanceAnnual = (distanceDay + amountAYear);
}
//Return distance annual
public double getAnnualDis()
{
return distanceAnnual;
}
}
Re: Java constructor/object
What variable's value is being printed? Where does that variable get assigned a value?
Is the code that assigns the variable a value executed before its value is printed?
Re: Java constructor/object
@pbrockway2, wouldn't the value of distanceAnnual change to what ever (distanceDay + amountAYear) is and then return that value? Could you explain why it return 0 ? Also, how can I make it display System.out.println("annualDis setting distanceAnnual to " + (distanceDay + amountAYear)); ? Thanks
** Is it because I haven't invoke the annualDis method?
--- Update ---
Quote:
Originally Posted by
Norm
What variable's value is being printed? Where does that variable get assigned a value?
Is the code that assigns the variable a value executed before its value is printed?
The variable value that I'm trying to print is distanceAnnual in the getAnnualDis() method. This variable get assigned 0.00 in the constructor then it is assign the value of distanceDay + amountAYeear in the AnnualDis() method. - I think I haven't invoke the method where distanceAnnual is assigned a new value that is why.
--- Update ---
I got it now. Thanks you so much!
Re: Java constructor/object
Quote:
haven't invoke the method where distanceAnnual is assigned a new value
You need to call that method to have some value put into the variable before printing it.
Re: Java constructor/object
Quote:
wouldn't the value of distanceAnnual change to what ever (distanceDay + amountAYear) is and then return that value?
It isn't a question of "wouldn't...?", it's a question of "does it ... or not?". In other words the task at this point is to see what is going on. The code I posted - which is just yours with a couple of lines added - should answer this question. Just add those two lines, compile and run.
At that point you should be able to answer Norm's three questions.
Quote:
Could you explain why it return 0 ?
Well, it returns zero because that's the value of distanceAnnual. Run the (ammended) code to see why. Or, if it's still not clear, describe what it does output.
[Edit] Massively slow :( Well done! Yes, you have to call a method for it to do its work.