Results 1 to 6 of 6
  1. #1
    aussieguy0101 is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Output of a basic Java program

    Hi
    I have been trying to work out (without imputing into eclipse) the output of this program.
    What I dont understand is A b and x have values and c doesnt. So how is it possible to workout the total output. On the school forums the answer is 66 but I have no idea how this number is worked out

    Java Code:
    public class Q4
    {
    	public static int a(int b, int c)
    	{
    		int x=0;
    		x=c-b;
    		return x;
    	}
    	public static int b(int c, int d)
    	{
    		int x;
    		if(c<d)
    		{
    			x=a(c,d);
    		}
    		else
    		{
    			if(d<c)
    			{
    				x=a(d,c);
    			}
    			else
    			{
    				x=0;
    			}
    		}
    		return x;
    	}
    	
    	public static void main(String args[])
    	{
    		int a=3;
    		int b=-3;
    		int x=10;
    		x=b(a,b);
    		System.out.print(x);
    		x=b(b,a);
    		System.out.print(x);
    		
    		
    			}
    	}
    Last edited by JosAH; 02-16-2013 at 10:58 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: Output of a basic Java program

    Take a piece of paper and a pencil and pretend you're the computer; execute the program step by step (and write down your steps). When you're finished you can see what you have 'printed'.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,248
    Rep Power
    9

    Default Re: Output of a basic Java program

    And also remember the difference between method arguments and method parameters and how they are used.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: Output of a basic Java program

    Quote Originally Posted by jim829 View Post
    And also remember the difference between method arguments and method parameters and how they are used.
    What is a method argument if it isn't a method parameter?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,248
    Rep Power
    9

    Default Re: Output of a basic Java program

    My understanding is that a method parameter is part of the method signature and a method argument is what is passed in the method invocation. So I had thought the poster did not understand the mapping from one to the other (hence not seeing where the variable 'c' was defined).

    Regards,
    Jim

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: Output of a basic Java program

    Quote Originally Posted by jim829 View Post
    My understanding is that a method parameter is part of the method signature and a method argument is what is passed in the method invocation. So I had thought the poster did not understand the mapping from one to the other (hence not seeing where the variable 'c' was defined).
    Ah, yes; it's just a naming misunderstanding; I know them by the name 'formal parameters' (as defined in the method declaration) and 'actual parameters' (as the method is called from somewhere).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2012, 10:10 PM
  2. Simple First Java program: Basic Contact register
    By ZinoXxX in forum New To Java
    Replies: 8
    Last Post: 03-17-2012, 04:13 PM
  3. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 05:12 AM
  4. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 08:25 AM
  5. Replies: 3
    Last Post: 09-22-2009, 05:16 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
  •