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

I have completed the Card Dealer program for Starting out with Java, 5th edition

ID: 3839010 • Letter: I

Question

I have completed the Card Dealer program for Starting out with Java, 5th edition. I was wondering if I have covered all the requirements below and if not how would I add to it...

1) The program must have File input AND output

2) It must handle some exceptions, more than what is needed for GUI and File I/O.

3) Use at least one of the following:

       a) Applet

       b) recursion

       c) searching and sorting of an array

4) Use a GUI interface with the ability to do different functions

This is what I have so far...

public class Cards extends JFrame
{
    private JLabel imageLabel1 = new JLabel(); //Label for card 1
    private JLabel imageLabel2 = new JLabel(); //Label for card 2
    private int numberOfCards = 56;             //Number of cards in deck
    private ImageIcon card1;                    //ImageIcon for card 1
    private ImageIcon card2;                    //ImageIcon for card 2
    private JPanel imagePanel;                  //To hold images
    private JPanel buttonPanel;                 //To hold buttons
    private int WINDOW_WIDTH = 200;             //Set window width
    private int WINDOW_HEIGHT = 150;            //Set window hight
  
    //Button to deal random cards
    private JButton dealButton;               
    Random rand = new Random();
  
    //Create a string array of 56 cards from 1 to 56
    String images[] = { "2_Clubs.jpg", "2_Diamonds.jpg", "2_Hearts.jpg",
        "2_Spades.jpg", "3_Clubs.jpg", "3_Diamonds.jpg", "3_Hearts.jpg",
        "3_Spades.jpg", "4_Clubs.jpg", "4_Diamonds.jpg", "4_Hearts.jpg",
        "4_Spades.jpg", "5_Clubs.jpg", "5_Diamonds.jpg", "5_Hearts.jpg",
        "5_Spades.jpg", "6_Clubs.jpg", "6_Diamonds.jpg", "6_Hearts.jpg",
        "6_Spades.jpg", "7_Clubs.jpg", "7_Diamonds.jpg", "7_Hearts.jpg",
        "7_Spades.jpg", "8_Clubs.jpg", "8_Diamonds.jpg", "8_Hearts.jpg",
        "8_Spades.jpg", "9_Clubs.jpg", "9_Diamonds.jpg", "9_Hearts.jpg",
        "9_Spades.jpg", "10_Clubs.jpg", "10_Diamonds.jpg", "10_Hearts.jpg",
        "10_Spades.jpg", "Ace_Clubs.jpg", "Ace_Diamonds.jpg",
        "Ace_Hearts.jpg","Ace_Spades.jpg", "Jack_Clubs.jpg",
        "Jack_Diamonds.jpg","Jack_Hearts.jpg","Jack_Spades.jpg",
        "Queen_Clubs.jpg", "Queen_Diamonds.jpg", "Queen_Hearts.jpg",
        "Queen_Spades.jpg", "King_Clubs.jpg", "King_Diamonds.jpg",
        "King_Hearts.jpg", "King_Spades.jpg", "Backface_Blue.jpg",
        "Backface_Red", "Joker_Red", "Joker_Black"};
  
    //Constructor that initializes the window
    public Cards()
    {
        //Set title
        setTitle("Card Dealer");
      
       //Set size
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
     
       //Close the panel
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
       //Create a BorderLayout manager
       setLayout(new BorderLayout());
     
       //Build the panels
       buildImagePanel();
       buildButtonPanel();
     
       //Add panels to the content pane
       add(imagePanel, BorderLayout.CENTER);
       add(buttonPanel, BorderLayout.SOUTH);
     
       //Pack and display the window
       pack();
       setVisible(true);
     
    }
  
    private void buildImagePanel()
    {
        //Create panel
        imagePanel = new JPanel();
      
        //Add two images to the deck
        card1 = new ImageIcon(images[rand.nextInt(images.length - 1)]);
        imageLabel1.setIcon(card1);
      
        card2 = new ImageIcon(images[rand.nextInt(images.length - 2)]);
        imageLabel2.setIcon(card2);
      
        //Remove two cards from the deck
        numberOfCards = numberOfCards - 2;
      
        //Add image labels to image panel
        imagePanel.add(imageLabel1);
        imagePanel.add(imageLabel2);
      
    }
  
    private void buildButtonPanel()
    {
        //Create panel
        buttonPanel = new JPanel();
      
        //Create button
        dealButton = new JButton("Deal a Card");
      
        //Register an action listener with the button
        dealButton.addActionListener(new ButtonListener());
      
        //Add the button to the panel
        buttonPanel.add(dealButton);
      
    }
  
    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //Check if number of cards are not zero
            if(numberOfCards > 0)
            {
                //Call shuffle method with number of cards to get
                //random card image
                shuffle(images, numberOfCards);
              
                //Set random card image to ImageIcon
                card1 = new ImageIcon(images[numberOfCards - 1]);
           
                //Add card to ImageLabel
                imageLabel1.setIcon(card1);
              
                //Remove a card from the deck
                numberOfCards--;
              
                //Add second random card image to ImageIcon
                card2 = new ImageIcon(images[numberOfCards - 1]);
              
                //Add card to ImageLabel
                imageLabel2.setIcon(card2);
              
                //Remove a random card from the deck
                numberOfCards--;
              
            }
            else
            {
                imageLabel1.setIcon(null);
                imageLabel2.setIcon(null);
                imageLabel1.setText("OUT OF CARDS");
                imageLabel2.setText("OUT OF CARDS");
            }
        }
    }
   public void shuffle(String images[], int n)
    {
       //Shuffle the cards
        for(int i = 0; i < n; i++)
        {
            //Generate two random numbers
            int rand1 = rand.nextInt(n);
            int rand2 = rand.nextInt(n);
          
            //Exchange cards
            String temp1 = images[rand1];
            String temp2 = images[rand2];
            images[rand2] = temp1;
            images[rand1] = temp2;
        }
    }
}

public class CardDealer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Intantiate CardDealer class
        new Cards();
    }
}

Explanation / Answer

(1)

CARDRUN CLASS

Card Class

Deck Class

(2)

(1)

CARDRUN CLASS

  public class CardRun {    public static void main(String[] args)  {      Deck deck = new Deck();      Card C;        System.out.println(deck.getTotalCards());        while(deck.getTotalCards() != 0)      {          C = deck.drawFromDeck();          System.out.println(C.toString());      }  }  

Card Class

  public class Card {      private int card, suit;  
  private static String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};      private static String[] cards = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};        Card(int suit, int card)      {          this.card = card;          this.suit = suit;      }        public @Override String toString()      {          return cards[card] + " of " + suits[suit];      }        public int getCard()      {          return card;      }        public int getSuit()      {          return suit;      }  }  

Deck Class

  public class Deck {    private Card[]cards;  int i;    Deck()  {      i = 51;       cards = new Card[52];      int x = 0;      for(int a=0; a<=3; a++)      {          for(int b=0; b<=12; b++)          {              cards[x] = new Card(a,b);              x++;          }      }  }    public Card drawFromDeck()  {      Random generator = new Random();      int index = 0;        index = generator.nextInt(i);      Card temp = cards[index];      cards[index] = cards[i];      cards[i] = null;      i--;      return temp;  }    public int getTotalCards()  {      return i;  }  

(2)

  public class CardsDeck {      private ArrayList<Card> mCards;      private ArrayList<Card> mPulledCards;      private Random mRandom;    public static enum Suit {      SPADES,      HEARTS,      DIAMONDS,      CLUBS;  }    public static enum Rank {      TWO,      THREE,      FOUR,      FIVE,      SIX,      SEVEN,      EIGHT,      NINE,      TEN,      JACK,      QUEEN,      KING,      ACE;  }    public CardsDeck() {      mRandom = new Random();      mPulledCards = new ArrayList<Card>();      mCards = new ArrayList<Card>(Suit.values().length * Rank.values().length);      reset();  }    public void reset() {      mPulledCards.clear();      mCards.clear();      /* Creating all possible cards... */      for (Suit s : Suit.values()) {          for (Rank r : Rank.values()) {              Card c = new Card(s, r);              mCards.add(c);          }      }  }      public static class Card {        private Suit mSuit;      private Rank mRank;        public Card(Suit suit, Rank rank) {          this.mSuit = suit;          this.mRank = rank;      }        public Suit getSuit() {          return mSuit;      }        public Rank getRank() {          return mRank;      }        public int getValue() {          return mRank.ordinal() + 2;      }        @Override      public boolean equals(Object o) {          return (o != null && o instanceof Card && ((Card) o).mRank == mRank && ((Card) o).mSuit == mSuit);      }      }  public Card pullRandom() {      if (mCards.isEmpty())          return null;        Card res = mCards.remove(randInt(0, mCards.size() - 1));      if (res != null)          mPulledCards.add(res);      return res;  }    public Card getRandom() {      if (mCards.isEmpty())          return null;        Card res = mCards.get(randInt(0, mCards.size() - 1));      return res;  }    public int randInt(int min, int max) {         int randomNum = mRandom.nextInt((max - min) + 1) + min;      return randomNum;  }      public boolean isEmpty(){      return mCards.isEmpty();  }  }  
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