Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Unhappy 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by HaplessNoob View Post
    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.
    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).

  3. #3
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Similar problem has already been discused here:

    Help with modularity? Declaring methods and return values

  4. #4
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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).
    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.
    .

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by HaplessNoob View Post
    Sound right, at least as far as the assignments description of method1 goes. Just not sure i understand what it means by that.

    What specifically confuses you?

  6. #6
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Just coming to grasps with using these basic method principals in general. Seems my assumotion for method1 was way off given your reply:

    Quote Originally Posted by Fubarable View Post
    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.
    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. :(

    //editted
    Last edited by HaplessNoob; 02-06-2011 at 01:58 PM.

  7. #7
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    should add that as i'm a part time java student, we're pretty much left to teach ourselves - hense my outstanding ignorance :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by HaplessNoob View Post
    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. :(

    //editted
    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;
       }
    
    }

  9. #9
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    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.

  10. #10
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    ///editted
    Last edited by HaplessNoob; 02-06-2011 at 02:55 PM.

  11. #11
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    You can return an array of ints. And again, similar situation has been already discused...

  12. #12
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Ah, ok. So arrays are essential to solving this afterall.
    Last edited by HaplessNoob; 02-06-2011 at 03:05 PM.

  13. #13
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Easiest by my opinion...

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by milovan View Post
    You can return an array of ints. And again, similar situation has been already discused...
    No you can't as that's not what your instructions tell you to do, and regardless of what myself or milovan suggest, you must follow your instructions.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by HaplessNoob View Post
    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.
    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:

    Java Code:
    double width = method1("Please enter a width");
    double height = method1("Please enter a height");
    double age = method1("Please enter an age");
    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.

  16. #16
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    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:

  17. #17
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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:

    Java Code:
    double width = method1("Please enter a width");
    double height = method1("Please enter a height");
    double age = method1("Please enter an age");
    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.
    Ah i only just caught this. thanks, I'll read over this now.

  18. #18
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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:

    Java Code:
    double width = method1("Please enter a width");
    double height = method1("Please enter a height");
    double age = method1("Please enter an age");
    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.
    I replaced the code with that in your first example but it doesn't compile?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by HaplessNoob View Post
    I replaced the code with that in your first example but it doesn't compile?
    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.

  20. #20
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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:

    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;
       }
    
    
    }
    The first double in main is flagged with, "method1() in Foo3 cannot be applied to (java.lang.String)".
    Last edited by Fubarable; 02-06-2011 at 06:01 PM. Reason: Moderator edit: quote tags changed to code tags

Page 1 of 2 12 LastLast

Similar Threads

  1. need for some advice
    By kasiopi in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2011, 12:36 PM
  2. Im new n looking for an advice
    By azlynn in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:47 AM
  3. [SOLVED] need advice on this method.
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 06:22 AM
  4. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 07:23 PM
  5. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •