Swap program trace: |
|
|
|
|
|
|
|
/** |
|
*
Swap variables |
|
* |
|
*
@author pepper |
|
*
@version 1/26/2009 |
|
*/ |
|
public class Swap |
|
{ |
|
|
|
public static void main() |
|
{ |
|
swapIt(); |
|
} |
|
public static void swapIt() |
value in firstCup |
value in secondCup |
value in thirdCup |
{ |
|
int firstCup = 5; |
|
|
|
int secondCup = 6; |
|
|
|
int thirdCup; |
|
|
|
|
|
System.out.println("The first cup holds " + firstCup); |
|
System.out.println("The second cup
holds " + secondCup); |
|
|
|
thirdCup = firstCup; |
|
|
|
firstCup = secondCup; |
|
|
|
secondCup = thirdCup; |
|
|
|
|
|
System.out.println("The first cup holds " + firstCup); |
|
System.out.println("The second cup
holds " + secondCup); |
|
System.out.println("Did it swap
the values in the cup?"); |
|
|
|
System.out.println("Now I will
combine cups 2 & 1 into 2 and then switch it back"); |
|
secondCup = secondCup + firstCup
; |
|
|
|
System.out.println("The second cup
holds " + secondCup); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("The first cup holds " + firstCup); |
|
System.out.println("The second cup
holds " + secondCup); |
|
System.out.println("Did it swap
the values in the cup?"); |
|
} |
|
|
|
} |
|
|
|
|
|