swap method in Java

Joined
Aug 16, 2007
Messages
1
Reaction score
0
public class Swap {
String jas = "jas";
String jaswinder = "jaswinder";

public String getJas(){
return jas;
}
public void setJas(String jas){
this.jas = jas;
}

public void setJaswinder(String jaswinder){
this.jaswinder = jaswinder;
}
public String getJaswinder(){
return jaswinder;
}

public void swap(String s1, String s2){
setJas(s2);
setJaswinder(s1);
}
public static void main(String[] args) {
Swap obj = new Swap();
obj.swap(obj.jas, obj.jaswinder);
System.out.println(obj.jas);
System.out.println(obj.jaswinder);
}
}
 
Joined
Sep 25, 2011
Messages
1
Reaction score
0
use this

package com.company;

public class SwapClass{

int a = 1;
int b = 2;

public static void main (String []args) {
SwapClass swapInstance = new SwapClass();
swapInstance.swapInstanceMethodNotWorks();
swapInstance.swapInstanceMethodThisWorks();
}

public void swapInstanceMethodNotWorks () {
System.out.println("Before a is " + a);
System.out.println("Before b is " + b);
this.swapNoThisNoWork(a,b);
System.out.println("After a is still " + a);
System.out.println("After b is still " + b);
}

public void swapInstanceMethodThisWorks () {
System.out.println("Before a is " + a);
System.out.println("Before b is " + b);
this.swapWithThisWorks(a,b);
System.out.println("After a is now " + a);
System.out.println("After b is now " + b);
}

public void swapNoThisNoWork (int a , int b) {
int localTempHolderVar;
localTempHolderVar = a;
a = b;
b = localTempHolderVar;
}

public void swapWithThisWorks (int a , int b) {
int localTempHolderVar;
localTempHolderVar = a;
this.a = b;
this.b = localTempHolderVar;
}

}


Before a is 1
Before b is 2
After a is still 1
After b is still 2
Before a is 1
Before b is 2
After a is now 2
After b is now 1
 
Joined
Jan 17, 2012
Messages
1
Reaction score
0
java swapping

can you help me with my project?
what is the code for java sorting in swapping 5 numbers??? i really need it right now..
thanks. :):)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top