aspose file tools*
The moose likes Beginning Java and the fly likes Expected output is incorrect! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Expected output is incorrect!

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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



[edit]New lines to keep text within width of screen. CR[/edit]
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
For instance for the first line, I should get the following output:


People Total Emissions Reduction Net Emission
1 1018 422 596

...and I am getting this:

People Total Emissions Reduction Net Emission
1 1018 184 834
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Remember that if the boolean expression in an if-clause is true, then the code in the corresponding else-clause will not be executed.
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
That's right. Then how am I going to do this; if I can't use if/else statements. I still need to be able to make these choices
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Could I replace all of the if/else statements with "while" statements?
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
Paper is true, so


is executed and not one of the else branches.

Edit ....

seems, i answered too slow.

You might go for something like


SCEA5, Certified ScrumMaster
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
David,

Think about the order in which you're checking Paper, Plastic, etc. It seems to me that your problem lies in there.

John.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Ok so what's happening here, just like Paul said, is that your not getting to the else statements because the original if statement is true so the else statements aren't evaluated.

So just look at this example and see if it makes sense:

This is how you have it set up.

Output = 5

But what if we just tested for conditions in reverse order

Output = 10;

Hope this helps.
Hunter.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Hector Tenedero
Ranch Hand

Joined: Oct 13, 2008
Posts: 31
I would also like to suggest the following:


same pattern should also be applied to other conditional statements
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
Hector Tenedero wrote:I would also like to suggest the following:


same pattern should also be applied to other conditional statements


I don't think, that i like this approach.

Worst case you would have to check for every possible combination. In this case
Paper, Plastic, Glass, Cans
  • Paper and Plastic
  • Paper and Glass
  • Paper and Cans
  • Plastic and Glass

  • ...
  • Paper, Plastic and Glass


  • etc.

    Regards,
    Uli




    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    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: 40052
        
      28
    And where is the bit about triangles?
    David Barry
    Ranch Hand

    Joined: Jan 13, 2009
    Posts: 83
    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
    You still have way too many ifs and duplicate code, as you handle each possible combination separately.

    David Barry
    Ranch Hand

    Joined: Jan 13, 2009
    Posts: 83
    Well I have trouble thinking of a better way.
    David Barry
    Ranch Hand

    Joined: Jan 13, 2009
    Posts: 83

    You might go for something like
    view plaincopy to clipboardprint?

    1.
    2. public void calcWasteReduction() {
    3. wasteReduction = 0;
    4. if (Paper) {
    5. wasteReduction += NumPeople * 184;
    6. }
    7.
    8. if (Plastic) {
    9. wasteReduction += (NumPeople * 25.6);
    10. }
    11.
    12. if (Glass) {
    13. wasteReduction += (NumPeople * 46.6);
    14. }
    15.
    16. if (Cans == true) {
    17. wasteReduction +=(NumPeople * 165.8);
    18. }
    19.
    20. }


    With some editing to this suggestion, I believe I have a solution that works. Thanks, Uli
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11498
        
      16

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Expected output is incorrect!