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


package game;
import java.util.*;

public class CardTest {
   public static void main(String[] args)
   {
Deck cd = new Deck();
cd.shuffle(null);
   }
}

class Card
{
int rank;
String suit;
public static String[] suits = { \"hearts\", \"spades\", \"diamonds\", \"clubs\" };
public static String[] ranks = { \"ACE(1)\" , \"TWO(2)\", \"THREE(3)\", \"FOUR(4)\", \"FIVE(5)\" , \"SIX(6)\", \"SEVEN(7)\", \"EIGHT(8)\", \"NINE(9)\", \"TEN (10)\", \"JACK(11)\", \"QUEEN (12)\", \"KING(13)\"};
  
public Card() { //setting card to ACE of SPADE
rank=Integer.parseInt(ranks[0]);
suit=suits[1];
}
  
Card(int r, String s){
rank = r;
suit = s;
}

public int getRank() {
return rank;
}

public void setRank(int rank) {
this.rank = rank;
}

public String getSuit() {
return suit;
}

public void setSuit(String suit) {
this.suit = suit;
}
}

class Deck
{
public ArrayList<Card> variablwe;
  
Deck()
{
variablwe= new ArrayList<Card>();
for(int i=0;i<Card.suits.length;i++)
{
String s=Card.suits[i];
for(int j=0;j<Card.ranks.length;j++)
{
int r=Integer.parseInt(Card.ranks[j]);
variablwe.add(r,s);
}
}
}
  
public void shuffle(ArrayList<Card> c){
if(c==null){
Collections.shuffle(variablwe);
}else{
Collections.shuffle(c);
}
}

public void print(){
for(Card card: variablwe){
System.out.println(\"Card: \"+card.getRank()+\" Suit: \"+card.getSuit());
}
}
}

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 tes
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 tes

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site