Implement poker game program in package PJ4: Part I (40%) Implement Decks class
ID: 3570380 • Letter: I
Question
Implement poker game program in package PJ4:
Part I (40%) Implement Decks class
Part II (60%) Implement MyPokerGame class
Bonus (30%) Add GUI, only after you have completed I & II
Must complete by same due date
Talk to me before you start this part.
See PJ4/MyPlayingCard.java and PJ4/MyPokerGame.java for more info.
- Use TestMyPokerGame.java to test correctness of your program
- Compile programs (you are in directory containing Readme file):
javac PJ4/*.java
javac TestMyPokerGame.java
Explanation / Answer
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank - valid values are 1 to 13
* Suit - valid values are 0 to 3
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw MyPlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws MyPlayingCardException {
if ((rank < 1) || (rank > 13))
throw new MyPlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new MyPlayingCardException("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(1,3); // A Spades
System.out.println(c1);
c1 = new Card(10,0); // 10 Clubs
System.out.println(c1);
c1 = new Card(10,5); // generate exception here
}
catch (MyPlayingCardException e)
{
System.out.println("MyPlayingCardException: "+e.getMessage());
}
}
}
class Decks {