You are to write a program made up of 3 separate classes wit
You are to write a program made up of 3 separate classes within the same package: The main class name cardtest.java, which will serve as the driver class to test the others. A Card (Card.java) class made up of 2 variables - an int for rank and a String for suit, will have two constructors - 1 no parameter that will set A Card to Ace of Spades and one that accepts 2 parameters to set a Card, will have 2 getters and 2 setters, will have a print method to print a Card. A Deck (Deck.java) class with one ArrayList variablwe that will accept a Card data element, one no parameter constructor that will creat a Deck of 52 Cards, a shuffle method to randomly shuffle the Deck of Cards. The card class will allow you to create a card. The deck class allow you to create a deck of cards which are placed in the ArrayList. A deck of cards is made up of 4 suits (clubs, heart, spades and diamonds) with each suit made up of 13 cards (2 through 10 and Jack, Queen, King and Ace). Place the deck of cards in a ArrayList in sorted order with all 13 hearts first, followed by the 13 clubs, then the 13 spades and finally the 13 diamonds. Display the contents of the ArrayList on the screen. Now take this deck of cards and randomly shuffle it (for at least 20 times - prompt the user for this number) and now display the randomly shuffled deck of cards.
Solution
SOURCE CODE:
Card.java
package card;
public class Card {
public Card() {
this.suite = \"Spades\";
this.rank = 1;
}
public Card(String suite, int rank) {
this.suite = suite;
this.rank = rank;
}
private String suite;
private int rank;
private String rankNames[]={\"\",\"Ace\",\"Two\",\"Three\",\"Four\",\"Five\",\"Six\",\"Seven\",\"Eight\",\"Nine\",\"Ten\",\"Jack\",\"Queen\",\"King\"};
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuite() {
return suite;
}
public void setSuite(String suite) {
this.suite = suite;
}
@Override
public String toString() {
return rankNames[rank]+\" of \"+suite;
}
}
Deck.java
package card;
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
ArrayList<Card> deck = new ArrayList<Card>();
public Deck() {
for(int i=2;i<=13;i++)
deck.add(new Card(\"Hearts\",i));
deck.add(new Card(\"Hearts\",1));
for(int i=2;i<=13;i++)
deck.add(new Card(\"Clubs\",i));
deck.add(new Card(\"Clubs\",1));
for(int i=2;i<=13;i++)
deck.add(new Card(\"Spades\",i));
deck.add(new Card(\"Spades\",1));
for(int i=2;i<=13;i++)
deck.add(new Card(\"Diamonds\",i));
deck.add(new Card(\"Diamonds\",1));
}
@Override
public String toString() {
String res=\"\";
for(int i=0;i<52;i++)
res=res+deck.get(i).toString()+\"\ \";
return res;
}
public void shuffleDeck(int times)
{
while((times--)>0)
Collections.shuffle(deck);
}
}
CardTest.java
package card;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CardTest {
public static void main(String[] args) throws IOException, InterruptedException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Deck d=new Deck();
System.out.println(\"Deck of cards before shuffling:\ \"+d.toString());
System.out.println(\"Enter the number of times you want to shuffle: \");
int t=Integer.parseInt(br.readLine());
d.shuffleDeck(t);
System.out.println(\"Deck of cards after shuffling \"+t+\" times:\ \"+d.toString());
}
}
OUTPUT:
Deck of cards before shuffling:
Two of Hearts
Three of Hearts
Four of Hearts
Five of Hearts
Six of Hearts
Seven of Hearts
Eight of Hearts
Nine of Hearts
Ten of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
Two of Clubs
Three of Clubs
Four of Clubs
Five of Clubs
Six of Clubs
Seven of Clubs
Eight of Clubs
Nine of Clubs
Ten of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
Two of Spades
Three of Spades
Four of Spades
Five of Spades
Six of Spades
Seven of Spades
Eight of Spades
Nine of Spades
Ten of Spades
Jack of Spades
Queen of Spades
King of Spades
Ace of Spades
Two of Diamonds
Three of Diamonds
Four of Diamonds
Five of Diamonds
Six of Diamonds
Seven of Diamonds
Eight of Diamonds
Nine of Diamonds
Ten of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
Enter the number of times you want to shuffle:
20
Deck of cards after shuffling 20 times:
Ace of Hearts
Five of Hearts
Eight of Diamonds
Queen of Hearts
Queen of Clubs
Jack of Hearts
Nine of Diamonds
Eight of Clubs
Ace of Spades
Jack of Spades
Queen of Spades
Six of Hearts
Eight of Spades
Two of Spades
Ten of Spades
Ten of Clubs
Two of Diamonds
King of Clubs
King of Spades
Four of Clubs
Four of Hearts
Nine of Spades
Three of Hearts
Five of Clubs
Five of Diamonds
Nine of Hearts
Seven of Clubs
Seven of Diamonds
Jack of Diamonds
Two of Hearts
Four of Diamonds
Six of Spades
Ten of Hearts
Jack of Clubs
Eight of Hearts
Seven of Spades
Three of Diamonds
Four of Spades
King of Hearts
Ace of Diamonds
Three of Spades
Ace of Clubs
Nine of Clubs
King of Diamonds
Five of Spades
Two of Clubs
Three of Clubs
Six of Clubs
Six of Diamonds
Seven of Hearts
Ten of Diamonds
Queen of Diamonds



