Expected output is incorrect!

Hi everybody. I am writing a program that calculates the amount of Carbon Dioxide produced in a year from waste by a few selected households and comparing how recycling can reduce the CO2 footprint. However, I believe the code is correct; I am just not getting the expected output. So, something must be hosed; however, I can't figure it out. For anybody that might know the problem, I have pasted the two classes below. Thanks

Minor point:
Never write if (b == true). You just write if (b) Similarly never write if (b == false), only if (!b)

Not only is that method shorter, it also avoids the risk of nasty errors if you mistakenly write = instead of ==.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44016

33

posted Mar 20, 2009 11:55:23

0

And where is the bit about triangles?

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 84

posted Mar 20, 2009 13:25:58

0

Thanks everyone for the tips. I have edited the code as I think should work. I have taken out the if/else statements and replaced with all if statements. However, something is still not right as the output is still incorrect.

Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57

posted Mar 20, 2009 13:38:34

0

You still have way too many ifs and duplicate code, as you handle each possible combination separately.

I would like to make some suggestions to improve the readability of your code.

1) correct your comments. It's confusing to see comments talking about triangles and shapes in a program that has nothing to do with triangles and shapes.

2) Member variables (in fact, ALL variables) should not start with a capital letter. Traditionally, class names do. So when I see something like "Paper" in your code, I think it's a class. All your boolean variables should be declared like this:

private boolean Paper, Plastic, Glass, Cans;

3) in anticipation of your next question, your constructor could then be changed to something along these lines:

4) I would suggest you get rid of your calcGrossWasteEmission() method. It's not really a mutator, since it doesn't let anyone change the value. Also, what happens if someone creates a CO2FromWaste object, but never calls this method? I would but the code for setting the grossWasteEmission value INSIDE the constructor.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors