This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have the following code that calculates the the amount of carbon dioxide emitted for each gallon of gas consumed. There are two different classes: one(the tester class) for the main method, and the other class containing the other various mutator and getter methods. However, when I run the program I get the following output: CO2 Emissions
Gallons Pounds Tons
of Gas from Gas from Gas
1493.0 0.00 0.000
I am wondering why it is producing zeros instead of the results to the calculations. Seems like there is no problem with the code. However, if anybody knows anything about what is going on here, I would appreciate it. Thanks. P.S. Please see code below
Ok so whats happening here is you're outputting the values stored in myTonsCO2 and myPoundsCO2 without actually doing the conversion from gallons to tons and gallons to pounds first. So pretty much you aren't storing anything in the class fields so they are returning zero.
You first need to call your mutator methods before you call your get methods
Hope this helps.
"If the facts don't fit the theory, get new facts" --Albert Einstein
Joined: Jan 13, 2009
See below to see how I have edited the code per your suggestion. However, nothing has changed on the output... ...this is confusing!
I think the problem is that you have a tonsCO2 field and a poundsCO2 field. Why do you need both, when they both represent the same quantity? You can simply multiply tons by however many (2240 here, 2000 in USA) and returning that many.
Look at the actual sequence of code being called. Do this by setting a breakpoint at your constructor, running the debugger and using the Step Over walkthrough to trace each step of your code, which will also show you the values of your variables - you will see that no conversion actually occurs because the conversion methods are never used.