You are initializing wammy
with the result of pricePerHundredGrams
, but price
and weightGrams
haven't been initialized yet, so they're both 0
. For double
arithmetic, 0
divided by 0
is NaN
(it's indeterminate in math).
Initialize wammy
after price
and weightGrams
have valid values in your constructor:
public Candy(String n, int cal, double p, double wG)
{
name = n;
calories = cal;
price = p;
weightGrams = wG;
// Initialize wammy here.
wammy = pricePerHundredGrams(price, weightGrams);
}
Additionally, since they are already instance variables, you don't need to pass price
and weightGrams
as parameters to pricePerHundredGrams
.