Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program 01 import java.util.Random; public class Program_01 { public static String[] cardsSet = { AC, 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C,

Java

Program 01

import java.util.Random;

public class Program_01 {

public static String[] cardsSet = {

"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",

"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",

"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",

"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD"

};

public static void initDeck(boolean[] deck)

{

for(int i = 0; i < deck.length; i++)

{

deck[i] = true;

}

}

public static boolean emptyDeck(boolean[] deck)

{

for(int i = 0; i < deck.length; i++)

{

if (deck[i] == true)

return false;

}

return true;

}

public static int dealCard(boolean[] deck)

{

Random rand = new Random();

int randNumber = rand.nextInt(52);

while(!deck[randNumber])

{

randNumber = rand.nextInt(52);

}

deck[randNumber] = false;

return randNumber;

}

public static void printCard(int card)

{

System.out.print(cardsSet[card] + " ");

}

public static void main(String[] args)

{

boolean[] myDeck = new boolean[52];

final int cardsPerRow = 8;

int cardsThisRow = 0;

int myCard;

initDeck(myDeck);

System.out.println(" Here is a shuffled deck ... ");

while (!emptyDeck(myDeck))

{

myCard = dealCard(myDeck);

++cardsThisRow;

if (cardsThisRow <= cardsPerRow)

{

printCard(myCard);

System.out.print(" ");

}

else

{

System.out.println("");

cardsThisRow = 1;

printCard(myCard);

System.out.print(" ");

}

}

System.out.println(" ");

}

}

Main topics:

Multiple Classes

Declaring / Using Instance Variables

Driver Classes

Program Specification:

You are to write a Class Deck which emulates a full deck of playing cards. That is 4 suits (Clubs, Spades, Hearts, and Diamonds) and 13 ranks (Ace, 2, 3, 4, 5, 6, 7, 8, 9, Jack, Queen, King) in each suit. This of course makes for a total of 52 playing cards in the deck.

Mandatory Instance variable:

private boolean[] deck = new boolean[52];

Mandatory Instance and Class methods:

public void initDeck()

// set the values of deck to indicate that they are all

// pressent - not delt yet.

public boolean emptyDeck()

// returns wheather or not all the cards in the deck

// have already been delt.

public int dealCard()

// returns a card (an int in the range 0 to 51) at random

// that has not been delt since the deck was initialize

// via intDeck. Also notes (in deck) that this card is

// no longer available.

public static String cardToString(int card)

// given a card (an int in the range 0 to 51) returns

// an appropriate String repressentation of this card

// based on a 1-1 and onto mapping of the set [0, 51]

// to the cards described above.

You are also to write a Driver Class DeckDriver to test your Deck class.

Mandatory Functionality:

Your driver class must minimally print all the cards in the deck in the random order that they are dealt.

Rules and Requirements:

All access to the instance variable(s) in your deck classes instance methods must be made via this.

Notes and Hint:

1. You should be able to re-use much of your methods code from Program 1 in writing your deck class.

2. You should be able to re-write your main method from Program 1 into your driver class with minimal modification / effort.

Lastly you are to write a second deck class SmartDeck which adds a second instance variable cardsDealt that at all times contains the number of cards dealt since that last call to initDeck()

Notes and Hint:

1. cardsDealt will need to be modified by initDeck(), and dealCard(), and will allow you to write emptyDeck() without the use of a loop.

2. Your DeckDriver class must also work identically whether myDeck is declared as Deck or SmartDeck.

Sample run(s):

Run 1: - with Deck class -

-----------------------------------------------------------

Here is a shuffled deck ...

7S KS 2H 6S 4C 2D 9D 9C 4H 7C 9H 3D 5H 5D 10S 2S JH AH 4S KC QC AD QD 7D AS KD 5C 7H KH 3C JC 2C 4D 8H AC 5S 10C JS 3H 9S 8D 10D 8S 6C QH 8C JD 3S QS 6D 10H 6H

Run 2: - with SmartDeck class -

-----------------------------------------------------------

Here is a shuffled deck ...

2D 10C AD 6C JC JH KS 4S 9C 9S 2S AC QS 3C 3H 8C 3S QC AS 4D 10S 2C 8S 6D 6S 9H 2H 5S JD KD QH 10D 7H QD 3D 6H 7D 8H 5D 4H KH AH 8D 7C 9D 7S 5C 5H KC JS 4C 10H

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

13

Answered: 1 week ago