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

Java Programming Introduction to Programming Introduction Throughout much of the

ID: 3840389 • Letter: J

Question

Java Programming

Introduction to Programming Introduction Throughout much of the semester we have been working with the cards program and have developed it in multiple forms. We shall continue this development process by refactoring the last instance of the program so as to use multip write classes that model a Card, a Deck of Cards, and a Hand which holds Cards. Finally, you w You wi Write a main class Poker, that will create a Deck of Cards, shuffle the Deck, and then deal out Cards to multiple Hands For the development process, we shall work from the bottom up. Please read and follow the instructions below to write the specified four classes. Please submit the four Java source code files with the names 1. Card. java 2 Deck. java 3. Hand. java 4. Poker .java to the Blackboard Assignment, class Card [20] This class represents a single card. It maintains the card's card number. It can provide the card's suit and rank, and a human readable version of the card 1. (4] Define two symbolic constants as arrays of String elements named SUITS and RANKS. These arrays will be identical to the arrays we used in the previous version of the program. Based on thes arrays define additional symbolic constants names NSUITS, NRANKS, and NCARDS (the last being the product of the first two) 2. [3] There will be one instance field named cardNum of type int. Declare this field 3. [3]Define a single constructor that accepts a card number and assigns it to the instance field 4. [4] Define two accessor methods similar to the ones previously written in the older version of the program: a. int rank b. int suit 5. 4] Define the method String toString() that will return a String of the form rank of suit'' 6. [2] You may write a local main() method that you can use to unit test the correctness of this class class Deck 30 This class represents a deck of cards (we assume a normal card deck without any jokers). It maintains an array of cards and keeps track of where in the deck we are while cards are being dealt. It provides for constructing a new, ordered, deck of cards, shuffling the deck, and producing a String version of the deck for display purposes (most likely for debugging) 1. [4] Import classes that will be needed by this class a. java.util.Random b java. util .Arrays c java .util. NoSuchElementException mport the static fields from the Card class so that we do not need to prefix their names with the class name, Card (e.g., Card.NCARDS can be referred to by just NCARDS) a. import static exam3.Card 2. [3] Declare 3 instance fields a. Card[] deck the array to hold the cards in the dec b. int nextCardthe index in the deck which will be the next card to deal out C. Random rand a random number generator used by shuffle (this is an instance field since we only need one generator that may be invoked multiple times if we shuffle the deck multiple times) 3. [7] Define a no-argument constructor. a. [2] Instantiate the deck array with NCARDS elements. b 21 Set each element of the deck array with the Cardelement whose cardNum matches its index in the deck c [2] Set nextCard to 0 to indicate that we will start dealing from the top of the deck. d. [1] Instantiate a new random number generator.

Explanation / Answer

/***********************
* Assignment 4
* Andreas Hadjigeorgiou
* ahh2131
* Game class
*/

import java.util.Arrays;
import java.util.Scanner;

public class Game
{

private final int HAND_SIZE = 5;
   private int again = 1;
  
   // instantiate Deck and Player
   Scanner scan = new Scanner(System.in);
   Deck deck = new Deck();
   Player player = new Player();
   Card[] hand;
  
  
  
   // plays the game
   public void play()
   {
       while (again == 1)
       {
           // fill deck
           deck.fillDeck();
          
           // shuffle
           deck.shuffle();
          
           // player draws
           hand = player.draw(deck);
          
           // sort hand      
           Arrays.sort(hand);
          
           // player redraws
           this.checkHand();
           hand = this.redraw();
          
           // display hand again
           // this.makeHand(); //<--- TA ! un-comment this and change makeHand()
           this.checkHand();
          
           // sort hand      
           Arrays.sort(hand);
          
           // evaluate the hand
           this.evaluate();
      
           // play again?
           this.again();
       }
       System.out.println("Thanks for playing! =]");
   }
  
   // makes a hand (for TA; testing purposes)
   public void makeHand()
   {
       hand[0].rank = 1;
       hand[1].rank = 2;
       hand[2].rank = 3;
       hand[3].rank = 4;
       hand[4].rank = 5;
      
       hand[0].suit = 1;
       hand[1].suit = 1;
       hand[2].suit = 1;
       hand[3].suit = 1;
       hand[4].suit = 1;
   }
  
   // tells player cards in hand
   public void checkHand()
   {
       for (int handCounter = 0; handCounter < HAND_SIZE; handCounter++)
       {
           this.display(hand[handCounter]);
       }
   }
  
   // asks if player wants to redraw
   public Card[] redraw()
   {
       for (int counter = 0; counter < 5; counter++)
       {
           System.out.print("Redraw card " + (counter + 1) + "?" +
                   " (1 for yes, 0 for no)");
           int answer = scan.nextInt();
           if (answer == 1)
           {
               hand[counter] = player.redraw(counter, deck);
           }
       }
       deck.refreshDeckPosition();
       return hand;
   }
  
  
   // evaluates the hand
   public void evaluate()
   {
       if (this.royalFlush() == 1)
       {
           System.out.println("You have a royal flush!");
       }
       else if (this.straightFlush() == 1)
       {
           System.out.println("You have a straight flush!");
       }
       else if (this.fourOfaKind() == 1)
       {
           System.out.println("You have four of a kind!");
       }
       else if (this.fullHouse() == 1)
       {
           System.out.println("You have a full house!");
       }
       else if (this.flush() == 1)
       {
           System.out.println("You have a flush!");
       }
       else if (this.straight() == 1)
       {
           System.out.println("You have a straight!");
       }
       else if (this.triple() == 1)
       {
           System.out.println("You have a triple!");
       }
       else if (this.twoPairs() == 1)
       {
           System.out.println("You have two pairs!");
       }
       else if (this.pair() == 1)
       {
           System.out.println("You have a pair!");
       }
       else
       {
           int highCard = this.highCard();
           System.out.println("Your highest card is " + highCard);
       }
   }
  
   // checks for a royal flush
   public int royalFlush()
   {
       if (hand[0].rank == 1 && hand[1].rank == 10 && hand[2].rank == 11 &&
               hand[3].rank == 12 && hand[4].rank == 13)
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
  
   // checks for a straight flush
   public int straightFlush()
   {
       for (int counter = 1; counter < 5; counter++)
       {
           if (hand[0].suit != hand[counter].suit)
           {
               return 0;
           }
       }
       for (int counter2 = 1; counter2 < 5; counter2++)
       {
           if (hand[counter2 - 1].rank != (hand[counter2].rank - 1))
           {
               return 0;
           }
              
       }
       return 1;
      
   }
  
   // checks for four of a kind
   public int fourOfaKind()
   {
       if (hand[0].rank != hand[3].rank && hand[1].rank != hand[4].rank)
       {
           return 0;
       }
       else
       {
           return 1;
       }
   }
  
   // checks for full house
   public int fullHouse()
   {
       int comparison = 0;
       for (int counter = 1; counter < 5; counter++)
       {
           if (hand[counter - 1].rank == hand[counter].rank)
           {
               comparison++;
           }
       }
       if (comparison == 3)
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
  
   // checks for flush
   public int flush()
   {
       for (int counter = 1; counter < 5; counter++)
       {
           if (hand[0].suit != hand[counter].suit)
           {
               return 0;
           }
       }
       return 1;
   }
  
   // check for straight
   public int straight()
   {
       for (int counter2 = 1; counter2 < 5; counter2++)
       {
           if (hand[counter2 - 1].rank != (hand[counter2].rank - 1))
           {
               return 0;
           }
              
       }
       return 1;
   }
  
   // checks for triple
   public int triple()
   {
       if (hand[0].rank == hand[2].rank || hand[2].rank == hand[4].rank)
       {
           return 1;
       }
       return 0;
   }
  
   // checks for two pairs
   public int twoPairs()
   {
       int check = 0;
       for(int counter = 1; counter < 5; counter++)
       {
           if (hand[counter - 1].rank == hand[counter].rank)
           {
               check++;
           }
       }
       if (check == 2)
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
  
   // check for pair
   public int pair()
   {
       int check = 0;
       for(int counter = 1; counter < 5; counter++)
       {
           if (hand[counter - 1].rank == hand[counter].rank)
           {
               check++;
           }
       }
       if (check == 1)
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
  
   // find highest card
   public int highCard()
   {
       int highCard = 0;
       for (int counter = 0; counter < 5; counter++)
       {
           if (hand[counter].rank > highCard)
           {
               highCard = hand[counter].rank;
           }
       }
       return highCard;
   }
  
   // asks user if they want to play again
   public void again()
   {
       System.out.print("Play again? (1 for yes, 0 for no)");
       again = scan.nextInt();
   }
  
   // generates string for each card in hand
   public void display(Card card)
   {
       if (card.rank == 1)
       {
           System.out.print("Ace of ");
       }
       if (card.rank == 2)
       {
           System.out.print("Two of ");
       }
       if (card.rank == 3)
       {
           System.out.print("Three of ");
       }
       if (card.rank == 4)
       {
           System.out.print("Four of ");
       }
       if (card.rank == 5)
       {
           System.out.print("Five of ");
       }
       if (card.rank == 6)
       {
           System.out.print("Six of ");
       }
       if (card.rank == 7)
       {
           System.out.print("Seven of ");
       }
       if (card.rank == 8)
       {
           System.out.print("Eight of ");
       }
       if (card.rank == 9)
       {
           System.out.print("Nine of ");
       }
       if (card.rank == 10)
       {
           System.out.print("Ten of ");
       }
       if (card.rank == 11)
       {
           System.out.print("Jack of ");
       }
       if (card.rank == 12)
       {
           System.out.print("Queen of ");
       }
       if (card.rank == 13)
       {
           System.out.print("King of ");
       }
       if (card.suit == 1)
       {
           System.out.print("Spades");
           System.out.println();
       }
       if (card.suit == 2)
       {
           System.out.print("Hearts");
           System.out.println();
       }
       if (card.suit == 3)
       {
           System.out.print("Diamonds");
           System.out.println();
       }
       if (card.suit == 4)
       {
           System.out.print("Clubs");
           System.out.println();
       }
      
   }
}

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