next up previous
Next: About this document ... Up: Assignment 10: Homemade Objects Previous: The Card class

Compare cards

1.
Write a method called compareCards that takes two cards as arguments and returns an integer with value -1 if the first card is less than the second, 1 if the first is greater than the second, and 0 if the two cards are the same. One card outranks another if its rank is higher or, if the two ranks are the same, if its suit is higher.

At this point, you might want to give some thought to the mapping you chose for the ranks. An obvious choice is something like

\begin{eqnarray*}1 & \mapsto & Ace \\
2 & \mapsto & 2 \\
... \\
11 & \mapsto & Jack \\
12 & \mapsto & Queen \\
13 & \mapsto & King
\end{eqnarray*}


This mapping is convenient for printing, but not as convenient for comparing, since it (incorrectly) puts the Ace at the bottom, when the Ace should outrank the King. Think about the pros and cons of various solutions and implement whatever you think is the best choice.

2.
Invoke your method from main and test that it works.



Allen B. Downey
1999-04-14