Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method construction trouble

 
jay hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jay hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jay hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize, I'm trying to figure this out. Would it be something like

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would my arguments be? My inputs?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Thank you so much everyone! Have a good day!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic