Java - BJP3 Exercise 14.9: switchPairs Write a method switchPairs that takes a s
ID: 654566 • Letter: J
Question
Java - BJP3 Exercise 14.9: switchPairs
Write a method switchPairs that takes a stack of integers as a parameter and that switches successive pairs of numbers starting at the bottom of the stack. For example, if the stack initially stores these values:
Your method should switch the first pair (3, 8), the second pair (17, 9), the third pair (99, 9), and so on, yielding this sequence:
If there are an odd number of values in the stack, the value at the top of the stack is not moved. For example, if the original stack had stored:
It would again switch pairs of values, but the value at the top of the stack (42) would not be moved, yielding this sequence:
Do not make assumptions about how many elements are in the stack. Use one queue as auxiliary storage.