• Post Reply Bookmark Topic Watch Topic
  • New Topic

back for another question on methods, variables  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so basically what i'm trying to do is add TWO methods under the calculateMPG method which both need the result of calculateMPG. i.e. if calculateMPG < 15 your car is a gas hog, else your car is an economy car. i tried storing calculateMPG's result into a variable, and writing the two new methods that way, but being either inside OR outside of the calculateMPG method, the program can't seem to find it to reference for the two new methods. here is what i'm working with:



again, i tried creating a reference variable for the result of calculateMPG but everywhere i put it when MilesPerGallon ran, it could not find that variable. i have a feeling this is something simple that i'm overlooking, but i'm hitting a wall and would love any input! and thanks in advance!
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for instance i tried doing this:



and obviously it didn't work. ugh...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the intent is to determine if the car is a gas hog based upon the mpg calculation, retiring a double from the determination method doesn't make much sense, does it. I mean, the answer to the question "is my car a gas hog" is either yes or no, not a numeric value.

So would not the following method make more sense?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods are all instance methods of the same object; you don't need the reference to car to reference the other methods.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your comment is fairly correct:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I noticed you've fallen into the habit of not assigning access modifiers (public, private) to your declarations. Any reason why?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

If I were you I'd write one method to determine if the car is a gas hog or economy. Write an If Else statement in the method and return a boolean or some other type that you can output in the main method. Or have a println() right in the new method.



Hope this helps.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:P.S. I noticed you've fallen into the habit of not assigning access modifiers (public, private) to your declarations. Any reason why?


thank everybody for the responses! to the response regarding boolean versus double...welp...i feel silly on that one. regarding not using public private...it's not something our teacher has gotten us in the habit of doing. we've only had 8-10 class days so far so maybe she'll get into that). let me try something now...
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Winterbourne wrote:Hi David,

If I were you I'd write one method to determine if the car is a gas hog or economy. Write an If Else statement in the method and return a boolean or some other type that you can output in the main method. Or have a println() right in the new method.



Hope this helps.


thanks for the response and trust me i came to that conclusion too, that it would be easier with just one. unfortunately we're asked to create two separate ones. it doesn't seem that daunting really, but me, being SO new to this, i have a feeling it's just something in the syntax that i'm missing.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and PS this is one thing i couldn't get...what to refer to calculateMPG as .

so now i'm trying this:



and getting those errors... i tried putting "return" in front of each line then the error changed to illegal way to start a statement or something...
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only need to output the text if it's a gas hog or economy car then you can change the return type of each method to "void". then you don't have to supply a return statement.

If you do need to return a boolean then you will need to declare a boolean variable inside the methods, set it to true or false, and then return that.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Winterbourne wrote:If you only need to output the text if it's a gas hog or economy car then you can change the return type of each method to "void". then you don't have to supply a return statement.

If you do need to return a boolean then you will need to declare a boolean variable inside the methods, set it to true or false, and then return that.


so this makes complete sense to me, but i'm having trouble figuring out where to put "void". so if i put void in front of boolean like:



then hooray it actually compiles!!...but wait then it doesn't output anything :/

EDIT: i shouldn't say it doesn't output anything. it just outputs the miles per gallon as normal without the accompanying text...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void or boolean

It either returns nothing, or returns a boolean, it can't do both.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put void in place of the boolean return type

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again for the replies guys, hopefully one day i'll be helping out other little greenhorns just as much.

so i tried that already too, changing to:



and it does compile, but then again, all it does is print the miles per gallon without the accompanying text. again, i feel like i'm looking over something so minute...
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to call these two methods from your main() method.

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Winterbourne wrote:You'll need to call these two methods from your main() method.



oh little baby jesus. hindsight is 20/15. finally getting something to work and then saying david...you are a moron. bad david! don't do that again!

seriously though i feel stupid. i was so caught up in making the syntax of the car class correct that i completely forgot the class with the main() is where that needed to happen.

for everyone that replied to me, thank you thank you thank you! i cheers my glass to yours
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are very welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!