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

Can you help me fix up my program so that it can run properly when I try it out.

ID: 3581553 • Letter: C

Question

Can you help me fix up my program so that it can run properly when I try it out. I don't understand what I'm doing wrong and I can't seem to run the program. Netbeans says the if(numberCards > remain()) statement is incorrect. Heres the program below.

import java.util.*;


//=================================================================================
/** class PlayingCardException: It is used for errors related to Card and Deck objects
* Do not modify this class!
*/
public class PlayingCard extends Exception {

    /* Constructor to create a PlayingCardException object */
    PlayingCard (){
       super ();
    }

    PlayingCard ( String reason ){
       super ( reason );
    }
}

//=================================================================================
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank - valid values are 1 to 13
* Suit - valid values are 0 to 3
* Do not modify this class!
*/
class Card {
  
    /* constant suits and ranks */
    static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
    static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    /* Data field of a card: rank and suit */
    private int cardRank; /* values: 1-13 (see Rank[] above) */
    private int cardSuit; /* values: 0-3 (see Suit[] above) */

    /* Constructor to create a card */
    /* throw PlayingCardException if rank or suit is invalid */
    public Card(int suit, int rank) throws PlayingCard {
   if ((rank < 1) || (rank > 13))
       throw new PlayingCard("Invalid rank:"+rank);
   else
           cardRank = rank;
   if ((suit < 0) || (suit > 3))
       throw new PlayingCard("Invalid suit:"+suit);
   else
           cardSuit = suit;
    }

    /* Accessor and toString */
    /* You may implement equals(), but it will not be used */
    public int getRank() { return cardRank; }
    public int getSuit() { return cardSuit; }
    public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }

  
    /* Few quick tests here */
    public static void main(String args[])
    {
   try {
        Card c1 = new Card(3,1);    // A Spades
        System.out.println(c1);
        c1 = new Card(0,10);   // 10 Clubs
        System.out.println(c1);
        c1 = new Card(5,10);        // generate exception here
   }
   catch (PlayingCard e)
   {
        System.out.println("PlayingCard: "+e.getMessage());
   }
    }
}

//=================================================================================
/** class Decks represents : n decks of 52 playing cards
* Use class Card to construct n * 52 playing cards!
*
* Do not add new data fields!
* Do not modify any methods
* You may add private methods
*/

class Decks {

    /* this is used to keep track of original n*52 cards */
    private List originalDecks;

    /* this starts with copying cards from originalDecks */
    /* it is used to play the card game                  */
    /* see reset(): resets gameDecks to originalDecks    */
    private List gameDecks;

    /* number of decks in this object */
    private int numberDecks;


    /**
     * Constructor: Creates default one deck of 52 playing cards in originalDecks and
     *             copy them to gameDecks.
     *              initialize numberDecks=1
     * Note: You need to catch PlayingCardException from Card constructor
     *         Use ArrayList for both originalDecks & gameDecks
     */
    public Decks()
    {
        this(1);// implement this method!
    }


    /**
     * Constructor: Creates n decks (52 cards each deck) of playing cards in
     *              originalDecks and copy them to gameDecks.
     *              initialize numberDecks=n
     * Note: You need to catch PlayingCardException from Card constructor
     *         Use ArrayList for both originalDecks & gameDecks
     */
    public Decks(int n)
    {
        originalDecks = new ArrayList<>();
        numberDecks = n;
        for(int i = 0; i < numberDecks; i++){
            for(int j = 0; j < 4; j++){
                for(int h = 1; h <=13; h++){
                    try{
                        originalDecks.add(new Card(h, j));
                    }
                    catch(PlayingCard e){
                        System.out.println(e);
                    }
                }
            }
        }
        gameDecks = originalDecks;
    }


    /**
     * Task: Shuffles cards in gameDecks.
     * Hint: Look at java.util.Collections
     */
    public void shuffle()
    {
        Collections.shuffle(gameDecks);
    }

    /**
     * Task: Deals cards from the gameDecks.
     *
     * @param numberCards number of cards to deal
     * @return a list containing cards that were dealt
     * @throw PlayingCardException if numberCard > number of remaining cards
     *
     * Note: You need to create ArrayList to stored dealt cards
     *       and should removed dealt cards from gameDecks
     *
     */
    public List deal(int numberCards) throws PlayingCard
    {
        List temp = new ArrayList<>();
        if(numberCards > remain()) //heres the error location
            throw new PlayingCard("You can't deal more than the number of cards left.");
        for(int i = 0; i < numberCards; i++){
            temp.add(gameDecks.remove(0));
        }
        return temp;
    }

    /**
     * Task: Resets gameDecks by getting all cards from the originalDecks.
     */
    public void reset()
    {
        gameDecks = originalDecks;
    }

    /**
     * Task: Return number of remaining cards in gameDecks.
     */
    public int remainSize()
    {
   return gameDecks.size();
    }

    /**
     * Task: Returns a string representing cards in the gameDecks
     */
    public String toString()
    {
   return ""+gameDecks;
    }


    /* Quick test                   */
    /*                              */
    /* Do not modify these tests    */
    /* Generate 2 decks of cards    */
    /* Loop 2 times:                */
    /*   Deal 30 cards for 4 times */
    /*   Expect exception last time */
    /*   reset()                    */

    public static void main(String args[]) {

        System.out.println("*******    Create 2 decks of cards      ********* ");
        Decks decks = new Decks(2);
       
   for (int j=0; j < 2; j++)
   {
           System.out.println(" ************************************************ ");
           System.out.println("Loop # " + j + " ");
       System.out.println("Before shuffle:"+decks.remainSize()+" cards");
       System.out.println(" "+decks);
           System.out.println(" ============================================== ");

                int numHands = 4;
                int cardsPerHand = 30;

           for (int i=0; i < numHands; i++)
      {
               decks.shuffle();
                System.out.println("After shuffle:"+decks.remainSize()+" cards");
                System.out.println(" "+decks);
           try {
                        System.out.println(" Hand "+i+":"+cardsPerHand+" cards");
                        System.out.println(" "+decks.deal(cardsPerHand));
                        System.out.println(" Remain:"+decks.remainSize()+" cards");
                    System.out.println(" "+decks);
                        System.out.println(" ============================================== ");
           }
           catch (PlayingCard e)
           {
                   System.out.println("*** In catch block:PlayingCardException:Error Msg: "+e.getMessage());
           }
       }


       decks.reset();
   }
    }


    }

Explanation / Answer

if(numberCards > remain())

in this code you are trying to call remain() method and comparing, but no where you have mentioned remain() method in your code

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