Results 1 to 3 of 3
  1. #1
    annna is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default Need help with my tutorial

    Can someone help me figure this out?

    Firstly, this is the whole code,

    public class Foo
    {
    int x;
    int y;

    void set(int x,int y)
    {
    this.x=x;
    this.y=y;
    }

    void swap1(int x,int y)
    {
    int t;
    t=x;
    x=y;
    y=t;
    }

    void swap2()
    {
    int t;
    t=this.x;
    this.x=this.y;
    this.y=t;
    }

    void print()
    {
    System.out.println(this.x);
    System.out.println(this.y);
    }
    }

    public class Bar
    {
    public static void main(String[] args)
    {
    int x=10, y=20;
    Foo foo = new Foo();

    foo.set(1,2);

    System.out.println("In the beginning...");
    foo.print();
    System.out.println(x);
    System.out.println(y);

    System.out.println("Invoking swap1 method");
    foo.swap1(x,y);
    foo.print();
    System.out.println(x);
    System.out.println(y);

    System.out.println("Invoking swap1 method again...");
    foo.swap1(foo.x,foo.y);
    foo.print();

    System.out.println("invoking swap2 method...");
    foo.swap2();
    foo.print();
    }
    }

    I am supposed to figure out the output of the program. The part I don't understand is the swap1 method.
    Why isn't x and y swapped to become 20, 10 respectively.
    Why does the output remain the same after swap1?
    And why is foo.swap2(); ()<-empty?

    Thanks!

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Java Code:
    public class Foo
    {
    int x;
    int y;
    
    void set(int x,int y)
    {
    this.x=x;
    this.y=y;
    }
    
    void swap1(int x,int y)
    {
    int t;
    t=x;
    x=y;
    y=t;
    }
    
    void swap2()
    {
    int t;
    t=this.x;
    this.x=this.y;
    this.y=t;
    }
    
    void print()
    {
    System.out.println(this.x);
    System.out.println(this.y);
    }
    }
    
    public class Bar
    {
    public static void main(String[] args)
    {
    int x=10, y=20;
    Foo foo = new Foo();
    
    foo.set(1,2);
    
    System.out.println("In the beginning...");
    foo.print();
    System.out.println(x);
    System.out.println(y);
    
    System.out.println("Invoking swap1 method");
    foo.swap1(x,y);
    foo.print();
    System.out.println(x);
    System.out.println(y);
    
    System.out.println("Invoking swap1 method again...");
    foo.swap1(foo.x,foo.y);
    foo.print();
    
    System.out.println("invoking swap2 method...");
    foo.swap2(); 
    foo.print();
    }
    }
    Scope of a method/object is one of the things the code is teaching.

    swap1
    Here, the method is just swapping the parameter variables which have a scope of the method where its declared and once the method exits out or terminated, it loses all memory of the swap.

    swap2
    Here, the method is not changing the parameter variables but its editing the instance variables which have a scope thoughout the class. So when you try to swap it is indeed swapped.

  3. #3
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default

    Swap means exchange the values to two or more variables.
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Similar Threads

  1. web tutorial
    By poobeer in forum Web Frameworks
    Replies: 1
    Last Post: 10-24-2009, 03:10 AM
  2. ejb tutorial
    By marcellis in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 09-30-2009, 02:43 PM
  3. tutorial
    By ayangupta in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-01-2009, 07:38 PM
  4. Tutorial?
    By Gudradain in forum Java Software
    Replies: 24
    Last Post: 01-09-2009, 04:22 AM
  5. tutorial help
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-12-2008, 05:34 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
  •