Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Parameters confusion  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure this is like having my Spanish students ask me how to say "yes" in Spanish, but I'm stuck.

I have to write a program with a method that has 3 parameters. The problem is to put in order 3 numbers that the user supplies. The problem is that if I scan the input & declare it in the method, it's not recognized when I call it in the main method. Let me illustrate a bit:

main method

      orderNumbers(num1, num2, num3);

public static void orderNumbers(int num1, int num2, int num3)

      Ask user for 3 integers; name them num1, num2, num3;
      do the operations
      System.out.print(result);

OK, the problem is that the main method doesn't recognize the variables that are in the parameters, apparently because they're locked in the orderNumbers method. But if I define them in the main method, then the orderNumbers method doesn't recognize them. I can solve the problem by eliminating the nums as parameters, & the program runs fine, but the problem is that my class directions specify using those variables as parameters. What can I do?
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:What can I do?
At the moment post the actual code you got so far.

Important: please do not forget to UseCodeTags (<- link to check how to use them).
And welcome to Ranch
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here we go:



Wow--since we aren't allowed to use an IDE on our mid-term, I wish I could stick in a couple of codes on it to get this format!
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Unless you have been told to use that package name, delete it. Package names are awkward when you are just beginning.
You have three numbers in the main method, line 6, but it is not obvious where they come from, so the compiler will get all confused and refuse to compile that code.
You have methods far too long. You want a method which gets the numbers, and a method which does the sorting and a method which does the displaying. You probably want a class with fields for the three numbers, or a method which puts all those things together. Or both.Please explain what the complicated‑looking predicates in lines 27 and 29 mean.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may prefer to get the three numbers in the constructor in which case the getNumbers method may be unnecessary.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It's going to take me a while to sort through your answer, because I've been taking Java for exactly 4 weeks & two days, & although I'd studied it on my own a bit before beginning the class & am pretty sure I can eventually figure out what you're talking about, it's going to take me some time!

I'm a linguist, & to me "predicate" means the verb & what follows it, & there's nothing on one of the lines you mention, but I'm going to assume you mean the two longest lines. Those are boolean expressions we learned. && means that both statements are true, ^ means one or the other but not both, etc. Your question tells me that there's a much simpler way, but apparently, they introduce us to things & then show us an easier way to do it.

I warned you I was a fetus! But if you guys have any questions about Spanish, I have a Ph.D. in that & can answer questions harder than "how do you say 'yes' in Spanish?"!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run the program, it starts with the main method:



What you are saying here (on line 2) is:

Take the value of num1, the value of num2, and the value of num3 and pass that into the displaySortedNumbers method.

The problem is...you don't HAVE a num1, num2, or num3. They don't exist. Therefore, it doesn't make sense to talk about what value they hold or to pass that in to another method.

Your displaySortedNumbers method declares the variables and initializes them to some value.

So my question is...why are you passing them from main to displaySortedNumbers? I don't think you need them at all. make your main have this:

displaySortedNumbers();

and make your method this:

public static void displaySortedNumbers()

Now, there is a lengthy discussion we could have about whether this method should be static or not (hint: it shouldn't), but there are other changes you'd have to make to fix that. We can save that for another time.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. My program runs fine that way. But that's not the assignment!

I actually figured out what my problem was, & it was simple: I have to give the parameters in the method (not the call) types.

However, you have put me in search of a way to do it via the methods you described, because my program IS pretty messy. I'm finding this course harder than the one I did at caveofprogramming.com, maybe because I got walked through everything step by step. I'm trying to learn to do things the RIGHT way, & not just in a way that works, although I still haven't figured out why it was better for me to create my website with Dreamweaver using html & CSS rather than a real WYSIWYG editor like Microsoft Expressions, where I can just paste things & grab them & move them where I want. But I know it's better to listen to people who know a lot more than I do. I'm hoping that one day we'll elect a president who's figured that out!

And thank you!!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so REALLY...you should have a lot more methods. You should strive to have a method do one thing, and one thing only. So a method to get input. A method to sort. a method to print. etc.

So, I might create a method that takes a prompt string as a parameter, and gets an integer input. so something like this:



Once you have that, you can call it multiple times from your main (again, not ideal, but for now, we'll go with it)


Then, once you KNOW your getIntWithPrompt method works, you can use it over and over and not worry about it any more.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!