This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes method construction trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method construction trouble" Watch "method construction trouble" New topic
Author

method construction trouble

jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
Ok, I am coding a simple java program. it asks a user for 3 inputs, then, in a method called MainMin it should take the min of the three numbers and return it. after I return it I need to do a simple system.out.print that says the output is the minimum of three numbers. I think I have everything right but I cant figure out how to get the last system.out.print in there. i keep getting errors.PLEASE HELP!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11249
    
  16

You realize you never run your MainMin method?

In a java program, you start in the main() method. So, we'd start on your line 8, make the Scanner, create some variables...

Then we prompt the user and get some inputs...then when we hit the closing bracket on line 19, your program ends. you need to call the MainMin method in your main method, and save what it returns in a new variable, then print it out from there. Your current line 50 is not in any method, and is therefore not valid.

Further...I'm not sure why your MainMin takes four parameters. You only need to pass it three, and then you return the min value as your output on line 44. You will have to declare output as a local variable in your method, but that's trivial...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
SO how do I call the method in main? once I call it, I then store the return as a double, then print the final line?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
jay hill wrote:SO how do I call the method in main?

You already know how to call a method. On lines 13 - 18 you call the the println and nextInt methods. So just try doing something similar with your MainMin method.

jay hill wrote:once I call it, I then store the return as a double, then print the final line?

Correct. As Fred said, you'll need to move your print statement inside the main method.


Joanne
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
Thank you for all the help, I appreciate it. One more thing. What is my instance when I call the method?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
MainMin is a bad name for a method. By convention, method names start with a lower-case letter. Upper-case is legal, but observing conventions makes your code more legible to others. How about something like findLowestValue?

There is no need for MainMin to take the "output" double as a parameter. In general, you should never re-assign the value of a method argument.

There is a much simpler way to determine the lowest value:
If you have studied loops and arrays, you can make this even simpler.
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
I am doing a practice exercise for an in- class exam I have this week. The book said mainMin so I just used that. I took your advise on the of structure. I just can't figure out how to call it in he min method...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

jay hill wrote:Thank you for all the help, I appreciate it. One more thing. What is my instance when I call the method?

It's a static method, so you don't need an instance:

If you were outside the class, you'd qualify it by using the class name:
but there's no need for that when you're still in the same class.
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
I apologize, I'm trying to figure this out. Would it be something like

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

Almost, but you need to pass in the arguments. Arguments are comma-separated.

(Note that the method name is case-sensitive. mainMin is better than MainMin because it follows the Java convention, but make sure that's what the method is actually called).
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
What would my arguments be? My inputs?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11249
    
  16

I should probably clarify my comment above...

AS WRITTEN, your MainMin method requires you to pass it four parameters. But you really shouldn't write it that way. You don't need to pass IN the outPut variable. I would delete it from line 21.

Then, inside your MainMin method, you need to declare it...before your first if-statement, you would need a line like

double output = 0;
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11249
    
  16

jay hill wrote:What would my arguments be? My inputs?

Think about what your method is doing...you pass it three values, and it returns the minimum. So, pass it whatever three values/variables you want to find the minimum of. In this case, your inputs is indeed what you want to pass it.

Note also that when you pass them in, you don't need to state what the type is...you just list them.
jay hill
Greenhorn

Joined: Dec 05, 2011
Posts: 7
Got it! Thank you so much everyone! Have a good day!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method construction trouble