Write a program that simulates a deck of cards using java. Use the scanner class
ID: 3737686 • Letter: W
Question
Write a program that simulates a deck of cards using java. Use the scanner class. There are 52 cards in a deck. A deck consists of four suits (Diamonds, Hearts, Clubs and Spades). Each suit contains 13 values (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen and King). The user will indicate how many cards they wish to draw from a deck of cards. Your program will then draw that many cards and then display the suits and values of all the cards drawn. You can assume you are drawing with replacement, which means you put each card back into the deck after drawing it.
Example Run
Assignment Requirements
Create a method that accepts one integer parameter and has a String array (or String[]) return type. The method will randomly draw cards from a simulated deck and add them to a string array that is returned after cards are drawn.
Create a method that accepts one String array (or String[]) parameter and has a void return type. The method will perform the System.out.println() function on all elements of the string array.
The main method of the program will receive user input and call both methods.
User input will be the parameter for the method 1 call
The returned string array from method 1 will be the parameter for the method 2 call
You may choose to use either the console or the Java dialog boxes for data input and output.
If the number of cards to draw is zero or a negative, your program should report the error and exit the program: you can do this with System.exit(0)
Perform the following test cases:
Cards to draw
Test run 1
10
Test runs 2, 3, 4
30 (perform 3 times)
Test run 5
0
Test run 6
-10
Cards to draw
Test run 1
10
Test runs 2, 3, 4
30 (perform 3 times)
Test run 5
0
Test run 6
-10
Explanation / Answer
import java.util.*;
public class main {
// You can declare this and other member functions as the non-static member of the class.
//I am using main class only so using them to be static
private static String deck[];
/***
* The method will randomly draw cards from a simulated deck and add them to
a string array that is returned after cards are drawn
* @param numberOfCardsDrawn
* @return
*/
public static String[] DrawCards(int numberOfCardsDrawn)
{
String draw[] = new String[numberOfCardsDrawn];
// determining random index, which will be used to draw the cards
int array[] = new int[52];
for(int i = 0; i < 52; i++)
array[i]= i;
List<Integer> list = new ArrayList<Integer>();
for (int i : array)
list.add(i);
Collections.shuffle(list);
for (int j = 0; j < list.size(); j++) {
array[j] = list.get(j);
}
for(int itr = 0; itr < numberOfCardsDrawn; itr++)
{
draw[itr] = deck[array[itr]];
}
return draw;
}
/**
*
* @param deckArray
*/
public static void Printdeck(String[] deckArray)
{
for(int i = 0; i < deckArray.length; i++)
{
System.out.println(deckArray[i]);
}
}
public static String[] CreateDeck()
{
String theDeck[] = new String[52];
String suiteName = "Heart";
for(int itr = 0; itr < 52; itr++)
{
String value;
if(itr/13 == 0)
suiteName = "Heart";
else if(itr/13 == 1)
suiteName = "Club";
else if(itr/13 == 2)
suiteName = "Spade";
else if(itr/13 == 3)
suiteName = "Diamond";
if(itr%13 >= 0 && itr%13 < 10)
{
value = suiteName + ":" + (itr%13+1);
theDeck[itr] = value;
}
else if(itr%13 >= 10 && itr%13 <13 )
{
value = suiteName + ":" + "Jack";
theDeck[itr] = value; itr++;
value = suiteName + ":" + "Queen";
theDeck[itr] = value; itr++;
value = suiteName + ":" + "King";
theDeck[itr] = value;
}
}
return theDeck;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
deck = CreateDeck();
int noOfDraws;
System.out.println("Please enter number of cards to draw="); // print n
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt())
{
noOfDraws = scanner.nextInt(); // read an integer from input stream.
if(noOfDraws <= 0)
{
System.out.println("Incorrect value entered!!");
System.exit(0);
}
else
{
String DrawnCards[] = DrawCards(noOfDraws);
Printdeck(DrawnCards);
}
}
else
{
System.out.println("Incorrect value entered!!");
System.exit(0);
}
}
}
/** test run 1:
Please enter number of cards to draw=
10
Diamond:10
Diamond:6
Spade:9
Heart:Jack
Heart:Queen
Diamond:3
Spade:Jack
Club:8
Diamond:8
Heart:6
** test run 2:
Please enter number of cards to draw=
2
Heart:2
Spade:3
* test run 3:
Please enter number of cards to draw=
3
Spade:10
Spade:5
Club:King
* test run 4:
Please enter number of cards to draw=
4
Spade:3
Heart:9
Heart:4
Club:6
* test run 5:
Please enter number of cards to draw=
0
Incorrect value entered!!
* test run 6:
Please enter number of cards to draw=
-10
Incorrect value entered!!
***/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.