Question
Elevans Lab Activity 2: Initial Design of a Deck Class (this activity has a main class and java class) Introduction: Think about a deck of
Elevans Lab
Activity 2: Initial Design of a Deck Class (this activity has a main class and java class)
Introduction:
Think about a deck of cards. How would you describe a deck of cards? When you play card games, what kinds of operations do these games require a deck to provide?
Exploration:
Now consider implementing a class to represent a deck of cards. Describe its instance variables and methods, and discuss your design with a classmate.
Read the partial implementation of the Deck class available in the Activity2 Starter Code folder. This file contains the instance variables, constructor header, and method headers for a Deck class general enough to be useful for a variety of card games. Discuss the Deck class with your classmates; in particular, make sure you understand the role of each of the parameters to the Deck constructor, and of each of the private instance variables in the Deck class.
Exercises:
1. Complete the implementation of the Deck class by coding each of the
following:
Deck constructor This constructor receives three arrays as parameters. The arrays contain the ranks(String), suits(String), and point values(int) for each card in the deck. The constructor creates an ArrayList, and then creates the specified cards and adds them to the list.
For example:
String[] ranks = {"A", "B", "C"};
String[] suits = {"Giraffes", "Lions"};
int[] values = {2,1,6};
The constructor would create the following cards:
["A", "Giraffes", 2],
["B", "Giraffes", 1],
["C", "Giraffes", 6],
["A", "Lions", 2],
["B", "Lions", 1],
["C", "Lions", 6]
and would add each of them to cards. The parameter size would then be
set to the size of cards, which in this example is 6.
Finally, the constructor should shuffle the deck by calling the shuffle
method. Note that you will not be implementing the shuffle method until
Activity 4.
isEmpty This method should return true when the size of the deck is 0;
false otherwise.
size This method returns the number of cards in the deck that are left
to be dealt.
deal This method deals a card by removing a card from the deck and
returning it, if there are any cards in the deck left to be
dealt. It returns null if the deck is empty. There are several
ways of accomplishing this task.
Here are two possible algorithms:
Algorithm 1: Because the cards are being held in an ArrayList, it would be easy to simply call the List method that removes an object at a specified index, and return that object. Removing the object from the end of the list would be more efficient than removing it from the beginning of the list. Note that the use of this algorithm also requires a separate discard list to keep track of the dealt cards. This is necessary so that the dealt cards can be reshuffled and dealt again.
Algorithm 2: It would be more efficient to leave the cards in the list. Instead of removing the card, simply decrement the size instance variable and then return the card at size. In this algorithm, the size instance variable does double duty; it determines which card to deal and it also represents how many cards in the deck are left to be dealt. This is the algorithm that you should implement.
NOTE: toString method is already implemented for you on this Activity
2. Once you have completed the Deck class, find DeckTester.java file in the Activity2 Starter Code folder. Add code in the main method to create three Deck objects and test each method for each Deck object.
Questions: (Answer questions as a footer to the lab)
1. Explain in your own words the relationship between a deck and a card.
2. Consider the deck initialized with the statements below. How many cards
does the deck contain?
String[] ranks = {"jack", "queen", "king"}; String[] suits = {"blue", "red"};
int[] pointValues = {11, 12, 13};
Deck d = new Deck(ranks, suits, pointValues);
3. The game of Twenty-One is played with a deck of 52 cards. Ranks run from
ace (highest) down to 2 (lowest). Suits are spades, hearts, diamonds, and
clubs as in many other games. A face card has point value 10; an ace has
point value 11; point values for 2, , 10 are 2, , 10, respectively.
Specify the contents of the ranks, suits, and pointValues arrays so that
the statement
Deck d = new Deck(ranks, suits, pointValues);
initializes a deck for a Twenty-One game.
4. Does the order of elements of the ranks, suits, and pointValues arrays matter?
Sample Output for Activity 2
**** Original Deck Methods ****
toString:
size = 6
Undealt cards:
king of red (point value = 13), king of blue (point value = 13),
queen of red (point value = 12), queen of blue (point value = 12),
jack of red (point value = 11), jack of blue (point value = 11)
Dealt cards:
isEmpty: false
size: 6
**** Deal a Card ****
deal: king of red (point value = 13)
**** Deck Methods After 1 Card Dealt ****
toString:
size = 5
Undealt cards:
king of blue (point value = 13), queen of red (point value = 12),
queen of blue (point value = 12), jack of red (point value = 11),
jack of blue (point value = 11)
Dealt cards:
king of red (point value = 13)
isEmpty: false
size: 5
**** Deal Remaining 5 Cards ****
deal: king of blue (point value = 13)
deal: queen of red (point value = 12)
deal: queen of blue (point value = 12)
deal: jack of red (point value = 11)
deal: jack of blue (point value = 11)
**** Deck Methods After All Cards Dealt ****
toString:
size = 0
Undealt cards:
Dealt cards:
king of red (point value = 13), king of blue (point value = 13),
queen of red (point value = 12), queen of blue (point value = 12),
jack of red (point value = 11), jack of blue (point value = 11)
isEmpty: true
size: 0
**** Deal a Card From Empty Deck ****
deal: null
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