Question: Fill in the shuffle() method by using the algorithm in Section 13.2.(Think Java 2) (Note the helper methods randomInt() and swapCards() are already written in

Fill in the shuffle() method by using the algorithm in Section 13.2.(Think Java 2) (Note the helper methods randomInt() and swapCards()are already written in the class Deck.)

Start a project Program13Deck in IntelliJ. Then put the three new JavaClass files into the project from the Card, Deck and Program13Deck below. They are from the textbook and they should run.

The programming assignment is to write the method shuffle(). Follow the pseudocode on page 191/219 pdf.

Then write the main() so it does the following.

  • deck.print(); // new deck
  • deck.shuffle();
  • deck.print(); // random deck

Run the program several times in a row to make sure the shuffle() is working. For example, the last card should not always be the same.

Attach the Program13Deck.java and the Deck.java -and- copyAndPaste the output.

Card.java

/** * A standard playing card. */ public class Card { public static final String[] RANKS = { null, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; public static final String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades"}; private final int rank; private final int suit; /** * Constructs a card of the given rank and suit. */ public Card(int rank, int suit) { this.rank = rank; this.suit = suit; } /** * Returns a negative integer if this card comes before * the given card, zero if the two cards are equal, or * a positive integer if this card comes after the card. */ public int compareTo(Card that) { if (this.suit < that.suit) { return -1; } if (this.suit > that.suit) { return 1; } if (this.rank < that.rank) { return -1; } if (this.rank > that.rank) { return 1; } return 0; } /** * Returns true if the given card has the same * rank AND same suit; otherwise returns false. */ public boolean equals(Card that) { return this.rank == that.rank && this.suit == that.suit; } /** * Gets the card's rank. */ public int getRank() { return this.rank;

} /** * Gets the card's suit. */ public int getSuit() { return this.suit; } /** * Returns the card's index in a sorted deck of 52 cards. */ public int position() { return this.suit * 13 + this.rank - 1; } /** * Returns a string representation of the card. */ public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; } }

Deck.java

import java.util.Random; public class Deck { private Card[] cards; // instance variable public Deck() { // default constructor because no parameters this.cards = new Card[52]; int index = 0; for (int suit = 0; suit <= 3; suit++) { for (int rank = 1; rank <= 13; rank++) { this.cards[index] = new Card(rank, suit); index++; } } } public Deck(int n) { this.cards = new Card[n]; // an array of nulls } public Card[] getCards() { return this.cards; } //public void shuffle() {} public void shuffle() { for( int i = 0; i < cards.length - 1; i++ ) { // cards.length = 52 // choose a random number between i and length - 1 // swap the ith card and the randomly-chosen card } } private int randomInt( int low, int high ) { Random r = new Random(); return low + r.nextInt( high - low + 1 ); } private void swapCards(int i, int j) { Card temp = cards[i]; cards[i] = cards[j]; cards[j] = temp; } public void print() { Card.printDeck(cards); } }

Program13Deck.java

public class Program13Deck { public static void main(String[] args) { Deck deck = new Deck(); System.out.println( deck ); } }

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 Programming Questions!