Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8.32 Implement class Hand that represents a hand of playing cards. The class sho

ID: 667201 • Letter: 8

Question

8.32 Implement class Hand that represents a hand of playing cards. The class should have a
constructor that takes as input the player ID (a string). It should support method addCard()
that takes a card as input and adds it to the hand and method showHand() that displays the
player’s hand in the format shown.
>>> hand = Hand('House')
>>> deck = Deck()
>>> deck.shuffle()
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.showHand()
House: 10 8 2

8.34 Implement class Date that support methods:
• __init__(): Constructor that takes no input and initializes the Date object to the
current date
• display(): Takes a format argument and displays the date in the requested format
Use function localtime() from the Standard Library module time to obtain the current
date. The format argument is a string
• ’MDY’ : MM/DD/YY (e.g., 02/18/09)
• ’MDYY’ : MM/DD/YYYY (e.g., 02/18/2009)
• ’DMY’ : DD/MM/YY (e.g., 18/02/09)
• ’DMYY’ : DD/MM/YYYY (e.g., 18/02/2009)
• ’MODY’ : Mon DD, YYYY (e.g., Feb 18, 2009)
You should use methods localtime() and strftime() from Standard Library module
time.
>>> x = Date()
>>> x.display('MDY')
'02/18/09'
>>> x.display('MODY')
'Feb 18, 2009'

8.35 Develop a class Craps that allows you to play craps on your computer. (The craps
rules are described in Problem 6.31.) Your class will support methods:
• __init__(): Starts by rolling a pair of dice. If the value of the roll (i.e., the sum of
the two dice) is 7 or 11, then a winning message is printed. If the value of the roll
is 2, 3, or 12, then a losing message is printed. For all other roll values, a message
telling the user to throw for point is printed.
• forPoint(): Generates a roll of a pair of dice and, depending on the value of the
roll, prints one of three messages as appropriate (and as shown):
>>> c = Craps()
Throw total: 11. You won!
>>> c = Craps()
Throw total: 2. You lost!

8.39 Write a container class called PriorityQueue. The class should supports methods:
• insert(): Takes a number as input and adds it to the container
• min(): Returns the smallest number in the container
• removeMin(): Removes the smallest number in the container
• isEmpty(): Returns True if container is empty, False otherwise
The overloaded operator len() should also be supported.
>>> pq = PriorityQueue()
>>> pq.insert(3)
>>> pq.insert(1)
>>> pq.insert(5)
>>> pq.insert(2)
>>> pq.min()
1
>>> pq.removeMin()
>>> pq.min()
2
>>> pq.size()
3
>>> pq.isEmpty()
False

Explanation / Answer

class Hand
{
void Hand(String id);
void addCard(String deck.dealCard());
}

  
public class deck
{
public static final int NCARDS = 52;
  
private Card[] deck;   
private int currentCard;   
  
public deck( ) // Constructor
{
    deck = new Card[ NCARDS ];
  
    int i = 0;
  
    for ( int suit = Card.SPADE; suit <= Card.DIAMOND; suit++ )
    for ( int rank = 1; rank <= 13; rank++ )
        deck[i++] = new Card(suit, rank);
  
    currentCard = 0;
}
  
public void shuffle(int n)
{
    int i, j, k;
  
    for ( k = 0; k < n; k++ )
    {
    i = (int) ( NCARDS * Math.random() ); // Pick 2 random cards
    j = (int) ( NCARDS * Math.random() ); // in the deck
  
    Card tmp = deck[i];
    deck[i] = deck[j];
    deck[j] = tmp;;
    }
  
    currentCard = 0; // Reset current card to dealCard
}

public Card dealCard()
{
    if ( currentCard < NCARDS )
    {
    return ( deck[ currentCard++ ] );
    }
    else
    {
    System.out.println("Out of cards error");
    return ( null ); // Error;
    }
}
  
public String toString()
{
    String s = "";
    int k;
  
    k = 0;
    for ( int i = 0; i < 4; i++ )
    {
    for ( int j = 1; j <= 13; j++ )
        s += (deck[k++] + " ");
  
    s += " ";
    }
    return ( s );
}
}
}
class Hands_main
{
public static void main(String args[])
{
hand = Hand('House');
deck = Deck()
deck.shuffle()
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.toString();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote