aspose file tools*
The moose likes Beginning Java and the fly likes Output Issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Output Issues

David Barry
Ranch Hand

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






Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi
You are initializing the values to 0.0. See if your calculation code is being called

Jhakda


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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.
Hunter M.


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

Joined: Jan 13, 2009
Posts: 83
See below to see how I have edited the code per your suggestion. However, nothing has changed on the output... ...this is confusing!



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
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.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

If you look carefully at line 15 in the CO2FootprintV1Tester class you should see what your problem is.

Hint : Instead of looking at the methods you are calling, look at the ones you aren't calling to see if you can see what is happening.


Hunter M.
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi
As Hunter said, find which method is not getting called. Just put a SOP in the calculation methods and you will know which is getting called and which isn't.
Jhakda
Keith R Wegner
Greenhorn

Joined: Mar 17, 2009
Posts: 8
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.


keith.wegner@gmail.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Output Issues