Results 1 to 16 of 16

Thread: Methods

  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default [SOLVED] Methods

    I am currently writing a code which involves methods. I am currently stuck at step 3.

    1.Write a method called findMin
    2. parameters: three integers
    3. the method returns an integer which is the minimum of the three ints

    My first line of code to complete step one and two is
    public static void findMin(int one, int two, int three)

    Now i dont know what code to accomplish step 3
    Last edited by soccer_kid_6; 02-27-2010 at 07:48 PM.

  2. #2
    Master-N is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    you want to write this:

    public int finMin(int a, int b, int c)
    {
    int min;
    if((a < b) && (a < c)
    {
    a = min;
    }
    else if((b < a) && (b < c))
    {
    b = min;
    }
    else
    {
    c = min;
    }
    return min;
    }
    Last edited by Master-N; 02-27-2010 at 02:59 AM. Reason: don't need a for loop

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Your method definition is wrong, because you just return void means nothing.

    Java Code:
    public static int findMin(int one, int two, int three) {
         // Find the minimum
         return one; // If one is the min
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Master-N View Post
    you want to write this:

    public int finMin(int a, int b, int c)
    {
    int min;
    if((a < b) && (a < c)
    {
    a = min;
    }
    else if((b < a) && (b < c))
    {
    b = min;
    }
    else
    {
    c = min;
    }
    return min;
    }
    Without just giving a code try to explain him what's the mistake he did and so on pal. That's the way to help him in much better way. ;)

  5. #5
    Master-N is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    Eranga is right.

    If you want to create a method that returns an int you have to say so in the method definition. Method definitions work like this:

    accessmodifier returntype name(parameters)

    public, private, protected are the access modifiers.

    int, String, void, etc. (any of the basic data types or objects) are the return types.

    you choose a name for the name

    parameters can be any datatype or object followed by a variable.

  6. #6
    Master-N is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Without just giving a code try to explain him what's the mistake he did and so on pal. That's the way to help him in much better way. ;)
    Yeah, you're right. I'll keep that in mind. :p

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Master-N View Post
    Yeah, you're right. I'll keep that in mind. :p
    That's much better ;)

  9. #9
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    thanks guys. But I am still encountering a problem. When i try to compile it it says "variable min might not have been initialized" But the value was already declared. I attempted declaring the value in different positions but still does not work. Anything I am missing

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

    Default

    Declaring and initializing are not the same thing. Declaring just states that you have a variable. Initializing it is when you assign a value to the variable (or assign it to null).

  11. #11
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Declaring and initializing are not the same thing. Declaring just states that you have a variable. Initializing it is when you assign a value to the variable (or assign it to null).
    still the min is assigned a value either a/b/c, at least from my understanding

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

    Default

    Quote Originally Posted by soccer_kid_6 View Post
    still the min is assigned a value either a/b/c, at least from my understanding
    Where do you assign min a value before you use it? Show me.

  13. #13
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Java Code:
    	public int finMin(int a, int b, int c)
    	{
    		int min; // min declared
    		if((a < b) && (a < c))
    		{
    			a = min;// value assigned
    		}
    		else if((b < a) && (b < c))
    		{
    			b = min;// value assigned
    		}
    		else
    		{
    			c = min;// value assigned
    		}
    	return min;
    }// end finMin method

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

    Default

    [QUOTE=soccer_kid_6;106844]
    Java Code:
    	public int finMin(int a, int b, int c)
    	{
    		int min; // min declared
    		if((a < b) && (a < c))
    		{
    			a = min;// value assigned ????
    		}
    You've got it backwards there. You don't assign a value to min on the line marked ???, but rather you are using min to assign a value to a. It's the left side of the "=" that gets assigned a value. So you are using min before it has been assigned a value. In fact, you never assign min anything anywhere.

    Take my advice and scrap master-n's code. Start over with your own code and you will do much better.

  15. #15
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    Thanks Fubarable, I realized that right before you posted it. Thanks for your help again mate, and the other people who posted in the thread

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 07:37 PM
  3. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  4. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  5. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 06:31 AM

Posting Permissions

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