CJCoding With Joseph
15free / daySign inCreate account

Thompson Rivers University

Question 3

Lab 4 Collections - SM 2025

Take your Pair2<T> class from Question 2 and add a new method swap() that exchanges the two stored values. In other words, when swap() is called, the original first moves into second and the original second moves into first, using a temporary variable of type T to perform the exchange.


Why This Matters

Sometimes you need to reverse or swap the two values stored in a pair without creating a brand-new object. By adding swap(), you let the same Pair2<T> instance flip its contents in place, which can be more efficient than reconstructing a new pair.


Original Pair2<T> Definition for Reference

Below is the Pair2<T> class from Question 2 so you do not need to open that file. We will modify this into a new class called Pair3<T> by adding a swap() method.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 54 more lessons in Thompson Rivers University.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.