(1) Cards are ranked by the rank first, as follows:
Ace > King > Queen > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2
(2) When 2 cards have the same rank,
the cards are ranked by their suit as follows:
Spades > Hearts > Diamonds > Clubs
|
For examples:
4 of Clubs > Ace of Spades ? false because Ace > 4
4 of Clubs > Ace of Clubs ? false because Ace > 4
Ace of Spades > Ace of Clubs ? true the card ranks (Ace) are same
and Spades > Clubs
|
To make the comparison between 2 cards easier, we decide to represent a Card object using integers for suit and rank, like this:
Card Rank Card Suit
------------------ -----------------
"2" --> 0 "Clubs" --> 0
"3" --> 1 "Diamonds --> 1
"4" --> 2 "Hearts" --> 2
"5" --> 3 "Spades" --> 3
"6" --> 4
"7" --> 5
"8" --> 6
"9" --> 7
"10" --> 8
"Jack" --> 9
"Quuen" --> 10
"King" --> 11
"Ace" --> 12
|
I have re-written the Card class that we have studied for you that uses the new representation:
import java.util.ArrayList;
import java.util.Arrays;
public class Card
{
private int suit;
private int rank;
// Constructor
Card(String cardSuit, String cardRank)
{
String[] suitName = { "Clubs", "Diamonds", "Hearts", "Spades"};
String[] rankName = { "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King", "Ace"};
ArrayList mySuit = new ArrayList<>( Arrays.asList(suitName) );
ArrayList myRank = new ArrayList<>( Arrays.asList(rankName) );
suit = mySuit.indexOf(cardSuit);
rank = myRank.indexOf(cardRank);
}
public String toString() // Used to print a card
{
String[] suitName = { "Clubs", "Diamonds", "Hearts", "Spades"};
String[] rankName = { "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King", "Ace"};
return rankName[rank] + " of " + suitName[suit];
}
public boolean higher( Card c )
{
// This method checks if this card ranks higher than the card c
// You need to write this method for the assignment
}
}
|
The Card class contains an updated constructor method and an updated toString() method used to print a Card object.
The method higher(card c) in the Card class will check if this card ranks higher that the input Card c. It must return true if this card ranks higher that the input Card c and return false otherwise.
You must complete the higher( ) method for this assignment.
Here is the Java program that you can use to test the new Card class after you completed the higher( ) method:
public class myProg
{
public static void main(String[] args)
{
Card c1 = new Card("Clubs", "4");
Card c2 = new Card("Spades", "Ace");
Card c3 = new Card("Clubs", "Ace");
System.out.println( c1 );
System.out.println( c2 );
System.out.println( c3 );
System.out.println( );
System.out.println(c1 + " > " + c2 + ": " + c1.higher(c2) );
System.out.println(c1 + " > " + c3 + ": " + c1.higher(c3) );
System.out.println(c2 + " > " + c3 + ": " + c2.higher(c3) );
}
}
|
If your program is correct, it will print out this message:
4 of Clubs Ace of Spades Ace of Clubs 4 of Clubs > Ace of Spades: false 4 of Clubs > Ace of Clubs: false Ace of Spades > Ace of Clubs: true |