JMU
Swapping
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Review
Review (cont.)
javaexamples/programmingpatterns/Swapping.java (Fragment: assignment)
                char swordOfAlice, swordOfBetty;
        
        swordOfAlice = 'C'; // Caladbolg
        swordOfBetty = 'D'; // Durendal
        
images/Swapping_Assignment.svg
A Defective Algorithm (cont.)
javaexamples/programmingpatterns/Swapping.java (Fragment: badswap)
                swordOfAlice = swordOfBetty;
        swordOfBetty = swordOfAlice;
        
images/Swapping_BadSwap.svg
Thinking About the Problem
The Pattern
The Pattern (cont.)
images/Swapping.svg
Example
javaexamples/programmingpatterns/Swapping.java (Fragment: swordswap)
                temp = swordOfAlice;        
        swordOfAlice = swordOfBetty;
        swordOfBetty = temp;
        
The Example (cont.)
images/Swapping_SwapSteps.svg
A Warning