Results 1 to 9 of 9
  1. #1
    kongahh11 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Question how to read this program??

    just some clarification needed. how does this program read?

    /////////////////////////////////////////////////////////////////

    import java.util.*;

    class Testing
    {
    public static void main(String[] args){
    int x = 1, y = 2, z;

    z = Calculate (x, y);
    System.out.println(Calculate(z, x+y));
    }
    public static int Calculate(int a, int b){
    if (a>b)
    { Jump(b);
    return a*b;
    }
    else
    { Walk(a);
    return a-b;
    }
    }

    //end of calculate

    public static void Jump (int b){
    System.out.println(b*b);

    }
    public static void Walk (int b){
    System.out.println(b*b*b);
    }
    }

    //end of Testing



    i get lost when Calculate comes in for z. what kind of a command is calculate? (x, y) means add, right? and the alternate methods within the program. i got confused when it said Walk (a), then the next method transferred changed to Walk (int b).

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kongahh11 View Post
    just some clarification needed. how does this program read?
    It reads quite lousy because you should've put your code between [code] ... [/code] tags.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bigka79 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    walk and calculate look to me like they are methods from a different class. The way im looking at it you do not have all of the code. Based on the name of the class you show here "class testing" i think walk and calculate are defined elsewhere.

  4. #4
    kongahh11 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    calculate and walk are defined in their own methods under the main method. i just don't know how the computer would read it in order. sorry, didn't mean to say calculate was a command. what i mean to say is that what does the computer do after:

    "z = Calculate (x, y);
    System.out.println(Calculate(z, x+y));
    }
    public static int Calculate(int a, int b){
    if (a>b)
    { Jump(b);
    return a*b;
    }
    else
    { Walk(a);
    return a-b;
    }
    }

    //end of calculate

    public static void Jump (int b){
    System.out.println(b*b);

    }
    public static void Walk (int b){
    System.out.println(b*b*b);
    }
    }

    ////////////////////////

  5. #5
    kongahh11 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
     
    
    import java.util.*;
    
    class Testing
    {
    public static void main(String[] args){
    int x = 1, y = 2, z;
    
    z = Calculate (x, y);
    System.out.println(Calculate(z, x+y));
    }
    public static int Calculate(int a, int b){
    if (a>b)
    {	Jump(b);
    return a*b;
    }
    else
    {	Walk(a);
    return a-b;
    }
    }
    
    //end of calculate
    
    public static void Jump (int b){
    System.out.println(b*b);
    
    }
    public static void Walk (int b){
    System.out.println(b*b*b);
    }
    }

  6. #6
    kongahh11 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    ok, got it!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    import java.util.*;
    
    class Testing
    {
    public static void main(String[] args){
    int x = 1, y = 2, z;
    
    z = Calculate (x, y);
    System.out.println(Calculate(z, x+y));
    }
    public static int Calculate(int a, int b){
    if (a>b)
    { Jump(b);
    return a*b;
    }
    else
    { Walk(a);
    return a-b;
    }
    }
    
    //end of calculate
    
    public static void Jump (int b){
    System.out.println(b*b);
    
    }
    public static void Walk (int b){
    System.out.println(b*b*b);
    }
    }
    First it declares the items, x, y, and z, initializing x and y.
    The next step will set z to the return value of calculate(x, y);

    calculate says that if the first input is greater then the second input is calls the method jump, and returns a*b.

    Since 1 is less than 2 it moves onto the else clause which calls the method walk on a and returns a - b.

    Walk will print y*y*y to the screen which will produce 1, and set z to -1(1 - 2)

    finally it will print out the return value of calculate with z, and x + y
    Java Code:
    print calculate(-1, 3);
    it will call walk on -1(a), then print out a - b

    output should look something like
    Java Code:
    1
    -1
    -4

  8. #8
    kongahh11 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    hey sunde. thanks for the help. so:

    Java Code:
     
    z = Calculate (x, y);
    doesn't mean add the value of those two. and

    Java Code:
    System.out.println(Calculate(z, x+y));
    why is there a z inside of the expression? and x+y? doesn't that mean add x and y, then set them as int b?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    the definition of calculate is
    Java Code:
    public static int Calculate(int a, int b){
    if (a>b)
    { Jump(b);
    return a*b;
    }
    else
    { Walk(a);
    return a-b;
    }
    }
    the code
    Java Code:
     
    z = Calculate(x, y);
    means, whatever Calculate(x, y) returns is the new value of z.
    This works because Calculate returns an int, so the return can be assigned to an int variable.

    Java Code:
    Calculate(z, x + y);
    means, return whatever calculate should return for the parameters z, and x + y.

    If you go through and replace all occurrences of a with z, and all occurrences of b with x + y you will get this:
    Java Code:
    public static int Calculate(int z, int (x + y)){
    if (z>(x + y))
    { Jump((x + y));
    return z*(x + y);
    }
    else
    { Walk(z);
    return z-(x + y);
    }
    }
    I hope this clarifies it for you.

Similar Threads

  1. Replies: 5
    Last Post: 11-17-2010, 06:53 PM
  2. how to read excel file through java program
    By madhuragowda in forum New To Java
    Replies: 2
    Last Post: 04-16-2010, 09:53 AM
  3. Replies: 2
    Last Post: 04-03-2009, 05:37 AM
  4. How to read a Web Page through java program
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-04-2008, 03:37 PM
  5. How to read a Web Page through java program
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:31 PM

Tags for this Thread

Posting Permissions

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