Question
Purpose The purpose of this lab is for you to work with many array list objects in many classes. The problem You will implement classes
Purpose
The purpose of this lab is for you to work with many array list objects in many classes.
The problem
You will implement classes that can be useful when writing card game programs.
A playing card has a rank and a suit. The rank of a card is 2 through 10 for the number cards, then Jack, Queen, King and Ace. Suits are clubs, diamonds, hearts and spades.
A brand new, fresh deck of cards always starts with 52 cards, in sorted order.
A hand is always 5 cards, which will be dealt from the deck.
You are required to implement the details of each class given below.
The Card class
Represents one single playing card. Aces will always be high i.e. Aces rank above Kings, not below 2s. No Jokers.
Must be able to create a new card from a rank and suit. Need getter methods for rank and suit. toString() must nicely format a card.
Constants
Reminder that appropriate constants are required in Card and all other classes, for clarity and to avoid magic numbers.
Required Exactly and only 2 instance variables
Both are implemented as integer, for easy creation and handling:
private final int rank; -valid values are: 2..10, J=11, Q=12, K=13, A=14 "aces high"
private final int suit; -valid values are: 0=clubs, 1=diamonds, 2=hearts, 3=spades
About the constructor
Must take two parameters to set a new cards rank and suit.
Must print an appropriate error message and exit the program if the rank, suit are not valid.
About toString()
Must nicely format a card. Is always rank followed by suit. Make the String 3 places wide, for better alignment. Some examples (with showing the space character):
rank 2, suit 0 is 2
rank 10 suit 1 is 10
rank 13 suit 2 is K
rank 14 suit 3 is A
So the rank part should always be 2 places wide.
Use the Unicode character codes to get the single, special character for suit e.g. in toString():
String out = "";
// put the rank into out
// always in 2 places, for nice formatting
. . .
// now add single, special char for suit
// clubs are black
if (suit == CLUBS)
out += '\u2663'; // Unicode char for black club
// diamonds are red
else if (suit == DIAMONDS)
out += '\u2662'; // white diamond. Closest to red
// hearts are red
else if (suit == HEARTS)
out += '\u2661'; // white heart
// spades are black
else if (suit == SPADES)
out += '\u2660'; // black spade
(BTW, these Unicode character codes for suit do not print correctly on every computer, in every text program. Is nothing to worry about.)
The Deck class
Represents a deck of cards. A brand new, fresh deck always starts with 52 cards in sorted order, rank within suit.
Must be able to create a brand new, fresh deck. Can shuffle a deck into random order. Deal a card. toString() must nicely format a deck.
Will use an array list since the size of a deck changes as cards are dealt.
Required Exactly and only 1 instance variable:
private ArrayList cards;
About the constructor
Creates a brand new, fresh deck of 52 cards in sorted order, rank within suit. Suit order is clubs, diamonds, hearts then spades.
About the shuffle() method
Shuffles the deck of cards into a random order.
About the dealCard() method
Deals a card from the top of the deck. Must remove and return the card at index 0, reducing the size of the deck.
Must print an appropriate error message and exit the program if the deck is empty.
About toString()
Must nicely format a deck. Break the line every 13 cards to make a full deck easy to read.
The Hand class
Represents a hand of 5 cards. A new hand always starts empty, then cards are dealt from the deck which are added to the hand.
Must be able to create a brand new, empty hand. Can add a card from the deck to the end of a hand.
Will use an array list since the size of a hand changes as cards are dealt.
Required Exactly and only 1 instance variable:
private ArrayList cards;
public void addCard(Card c)
Adds a card to the end of the hand. Must take a card object as its parameter, as can be seen in the method header given here.
About toString()
No special formatting required, so simply:
public String toString()
{
return cards.toString();
}
The DealHands class
Write a DealHands class that controls everything and runs your classes. Contains only a single main() method, that implements the following steps in order:
-create a new deck object and print it out, nicely labelled
-shuffle the deck then print it out, nicely labelled
-prompt the user to enter the number of players, then read this number from the keyboard
-create a new array list object named hands and add this many empty hand objects
-deal cards correctly to the players. So one card must be dealt from the deck to each hand in turn, until all players have 5 cards
-print each hand
-print what remains in the deck, nicely labelled
-now test error handling, nicely labelled
-try to create a bad card. (Comment out your System.exit() method calls, so that the program keeps running after this)
-write a loop that deliberately tries to deal a card from the deck after it has been emptied
For example, a run of the program would look something like:
A brand new, fresh deck
2 3 4 5 6 7 8 9 10 J Q K A
2 3 4 5 6 7 8 9 10 J Q K A
2 3 4 5 6 7 8 9 10 J Q K A
2 3 4 5 6 7 8 9 10 J Q K A
Shuffled
4 7 5 3 J J K 6 K 9 3 2 8
Q A 7 J 6 10 9 4 10 J 9 5 5
K 2 5 6 3 K 8 Q 4 A 6 7 9
Q 8 10 2 Q 8 4 A 3 7 2 A 10
How many players?
6 [ 4, K, 8, 10, 5]
[ 7, 6, Q, 9, 5]
[ 5, K, A, 4, K]
[ 3, 9, 7, 10, 2]
[ J, 3, J, J, 5]
[ J, 2, 6, 9, 6]
What remains
3 K 8 Q 4 A 6 7 9 Q 8 10 2
Q 8 4 A 3 7 2 A 10
Test error handling
In Card::ctor() -- rankIn not valid: 15
In Card::ctor() -- suitIn not valid: -1
In Deck::dealCard() -- deck is empty
(Obviously your output will be different, since the deck is shuffled.)
Hints
You are writing four new classes named Card, Deck, Hand and DealHands. The last three all use array lists.
-design your new classes on a piece of paper
-identify instance variables and data types
-identify methods, design algorithms, think about parameters and return values
-identify constants, for clarity and to avoid magic numbers
-now use BlueJ to write and test your classes and methods as you go
-(create a new project in BlueJ, then click New Class to start writing your program)
-write a method, create a new object and test the new method
-finish one method before starting the next
-finally, have DealHands run your program. Check that the output in the Terminal Window is correct, then use Options | Save to file to save your output file as output.txt
Required
Failure to meet these requirements will cost you points:
-you must run your program for 3 players, when you submit your lab
-every class must have appropriate constants, for clarity and to avoid magic numbers
-every class and method must have a meaningful, correct Javadoc comment -automatically and routinely use all the other components of simplicity and clarity, as listed in Canvas, Course Information, How labs are graded
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started