Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

Output Issues

 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi
You are initializing the values to 0.0. See if your calculation code is being called

Jhakda
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic