Java programm : Write a class Card that will represent a card in a standard deck
ID: 3783888 • Letter: J
Question
Java programm : Write a class Card that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank ( A, K, Q, J, 10, 9, 8, 7, 6, 5, 6, 7, 2) of each card. Provide useful methods for class Card. Then write class TestCards to make an array of Cards
Card deck[] = new Card[52];
Write a method that will perform a perfect shuffle on a deck of cards represented using the class from part a. In a perfect shuffle, the deck is broken exactly in half and rearranged so that the first card is followed by the 27th card, followed by the second card, followed by the 28th card, and so on. Print nice out put
Explanation / Answer
Create file cards.java and paste below code:
public class cards
{
public String myarray[] = new String[52] ;
public cards()
{
String[] a1 = { "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2" };
String[] a2 = { "clubs", "diamonds" , "hearts" ,"spades" };
int i = 0;
int index = 0;
while( i < 4)
{
int j = 0;
while(j < 13)
{
this.myarray[index] = a2[i] + " " + a1[j];
index = index + 1;
j = j+1;
}
i = i+1;
}
}
public void shuffle()
{
String myarraynew[] = new String[52];
int x = 0;
while(x < 52)
{
myarraynew[x] = this.myarray[x];
x = x+ 1;
}
int i = 1;
int j = 27;
int index = 0;
while(i<=26)
{
this.myarray[index] = myarraynew[i-1];
this.myarray[index+1] = myarraynew[j-1];
index = index + 2;
i = i+1;
j = j+1;
}
}
}
create file TestCards.java and paste below code in it. Please note that keep both files in same folder, and just run TestCards.java file, this file creates the object of cards class.
public class TestCards
{
public static void main(String[] args)
{
cards deck1 =new cards();
System.out.println("Initial Sequence of cards is as follows");
int i = 0;
while(i<52)
{
System.out.println(deck1.myarray[i] );
i = i+1;
}
deck1.shuffle();
System.out.println("After shuffling, Sequence of cards is as follows");
i = 0;
while(i<52)
{
System.out.println(deck1.myarray[i] );
i = i+1;
}
}
}
Sample output:
Initial Sequence of cards is as follows
clubs A
clubs K
clubs Q
clubs J
clubs 10
clubs 9
clubs 8
clubs 7
clubs 6
clubs 5
clubs 4
clubs 3
clubs 2
diamonds A
diamonds K
diamonds Q
diamonds J
diamonds 10
diamonds 9
diamonds 8
diamonds 7
diamonds 6
diamonds 5
diamonds 4
diamonds 3
diamonds 2
hearts A
hearts K
hearts Q
hearts J
hearts 10
hearts 9
hearts 8
hearts 7
hearts 6
hearts 5
hearts 4
hearts 3
hearts 2
spades A
spades K
spades Q
spades J
spades 10
spades 9
spades 8
spades 7
spades 6
spades 5
spades 4
spades 3
spades 2
After shuffling, Sequence of cards is as follows
clubs A
hearts A
clubs K
hearts K
clubs Q
hearts Q
clubs J
hearts J
clubs 10
hearts 10
clubs 9
hearts 9
clubs 8
hearts 8
clubs 7
hearts 7
clubs 6
hearts 6
clubs 5
hearts 5
clubs 4
hearts 4
clubs 3
hearts 3
clubs 2
hearts 2
diamonds A
spades A
diamonds K
spades K
diamonds Q
spades Q
diamonds J
spades J
diamonds 10
spades 10
diamonds 9
spades 9
diamonds 8
spades 8
diamonds 7
spades 7
diamonds 6
spades 6
diamonds 5
spades 5
diamonds 4
spades 4
diamonds 3
spades 3
diamonds 2
spades 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.