Win a copy of Functional Reactive Programming this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic

Expected output is incorrect!

 
David Barry
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Report post to moderator
Could I replace all of the if/else statements with "while" statements?
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Report post to moderator
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

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Hector Tenedero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Report post to moderator
I would also like to suggest the following:


same pattern should also be applied to other conditional statements
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
    Pie
    Posts: 50196
    79
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Pie
    Posts: 50196
    79
    • Mark post as helpful
    • send pies
    • Report post to moderator
    And where is the bit about triangles?
     
    David Barry
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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
    Posts: 57
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You still have way too many ifs and duplicate code, as you handle each possible combination separately.

     
    David Barry
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Well I have trouble thinking of a better way.
     
    David Barry
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
    • Report post to moderator

    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
    Posts: 12196
    35
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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.
     
      Bookmark Topic Watch Topic
    • New Topic