CSE1020 Lab 02

Part 2: Swapping two numbers

Part 2 asks you to solve the seemingly simple problem of swapping the values stored in two variables. Swapping the values stored in two variables is a very common task in computer algorithms.

You should be able to complete Part 2 in 20 minutes or less.

Re-save your work from Part 1

Recycle your program from Part 1 by changing the name of the class to IntSwap and saving the file as IntSwap.java as shown below.

 

 

Modify your program

Add a few lines of code to the end of the main method so that the values stored in first and second are exchanged (swapped). For example, if first originally had the value of 10 and second originally had the value of 20 then after swapping first would have the value 20 and second would have the value 10; your lines of code should go where the black box is in the image below.

A memory diagram for lines 9 and 10 might help you figure out how to solve this problem.

 

 

Test your program

Compile, debug, and run your program. Repeat these steps until you are convinced that your program is correct.

 

Submit

Submit your program using the command:

submit 1020 L02 IntSwap.java

and proceed to Part 3.