From N V Fitton, ideas to share with my students at Northern Virginia Community College, Alexandria campus.
I teach mathematics and computer science.

Sunday, February 27, 2005

201 swaps

Code to test the swaps we talked about recently:

public class MySwaps 
{
public static void swapPrim(int x, int y)
{
int t = x;
x = y;
y = t;
}

public static void swapObj(IntObj x, IntObj y)
{
int t = x.get();
x.set(y.get());
y.set(t);
}
}


Code for integer objects:

public class IntObj 
{
private int value;

public void set (int valueIn) {
value = valueIn;
}

public int get ( ) {
return value;
}
}


When we talk about overloading methods,
we will see that the two swaps could have the same name
with no collision provided that their parameter lists
are different.

0 Comments:

Post a Comment

<< Home