Java We have an assignment with the tasks of “creating” a deck of cards, placing
ID: 3937562 • Letter: J
Question
Java
We have an assignment with the tasks of “creating” a deck of cards, placing the contents in a single-dimensional array, and then displaying the contents of the array. Make sure that the output matches what I expect.
Task #1 – Create your Deck
You first need to create a simple deck of cards. Use a simple one-dimensional String array to hold your card deck. The array should contain 52 locations – one for each card. Populate the array with your card data – one card per each array element. There is no Joker in the deck.
The data values that mimic the deck of cards should be:
2 of Spades
2 of Hearts
2 of Clubs
2 of Diamonds
:
King of Clubs
:
Ace of Diamonds
no jokers in deck
Task #2 – Display the Unshuffled Deck
Display your complete unshuffled deck in a comma delimited format.
2 of Spades, 2 of Hearts, 2 of Clubs, …….
Explanation / Answer
public class Deck {
public static void main(String[] args) {
//single-dimensional array to store 52 card
String deckArray[]= new String[52];
String valArray[]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
//This loop will do the task-1 of creating the deck
for(int i=0;i<13;i++){
deckArray[(i*4)]=valArray[i]+ " of Spades";
deckArray[(i*4)+1]=valArray[i]+ " of Hearts";
deckArray[(i*4)+2]=valArray[i]+ " of Clubs";
deckArray[(i*4)+3]=valArray[i]+ " of Diamonds";
}
//This loop will do the task-2 of displaying the unshuffled deck
for(int j=0;j<52;j++){
System.out.print(deckArray[j]);
if(j!=51){
System.out.print(", ");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.