Results 1 to 20 of 24
Thread: Need some method advice please
- 02-06-2011, 12:45 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Need some basic method advice please
Hi all,
This is my first (and hopefully last) question I’ve asked here.
Should also add that I’m not looking for anyone to do my assignment for me, I’m actually only looking for advice on what type of method/array i should be investigating in one particular part of the code - so no ‘rtfm’ comments if that’s ok because I’ve spent the last 2 days trying that
Ok so here is it. I’ve been asked to take an old sequential program that I previously made, and break it up into 2 methods and 1 main.
1.) The first method should prompt and read 3 values, e.g:
“Enter size of blah”;
“Enter size of blah2”;
“Enter size of blah3”;
(It will also use a while loop to check for invalid data, but that’s not important right now).
2.) The second method calls the values entered in the first method and applies some maths to them.
3.) The main then (presumably) calls the results and prints them to screen in the same fashion that it previously did.
Now, i know how to make basic void methods and call them and i know how to make basic returning methods – but they were only doing simple addition. And it seems that every methods tutorial i’ve read only deals with single values being called and not 3.
This is where i’m stuck: assuming method one needs to return 3 values to the second method – then what kind of method should i be using? I’ve tried ‘return blah1;’ but I assume a returning method can only return one value and not three? Should they be returning methods or am I missing the point completely and should be using an array.
Assuming any of that made sense, any advice would be welcome :)
Thanks for your time,
Hapless,Last edited by HaplessNoob; 02-06-2011 at 12:54 PM.
-
Method 1 (or better method1) should in fact return only one value. The key here is that the main method will likely call method1 three times. My guess is that method1 will take a String parameter, the prompt, and then return the value entered by the user (after checking for correctness).
- 02-06-2011, 01:01 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Similar problem has already been discused here:
Help with modularity? Declaring methods and return values
- 02-06-2011, 01:43 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Sound right, at least as far as the assignments description of method1 goes. Just not sure i understand what it means by that.
.the goal this time is to code the following methods and to use them in you ‘main’ method:
method 1: the parameter passed into the method is the prompt text (a String) and the value returned is the value read (a double).
method 2: 3 parameters (all doubles) are passed into the method and the calculated result (again a double) is returned.
-
- 02-06-2011, 01:56 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Just coming to grasps with using these basic method principals in general. Seems my assumotion for method1 was way off given your reply:
would you know of any examples of this that I could mull over? ideally something similar so that i can put it in context. the above array example seems thorough but sadly out of my depth at the moment since we haven't touched arrays just yet. :(
//edittedLast edited by HaplessNoob; 02-06-2011 at 01:58 PM.
- 02-06-2011, 02:00 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
should add that as i'm a part time java student, we're pretty much left to teach ourselves - hense my outstanding ignorance :)
-
Here's an example where you have a method, getNextCount() that returns an int. Since I need three ints for the variables a, b, and c, I call getNextCount() three times:
Java Code:public class Foo1 { private static int count = 0; public static void main(String[] args) { int a = getNextCount(); int b = getNextCount(); int c = getNextCount(); System.out.println("results are"); System.out.println("a: " + a); System.out.println("b: " + b); System.out.println("c: " + c); } private static int getNextCount() { count++; return count; } }
- 02-06-2011, 02:43 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Thanks for taking the time to post that.
I understand those types of return methods fine, i'm just unable to understand how to apply them to the first method in my original post where there are 3 variables that need to be returned, not just one.
e.g. Say that method1 prompts the user to input 3 different doubles; height, width and depth (it doesn't, but just as an example).
method2 then needs to take each of those and multiply them together and output the result.
- 02-06-2011, 02:50 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
///editted
Last edited by HaplessNoob; 02-06-2011 at 02:55 PM.
- 02-06-2011, 02:54 PM #11
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
You can return an array of ints. And again, similar situation has been already discused...
- 02-06-2011, 03:03 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Ah, ok. So arrays are essential to solving this afterall.
Last edited by HaplessNoob; 02-06-2011 at 03:05 PM.
- 02-06-2011, 03:16 PM #13
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Easiest by my opinion...
-
-
Again, you call method1 THREE times, just as I'm calling a method above three times, and each time you call it you will retrieve the double returned and place it into a variable:
It's as simple as that. Again, do NOT use arrays here as there is simply no need and it will actually make things more difficult not easier.Java Code:double width = method1("Please enter a width"); double height = method1("Please enter a height"); double age = method1("Please enter an age");
- 02-06-2011, 03:42 PM #16
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Ok thanks, i'll try to stick to what it says then.
So far, i've managed to get it to work somewhat.
One question though, the instructions are a little vauge to me: what does it mean by saying "the parameter passed into the first method is the prompt text (a String)"? So far my first method doesn't have anything being passed to it, its just prompting for 3 values and sending each of those to the second method for calculation :confused:
- 02-06-2011, 03:43 PM #17
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
- 02-06-2011, 05:26 PM #18
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
-
This is why you shouldn't blindly copy and paste code from the forum into your program. The examples posted in the forum aren't meant to be copied and pasted into your program but are to give you an idea of the code you are supposed to write. So get rid of the pasted code, and try to write your own that compiles. If it doesn't, then post your code and the error messages.
- 02-06-2011, 05:55 PM #20
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Sorry, i didn't mean to give the impression that i dumped your code into my program because i didn't. My actual assignment is actually nothing to do with dimensions :)
What i did was merge your two examples together in an effort to see your code working is all.
Like this:
The first double in main is flagged with, "method1() in Foo3 cannot be applied to (java.lang.String)".Java Code:public class Foo3 { private static double count = 0; public static void main(String[] args) { double width = method1("Please enter a width"); double height = method1("Please enter a height"); double age = method1("Please enter an age"); System.out.println("results are"); System.out.println("a: " + width); System.out.println("b: " + height); System.out.println("c: " + depth); } private static double method1() { count++; return count; } }Last edited by Fubarable; 02-06-2011 at 06:01 PM. Reason: Moderator edit: quote tags changed to code tags
Similar Threads
-
need for some advice
By kasiopi in forum AWT / SwingReplies: 3Last Post: 01-26-2011, 12:36 PM -
Im new n looking for an advice
By azlynn in forum New To JavaReplies: 2Last Post: 12-10-2009, 02:47 AM -
[SOLVED] need advice on this method.
By PureAwesomeness in forum New To JavaReplies: 1Last Post: 02-24-2009, 06:22 AM -
Some advice please!
By awebbtt in forum New To JavaReplies: 3Last Post: 02-02-2009, 07:23 PM -
Advice on best method for....
By shaungoater in forum Java 2DReplies: 1Last Post: 06-23-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks