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:
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
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?
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.
posted 4 years ago
You may prefer to get the three numbers in the constructor in which case the getNumbers method may be unnecessary.
posted 4 years ago
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?"!
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:
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
posted 4 years ago
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!