Question: HELP ME!! This is Java. I want to get output like this. A brand new, fresh deck 2 3 4 5 6 7 8 9

HELP ME!! This is Java.

I want to get output like this.

A brand new, fresh deck

2 3 4 5 6 7 8 9 10 J Q K A

2 3 4 5 6 7 8 9 10 J Q K A

2 3 4 5 6 7 8 9 10 J Q K A

2 3 4 5 6 7 8 9 10 J Q K A

However, I got this.

A brand new, fresh deck 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

How should I fix these?

This is my code.

public class DealHands { /** * Create new deck object */ public static void main(String[] args) { //create new deck Deck bigDeck = new Deck(); System.out.println("A brand new, fresh deck"); System.out.println(bigDeck.toString());

}

public class Deck { //constant public static final int SUIT_TOTAL = 4; public static final int RANK_HIGH = 14; public static final int RANK_LOW = 2; public static final int CARDS_PER_LINE = 13; // instance variables private ArrayList cards; /** * Constructor for objects of class Deck */ public Deck() { // initialise instance variables cards = new ArrayList<>(); //create a brand new, fresh deck of 52 cards in stored order int rank = 2; int suit = 0; for(int count1 = 1; count1 <= SUIT_TOTAL; ++count1){ for(int count2= RANK_LOW; count2 <= RANK_HIGH; ++ count2){ cards.add(new Card(rank,suit)); ++ rank; } ++ suit; } }

public String toString() { String out = ""; int count = 0; for(Card card: cards){ out += card.toString() + " "; ++count; if (count % CARDS_PER_LINE == 0) out += ' '; } return out; }

}

public class Card { //constants public static final int LOW_RANK = 2; public static final int J = 11; public static final int Q = 12; public static final int K = 13; public static final int A = 14; public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3; // instance variables private final int rank; private final int suit; /** * Constructor for objects of class Card */ public Card(int rankln, int suitln) { // initialise instance variables rank = rankln; suit = suitln; } /** * String method */ public String toString() { String out = ""; out += String.format("%2d",rank); // now add single, special char for suit // clubs are black if (suit == CLUBS) out += '\u2663'; // Unicode char for black club //diamonds are red else if (suit == DIAMONDS) out += '\u2662'; // white diamond. Closest to red // hearts are red else if (suit == HEARTS) out += '\u2661'; // white heart // spades are black else if (suit == SPADES) out += '\u2660'; //black spade return String.format("%s",out); } }

Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!