Another newbie question...

Hi,

Well, I have a mulitplication problem, so it seems. The program reads the right values for the variable empHours and empRate. However when i try to calculate earnings, the result is $0.00...Here is the code...What am I missing? Shouldn't this be pretty straightfoward? Here is the output of the program...

The initial parameters are:

The employee's name is: Paul

Paul worked 4.0 hours this week.

Paul's hourly rate is 1.0

Paul earned $0.00 this week.

Press any key to continue . . .

publicclass empinfo

{

private String empName;//Employee name for this class

privatedouble empHours;

privatedouble empRate;

//constructor initializes name

public empinfo( String name,double hours,double rate)

{

empName = name;

empHours = hours;

empRate = rate;

}

// method to set the empName

publicvoid setEmpName( String name )

{

empName = name;//store the empName

}//end method

//method to set empHours

publicvoid setempHours (double hours )

{

empHours = hours;//store empHours

}//end method

//method to set empRate

publicvoid setempRate (double rate )

{

empRate = rate;// store empRate

}//end method

//method to retrieve empRate

publicdouble getEmpRate()

{

return empRate;

}//end method

// method to retirev empHours

publicdouble getEmpHours()

{

return empHours;

}//end method

// method to retrieve empName

public String getEmpName()

{

return empName;

}//end method

//method to calculate earnings

publicdouble earnings = empHours * empRate;

//method to retrieve earnings

publicdouble getEarnings()

{

return earnings;

}

// Display the employee info

publicvoid displayInfo()

{

System.out.printf("The employee's name is: %s\n", getEmpName() );

System.out.printf("%s worked %s hours this week.\n", getEmpName(), getEmpHours() );

System.out.printf("%s's hourly rate is %s\n", getEmpName(), getEmpRate() );

System.out.printf("%s earned $%.2f this week.", getEmpName(), getEarnings() );

}//end method displayInfo

}//end class empinfo

[4756 byte] By [PaulDoca] at [2007-11-26 18:50:47]
# 1

I'm not sure when you think this line will be executed: public double earnings = empHours * empRate;

but it will only happen once--when the object is created, before the constructor is run, that is, when hours and rate still have their default zero values.

For a calcualted value like that, don't store it. Just calculate it when the get method is called. Otherwise you have to keep it in sync every time one of its consituent values changes. For expensive calculations, it may be worth doing that for caching purposes, but you don't need that here.

jverda at 2007-7-9 6:24:50 > top of Java-index,Java Essentials,New To Java...
# 2

Is that because the program reads this info while executing the main program?

So if I write this in a method within this class and call it from the main program, will that work? I have to use this class and a method from within that class to calculate the rate...

Thanks for the help.

PaulDoca at 2007-7-9 6:24:50 > top of Java-index,Java Essentials,New To Java...
# 3

> Is that because the program reads this info while

> executing the main program?

Member variable initializers such as that are executed once, during construction, before the constructor is run.

> So if I write this in a method within this class and

> call it from the main program, will that work? I have

> to use this class and a method from within that class

> to calculate the rate...

Yes, if, instead of having an earnings variable, you just call a getEarnings() method that returns rate * hours, that will work. Note that you're calculating earnings, not rate. Rate is just set directly.

jverda at 2007-7-9 6:24:50 > top of Java-index,Java Essentials,New To Java...
# 4
That does it. Thank you very much. I'm sure I'll be back....
PaulDoca at 2007-7-9 6:24:50 > top of Java-index,Java Essentials,New To Java...