Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Difficulty with method headers.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difficulty with method headers.

    Write only the method headers. Overload a method name, if appropriate.
    a. Calculate the largest of 2, 3, or 4 integer values.

    int val(int a, int b, int c ,int d)

    if (a > b && b > c && b > d)
    {return a + b;}

    else if (a > c && c > b && b > d)
    {return a + c;}


    Is it asking for something similar to this? What do they mean by calculate the largest?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Difficulty with method headers.

    what you have would be appropriate for 4 value inputs, but sometimes you'll only have 2 or 3, according to the problem. Overload the method name by making the same method again, but only giving it values a & b, then again with values a, b & c. Obviously, rewrite the body to reflect the number of inputs. Then, the correct method will be called depending on how many values are passed in.

    correct your body for the 4 inputs first, right now it's not going to work. something like:

    public int vals(int a, int b, int c, int d){
    int highest = a;
    if(b>highest)
    highest = b;
    if(c>highest)
    highest = c;
    if(d>highest)
    highest = d;
    return highest;
    }
    Last edited by Herah; October 29th, 2011 at 06:28 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with method headers.

    public class ttt
    {
    public int vals(int a, int b, int c, int d)
    {
    a=3; b=2; c=10; d=4;
    int highest = a;
    if(b>highest)
    highest = b; System.out.println(highest);
    if(c>highest)
    highest = c; System.out.println(highest);
    if(d>highest)
    highest = d;
    System.out.println(highest);
    return highest;
    }
    }

    There are no errors but nothing shows up. Can you tell me why?

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Difficulty with method headers.

    What you have listed above is a fragment, not a program, so it wont run. If you want to test it you could do something like this:
     
    public class ttt {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    	vals(3, 2, 10, 4);  // calls vals method using 4 arguments	
    	}
    	public static int vals(int a, int b, int c, int d){
     
    	int highest = a;
    	if(b>highest)
    		highest = b; System.out.println(highest);
    	if(c>highest)
    		highest = c; System.out.println(highest);
    	if(d>highest)
    		highest = d; System.out.println(highest);
    	return highest;
    	}
    }

    your print statements are just giving you an idea of what's happening to your highest value, so are good for debugging. You don't need them for the actual program, of couse.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with method headers.

    how does vals(3, 2, 10, 4); and my int numbers different?

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Difficulty with method headers.

    They're essentially going to end up printing the same thing, but what you're doing by putting int numbers into your method is overriding the ints that are passed in, making them meaningless. However, it still would have worked. What you were missing to get the program to run was a main method. The main method calls your other method and should pass the values in when it's called.

Similar Threads

  1. [SOLVED] Difficulty with string passing...
    By Destined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 06:43 AM
  2. How to remove all Headers from a MimeMessage
    By ArgyrisV in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2011, 12:38 PM
  3. Set headers for HttpServletRequest object?
    By FailMouse in forum Web Frameworks
    Replies: 3
    Last Post: December 1st, 2010, 03:00 PM
  4. Javamail html mail sended as text and headers problem
    By johnymj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 29th, 2010, 10:22 AM
  5. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM