Results 1 to 3 of 3
Thread: Need help with my tutorial
- 01-27-2010, 06:15 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
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!
- 01-27-2010, 07:24 AM #2
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 11
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(); } }
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.
- 02-05-2010, 04:00 PM #3
Similar Threads
-
web tutorial
By poobeer in forum Web FrameworksReplies: 1Last Post: 10-24-2009, 03:10 AM -
ejb tutorial
By marcellis in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 09-30-2009, 02:43 PM -
tutorial
By ayangupta in forum Threads and SynchronizationReplies: 1Last Post: 03-01-2009, 07:38 PM -
Tutorial?
By Gudradain in forum Java SoftwareReplies: 24Last Post: 01-09-2009, 04:22 AM -
tutorial help
By yuriythebest in forum Java AppletsReplies: 1Last Post: 11-12-2008, 05:34 PM
Bookmarks