Forums Register Login

Expected output is incorrect!

+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Remember that if the boolean expression in an if-clause is true, then the code in the corresponding else-clause will not be executed.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Could I replace all of the if/else statements with "while" statements?
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
David,

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

John.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I would also like to suggest the following:


same pattern should also be applied to other conditional statements
+Pie Number of slices to send: Send
 

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




    +Pie Number of slices to send: Send
    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 ==.
    +Pie Number of slices to send: Send
    And where is the bit about triangles?
    +Pie Number of slices to send: Send
    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.



    +Pie Number of slices to send: Send
    You still have way too many ifs and duplicate code, as you handle each possible combination separately.

    +Pie Number of slices to send: Send
    Well I have trouble thinking of a better way.
    +Pie Number of slices to send: Send
     


    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
    +Pie Number of slices to send: Send
    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.
    Something about .... going for a swim. With this tiny ad ...
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    Reply locked
    This thread has been viewed 1942 times.
    Similar Threads
    using array list
    calculation methods
    array of objects confusion
    using sin in java
    Output Issues
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 08:36:45.