Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Turn the code at the bottom into an object-oriented class ----------------------

ID: 3757524 • Letter: T

Question

Turn the code at the bottom into an object-oriented class

-----------------------------------------------------

What to implement

• public CardGame(totalPlayers: int)

o Initialize your instance variables. A deck holds 52 cards. The total number of players allowed must be between 2 and 8. If a number passed in is not in range, the default size is 2. cardsDealt is initialized to 0. hasDealtCards is false when the deck has just been shuffled, and true when the cards get dealt. This ensures a deck is only allowed to deal cards after it is shuffled.

• getTotalCardsDealt()

o returns the total number of cards dealt

• resetDeck()

o Collect all cards, set cardsDealt to 0, and reset hasDealtCards to false indicating the cards can be dealt again.

• dealHands()

o Deal out cards to each player in the game. You will need to call dealCard. Now you will also need to check hasDealtCards. If the deck has already dealt cards and this method is called, you must display an error message to the console saying the cards have been dealt already and not deal out any more cards.

Example of what driver could look like.

CardGame game1 = new CardGame(3) //create cardgame with 3 players

game1.resetDeck(); //make it so all cards available

game1.dealHands(); //deal out hands to each player

game1.dealHands(); //can not do this, will print error message

game1.resetDeck(); //”collect” all cards

game1.dealHands(); //can now re-deal again

• dealCard()

o Deal one card from the deck. Take note of the access permissions of this method.

• convertCard(card: int)

o Return a string representation of a card. Take note of the access permissions of this method.

• displayHands()

o Display all players hands.

• addPlayers(playersToAdd: int)

o A new method you must add. Add the specified number of players to the deck. Make sure the number passed in does not exceed the max number of players allowed and it does not reduce the total number of current players. If either of these two tests fail, do not copy anything and display an error message to the screen. You will need to create a new 2D String array, copy all contents from the current array to the new array in the same order, then deal out the cards to the new players. Don’t forget to reassign your instance variable. This method should not be affected by the hasDealtCards boolean variable and if the cards have already been shown that is ok. An example is below, note how player 1,2 and 3’s hands do not change when 3 more players are added.

ex.

Game One Players:

Player 1 hand: 7S 9C 2D 4S 3H

Player 2 hand: 8C 4H JS 4C JD

Player 3 hand: KD AS 7D AD 8H

Game One Players has added 3 players.

Player 1 hand: 7S 9C 2D 4S 3H

Player 2 hand: 8C 4H JS 4C JD

Player 3 hand: KD AS 7D AD 8H

Player 4 hand: 7H 5S 9H 7C 4D

Player 5 hand: 3S 8D 2C 10D AH

Player 6 hand: 2S JC KS AC 2H

---------------------------------------------------------

Code to change

import java.util.Scanner;

class CardGame {

   public static final String[] cardNumbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

   public static final String[] cardSuits = {"D","H","C","S"};

   public static void main(String[] args) {

       Scanner scr = new Scanner(System.in);

       boolean[] deck = new boolean[52];

       String[][] players;

       boolean playAgain;

       do{

           players = playerPrompt(scr);

           resetDeck(deck);

           dealHands(players, deck);

           displayHands(players);

           playAgain = playerRePrompt(scr);

       } while (playAgain);

       System.out.println("Thank you, goodbye");

       scr.close();

   }

   public static String[][] playerPrompt (Scanner scr)

   {

       System.out.println("How many people are playing? ");

       int totalPlayers = scr.nextInt();

       while(totalPlayers<2||totalPlayers>8){

           System.out.println("Please re-enter a number between 2 and 8. How many people are playing? ");

           totalPlayers = scr.nextInt();

       }

       return new String[totalPlayers][5];

   }

   public static boolean playerRePrompt (Scanner scr)

   {

       System.out.println("Would you like to deal again? (Type 'no' to quit.)");

       String input = scr.next();

       if(input.equals("no"))

           return false;

       return true;

   }

   static void resetDeck(boolean[] deck)

   {

       for(int i=0;i<52;i++)

           deck[i] = true;

   }

   public static void dealHands(String[][] players, boolean[] deck)

   {

       for (int i=0; i<players.length;i++)

       {

           for (int j=0; j<5; j++)

           {

               players[i][j] = dealCard(deck);

           }

       }

   }

   static String dealCard(boolean[] deck)

   {

       int rand;

       do{

           rand = (int)(Math.random()*52);

       }while(deck[rand]==false);

       deck[rand] = false;

       return convertCard(rand);

   }

   public static String convertCard(int card)

   {

       String numbers = cardNumbers[card%13];

       String suits = cardSuits[card/13];

       return numbers + suits;

   }

   public static void displayHands(String[][] players)

   {

       for(int i = 0; i<players.length; i++)

       {

           System.out.print("Player " + i + " hand: ");

           for(int j = 0; j<5; j++)

           {

               System.out.print(players[i][j]+" ");

           }

           System.out.print(" ");

       }

   }

}

Explanation / Answer

CardGameDriver.java


public class CardGameDriver {

   public static void main(String[] args) {

       CardGame g1 = new CardGame(4);
       CardGame g2 = new CardGame(8);
       CardGame g3 = new CardGame(10);
      
       g1.displayHands(players);
      
   }

}


CardGame.java

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class CardGame {

   private static final String[] cardNumbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
   private static final String[] cardSuits = {"D","H","C","S"};
   private static final int defaultPlayers = 2;
  
   private boolean deck[];
   private String[][] players;
   private int cardsDealt;
   private boolean hasDealtCards;
  
   public CardGame (int totalPlayers) {
      
       totalPlayers = totalPlayers <= 8 && totalPlayers >= 2 ? totalPlayers : defaultPlayers;
       this.players = new String [totalPlayers][5];
       this.deck = new boolean[52];
       this.resetDeck(this.deck);
   }
  
   public int getTotalCardsDealt() {
       return cardsDealt;
   }
  
  
   private void setHasDealtCards(boolean hasDealtCards) {
       this.hasDealtCards = hasDealtCards;
   }
  
   public boolean getHasDealtCards() {
       return hasDealtCards;
   }
  
   private void setCardsDealt(int cardsDealt) {
       this.cardsDealt = cardsDealt;
   }


   public void resetDeck(boolean[] deck){  
       Arrays.fill(deck, Boolean.TRUE);
       this.setHasDealtCards(false);
       this.setCardsDealt(0);
      
   }
  
   public void dealHands(String[][] players, boolean[] deck){
      
       if (!getHasDealtCards()) {
           for(int i = 0; i<players.length; i++){
               for(int k = 0; k<players[i].length; k++){
                   players[i][k] = dealCard(deck);  
               }
           }  
       }  
       else {
           System.out.println("Error - The cards have already been dealt. Can't deal more cards");
       }
          
   }

  
   private String dealCard(boolean[] deck){
      
        while(true){
            int tempCard = ThreadLocalRandom.current().nextInt(0, 52);
            if(deck[tempCard]) {
                deck[tempCard] = false;
                return convertCard(tempCard);
            }
        }
      
    }

  
   public String convertCard(int card){
      
       return cardSuits[card%4] + cardNumbers[card%13];
   }


   public void displayHands(String[][] players){

       String output = "";
       for(int i = 0; i<players.length; i++){
           output += String.format(" Player %s hand: ", i+1);
            for( String card : players[i]) {
                output += card + " ";
            }
            output += " ";
       }
       System.out.print(output);
   }
  
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote