home / study / engineering / computer science / computer science questions and a
ID: 3592558 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / java homework help the book is data structures and algorithims 6th edition for this assignment ...
Question: Java Homework Help The book is Data Structures and Algorithims 6th Edition For this assignment yo...
Java Homework Help
The book is Data Structures and Algorithims 6th Edition
In the project you will do the following (not listed in order):
Complete assignment P-7.60 on page 305 of the textbook that implements the CardHand class with the additional requirements as listed.
Problem 7-60
P-7.60 Implement a CardHand class that supports a person arranging a group of cards in his or her hand. The simulator should represent the sequence of cards using a single positional list ADT so that cards of the same suit are kept together. Implement this strategy by means of four “fingers” into the hand, one for each of the suits of hearts, clubs, spades, and diamonds, so that adding a new card to the person’s hand or playing a correct card from the hand can be done in constant time. The class should support the following methods:
•addCard(r, s): Add a new card with rank r and suit s to the hand.
•play(s): Remove and return a card of suit s from the player’s hand; if there is no card of suit s, then remove and return an arbitrary card from the hand.
•iterator( ): Return an iterator for all cards currently in the hand. •suitIterator(s): Return an iterator for all cards of suit s that are currently in the hand.
When arranging your cards:
you should not worry about the relative values of the suites (i.e. does a Heart come before a Diamond).
Rather you should arrange the suits so that the first suit you are dealt is to the “left” (first) and additional suits are added to the “right” (later) as they are received.
Within a suit you so not need to worry about relative ranks of the cards. Just group all of the cards for a given suit together.
Implement your CardHand class so that adding a new card can be done in constant time, O( 1 ).
You must use a LinkedPositionalList to represent the card hand.
You must use the “four fingers” method suggested in the textbook.
You do NOT need to implement the play(s) method described in the text.
You DO need to implement the addCard(r,s) [ you can implement an addCard( card ) instead ], iterator() and suitIterator(s) methods described in the text.
2. Implement a Card class that represents a playing card.
A card has a suit ( Club, Diamond, Heart, or Spade )
A card has a value ( 2 through 10, Jack, Queen, King, or Ace) with 2 being the lowest value and Ace being the highest value.
You do not need to support Jokers
3. Implement a Deck class that represents a deck of cards. This Deck class must include the following methods:
Deck( ): the constructor that creates a deck of 52 cards, one of each possible suit-value combination.
card( ): deals a random card from the deck, where: 1) cards are dealt on a non-replacement basis (i.e. once a card is dealt it cannot be dealt again), and 2) on each deal all cards (remaining) in the deck have an equal probability of being dealt.
Selecting the correct data structure should make this easy to implement.
4. Implement a Game class that represents a card game. This Game class must include:
An instance variable that represents the number of players in the game (should be passed in as a parameter).
An instance variable that represents the maximum number of cards that can be in each player’s hand (should be passed in as a parameter).
An instance variable for the deck of cards used in the game.
An instance variable that is an array of the player’s card hands.
A method getCard() that deals one card to each player in the array of players (item c). When a player gets a new card they should immediately add this card to their hand so that they are ordered correctly by suit and value (item 1).
Note that the player must immediately insert each new card into the correct position in their hand. The player cannot wait until they have received all of their cards and then sort the cards.
5. A client that tests your classes by dealing a card game. Your client should:
Create a new instance of a game with four ( 4 ) players, and 13 cards per player (e.g. like a game of Bridge).
Have cards dealt to each player one card at a time and display the hands of each player after each round is dealt. You output should look something like the following (Keep in mind that the order of the suits in each player’s hand is determined by the order the suits are received):
Card 01:
Player 1: 2C
Player 2: KH
Player 3: 8H
Player 4: 2D
Card 02:
Player 1: 2C 7D
Player 2: KH 9S
Player 3: 3H 8H
Player 4: 9D 2D
And so on until
Card 13:
Player 1: 7C QC 10C 9C 5C KC 2C 5D 7D 6D KS AH 4H
Player 2: 3H QH JH KH 4S 9S 7S JS 4D 8D 4D 3D 10C
Player 3: 5H 8H 6H 7H 10H 8C 3C AD KD QS 6S 5S 3S
Player 4: JD QD 2D 9D AS 8S 10S 2S 9H 2H 6C JC AC
Explanation / Answer
A single iterator instance supports only one pass through a collection; calls to next
can be made until all elements have been reported, but there is no way to “reset”
the iterator back to the beginning of the sequence.
However, a data structure that wishes to allow repeated iterations can support
a method that returns a new iterator, each time it is called. To provide greater
standardization, Java denes another parameterized interface, named Iterable, that
includes the following single method:
iterator( ): Returns an iterator of the elements in the collection.
An instance of a typical collection class in Java, such as an ArrayList, is iterable
(but not itself an iterator); it produces an iterator for its collection as the return value
of the iterator( ) method. Each call to iterator( ) returns a new iterator instance,
thereby allowing multiple (even simultaneous) traversals of a collection.
Java’s Iterable class also plays a fundamental role in support of the “for-each”
loop syntax (described in Section 1.5.2). The loop syntax,
for (ElementType variable : collection) { // may refer to ”variable”
loopBody
}
is supported for any instance, collection, of an iterable class. ElementType must be
the type of object returned by its iterator, and variable will take on element values
within the loopBody. Essentially, this syntax is shorthand for the following:
Iterator<ElementType> iter = collection.iterator( );
while (iter.hasNext( )) {
ElementType variable = iter.next( );
loopBody // may refer to ”variable”
}
We note that the iterator’s remove method cannot be invoked when using the
for-each loop syntax. Instead, we must explicitly use an iterator. As an example,
the following loop can be used to remove all negative numbers from an ArrayList
of oating-point values.
ArrayList<Double> data; // populate with random numbers (not shown)
Iterator<Double> walk = data.iterator( );
while (walk.hasNext( ))
if (walk.next( ) < 0.0)
walk.remove( );
www.it-ebooks.info
284 Chapter 7. List and Iterator ADTs
7.4.2 Implementing Iterators
There are two general styles for implementing iterators that differ in terms of what
work is done when the iterator instance is rst created, and what work is done each
time the iterator is advanced with a call to next( ).
A snapshot iterator maintains its own private copy of the sequence of elements,
which is constructed at the time the iterator object is created. It effectively records
a “snapshot” of the sequence of elements at the time the iterator is created, and is
therefore unaffected by any subsequent changes to the primary collection that may
occur. Implementing snapshot iterators tends to be very easy, as it requires a simple
traversal of the primary structure. The downside of this style of iterator is that it
requires O(n) time and O(n) auxiliary space, upon construction, to copy and store
a collection of n elements.
A lazy iterator is one that does not make an upfront copy, instead perform-
ing a piecewise traversal of the primary structure only when the next( ) method is
called to request another element. The advantage of this style of iterator is that
it can typically be implemented so the iterator requires only O(1) space and O(1)
construction time. One downside (or feature) of a lazy iterator is that its behavior
is affected if the primary structure is modied (by means other than by the itera-
tor’s own remove method) before the iteration completes. Many of the iterators in
Java’s libraries implement a “fail-fast” behavior that immediately invalidates such
an iterator if its underlying collection is modied unexpectedly.
We will demonstrate how to implement iterators for both the ArrayList and
LinkedPositionalList classes as examples. We implement lazy iterators for both,
including support for the remove operation (but without any fail-fast guarantee).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.