Need to crate a java card program I beleive Itll have three
Need to crate a java card program. I beleive It\'ll have three classes: CardApp, Deck, and Shuffle if im not mistaken. I\'ll include what I came up with below. Any tweaks or entierly new code to fit the instructions below would be great
Instructions:
Create an array for the card suits
Create an array to hold all 52 cards
Write a method to load the card array
Write a method to print the card array
Insure that the array is loaded properly
Write a method to shuffle the cards
Test to insure that the cards were shuffled
Create an array to hold 4 hands of 5 cards
Write a method to deal the cards
Write a method to display one hand per line
Code:
CardDeckApp:
public class CardDeckApp
{
public int nRank;
public int maxRank = 13;
public int nSuit;
public int maxSuit = 4;
public String[] suits = new String[maxSuit ];
{
suits[0] = \"Clubs\";
suits[1] = \"Diamonds\";
suits[2] = \"Spades\";
suits[3] = \"Hearts\";
}
public String[] ranks = new String[maxRank];
{
ranks[0] = \"Ace\";
ranks[1] = \"Two\";
ranks[2] = \"Three\";
ranks[3] = \"Four\";
ranks[4] = \"Five\";
ranks[5] = \"Six\";
ranks[6] = \"Seven\";
ranks[7] = \"Eight\";
ranks[8] = \"Nine\";
ranks[9] = \"Ten\";
ranks[10] = \"Jack\";
ranks[11] = \"Queen\";
ranks[12] = \"King\";
}
public String suit = suits[nSuit];
public String rank = ranks[nRank];
public CardDeckApp(int x,int y){ this.nRank = x; this.nSuit = y; this.suit =
suits[nSuit]; this.rank = ranks[nRank]; }
public String whatCard(){
return rank + \" of \" + suit;
}
}
Deck:
public class Deck {
public static int nRanks = 13;
public static int nSuits = 4;
public static int nCard = nRanks * nSuits;
CardDeckApp[] deck = new CardDeckApp[nCard ];
int h = 0;
public Deck() {
while(h < 52){
for(int i = 0; i < nRanks; i++){
for(int j = 0; j < nSuits; j++){
deck[h] = new CardDeckApp(i,j);
h++;
}
}
}
}
public CardDeckApp getCard(int x){
return deck[x];
}
}
Shuffle:
public class Shuffle {
public static void main(String[] args){
Deck newDeck = new Deck();
for(int i = 0; i < Deck.nCard; i++){
System.out.println(newDeck.getCard(i).whatCard());
}
}
}
Solution
Solution:
The modified code.
Your code is modified to implement the given methods.
Java Code
CardDeckApp.Java
package shuffle;
public class CardDeckApp
{
public int nRank;
public int maxRank = 13;
public int nSuit;
public int maxSuit = 4;
public String[] suits = new String[maxSuit ];
{
suits[0] = \"Clubs\";
suits[1] = \"Diamonds\";
suits[2] = \"Spades\";
suits[3] = \"Hearts\";
}
public String[] ranks = new String[maxRank];
{
ranks[0] = \"Ace\";
ranks[1] = \"Two\";
ranks[2] = \"Three\";
ranks[3] = \"Four\";
ranks[4] = \"Five\";
ranks[5] = \"Six\";
ranks[6] = \"Seven\";
ranks[7] = \"Eight\";
ranks[8] = \"Nine\";
ranks[9] = \"Ten\";
ranks[10] = \"Jack\";
ranks[11] = \"Queen\";
ranks[12] = \"King\";
}
public String suit = suits[nSuit];
public String rank = ranks[nRank];
public CardDeckApp(int x,int y){ this.nRank = x; this.nSuit = y; this.suit =
suits[nSuit]; this.rank = ranks[nRank]; }
public String whatCard(){
return rank + \" of \" + suit;
}
}
Deck.Java
package shuffle;
public class Deck {
public static int nRanks = 13;
public static int nSuits = 4;
public static int nCard = nRanks * nSuits;
CardDeckApp[] deck = new CardDeckApp[nCard ];
int h = 0;
public Deck() {
while(h < 52){
for(int i = 0; i < nRanks; i++){
for(int j = 0; j < nSuits; j++){
deck[h] = new CardDeckApp(i,j);
h++;
}
}
}
}
public CardDeckApp getCard(int x){
return deck[x];
}
}
Shuffle.java
package shuffle;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class Shuffle {
//method to print the card array
public void printCard(){
Deck newDeck = new Deck();
for(int i = 0; i < Deck.nCard; i++){
System.out.println(newDeck.getCard(i).whatCard());
}
}
//method to shuffle the array
public void Shuffl(){
Deck newDeck = new Deck();
int[] arr=new int[52];
for(int vi=0;vi<52;vi++)
{
arr[vi]=vi;
}
Random rand = ThreadLocalRandom.current();
for(int vi=51;vi>=0;vi--)
{
int indx=rand.nextInt(vi+1);
int va=arr[indx];
arr[indx]=arr[vi];
arr[vi]=va;
}
System.out.println(\"\ THE SHUFFLED DECK\ \");
for(int i = 0; i < 52; i++){
System.out.println(newDeck.getCard(arr[i]).whatCard());
}
}
//method to deal the card
public String[][] dealcard()
{
Deck newDeck = new Deck();
int[] arr=new int[52];
String deal[][]=new String [4][5];
for(int vi=0;vi<52;vi++)
{
arr[vi]=vi;
}
Random rand = ThreadLocalRandom.current();
for(int vi=51;vi>=0;vi--)
{
int indx=rand.nextInt(vi+1);
int va=arr[indx];
arr[indx]=arr[vi];
arr[vi]=va;
}
int cou=0;
for(int vi=0;vi<4;vi++)
{
for(int vj=0;vj<5;vj++)
{
deal[vi][vj]=newDeck.getCard(arr[cou]).whatCard();
cou++;
}
}
return deal;
}
//method to show the deal
public void showdeal(String arr[][])
{
for(int vk=0;vk<4;vk++)
{
for(int vl=0;vl<5;vl++)
{
System.out.print(arr[vk][vl]+\", \");
}
System.out.println();
}
}
public static void main(String[] args){
Shuffle shuff= new Shuffle();
System.out.println(\"\ The Array of Cards is\ \");
shuff.printCard();
shuff.Shuffl();
//array to hold 4 hands of 5 cards.
String[][] hands=new String[4][5];
hands=shuff.dealcard();
System.out.println(\"\ The Dealed hands are\ \");
shuff.showdeal(hands);
}
}
Sample Output run:
run:
The Array of Cards is
Ace of Clubs
Ace of Diamonds
Ace of Spades
Ace of Hearts
Two of Clubs
Two of Diamonds
Two of Spades
Two of Hearts
Three of Clubs
Three of Diamonds
Three of Spades
Three of Hearts
Four of Clubs
Four of Diamonds
Four of Spades
Four of Hearts
Five of Clubs
Five of Diamonds
Five of Spades
Five of Hearts
Six of Clubs
Six of Diamonds
Six of Spades
Six of Hearts
Seven of Clubs
Seven of Diamonds
Seven of Spades
Seven of Hearts
Eight of Clubs
Eight of Diamonds
Eight of Spades
Eight of Hearts
Nine of Clubs
Nine of Diamonds
Nine of Spades
Nine of Hearts
Ten of Clubs
Ten of Diamonds
Ten of Spades
Ten of Hearts
Jack of Clubs
Jack of Diamonds
Jack of Spades
Jack of Hearts
Queen of Clubs
Queen of Diamonds
Queen of Spades
Queen of Hearts
King of Clubs
King of Diamonds
King of Spades
King of Hearts
THE SHUFFLED DECK
Ten of Clubs
Ten of Diamonds
Queen of Clubs
Ace of Spades
Six of Spades
Six of Clubs
Ten of Spades
Nine of Spades
Six of Diamonds
Five of Spades
Eight of Diamonds
Seven of Diamonds
Two of Clubs
Jack of Clubs
Four of Spades
Two of Hearts
Two of Diamonds
Queen of Spades
Five of Diamonds
King of Spades
Queen of Diamonds
Jack of Hearts
Three of Diamonds
Two of Spades
Five of Hearts
Three of Clubs
King of Clubs
Ace of Diamonds
Ace of Hearts
Ten of Hearts
Nine of Clubs
Ace of Clubs
Seven of Hearts
Three of Hearts
Four of Hearts
Eight of Clubs
Six of Hearts
Nine of Hearts
King of Hearts
Eight of Hearts
King of Diamonds
Nine of Diamonds
Four of Clubs
Seven of Clubs
Four of Diamonds
Seven of Spades
Eight of Spades
Jack of Spades
Jack of Diamonds
Queen of Hearts
Three of Spades
Five of Clubs
The Dealed hands are
Ten of Hearts, Nine of Hearts, Nine of Diamonds, King of Clubs, Six of Spades,
Four of Hearts, Jack of Clubs, Ace of Spades, Seven of Hearts, Queen of Diamonds,
Two of Hearts, Ace of Clubs, Six of Clubs, Four of Diamonds, Queen of Spades,
King of Spades, Nine of Clubs, Three of Hearts, Jack of Spades, Three of Spades,
BUILD SUCCESSFUL (total time: 0 seconds)






