DeckofCards.class (Basic Programming)

Hello, I have a DeckofCards.class and I'm not sure how to do a few things here. I haven't created the Driver class yet but I'm not sure how to set up the toString method in this class and I'm also not sure how to have the dealCard method return the card[count].   hmmm, any suggestions welcome. Just a little confusion here. Thanks.


import java.util.Random;

public class DeckofCards

{
      /*************************************************************
      *  Instance Variables
      *************************************************************/
      // create the limit of the number of cards in the deck
      private final int DECK = 52;
      private int count = 0;
      Random generator = new Random();
      // instantiate the Card objects and create the space for them in memory
      Card[] card = new Card[DECK];
      
      
      
      /*************************************************************************
      *  This is the constructor
      *************************************************************************/
      
      public DeckofCards()
      {      
            
            // A loop that keeps creates a full deck of cards.
                        for (int cardvalue=1; cardvalue <= 13; cardvalue++)
                              for (int cardtype=1; cardtype <= 4; cardtype++)
                              {      
                                    card[count] = new Card(cardvalue, cardtype);
                                    
                                    //System.out.println(count); //Just a test.
                                    //System.out.println(card[count]); //Just another test
                                    count++;
                              }                        
                        
                  }            
      
      
      // Method to deal a card and take the appropriate card out of the deck.
      public String DealCard()
      {
            return card[count];
            count--;
      }
      
      // Method to get the total number of cards left in the deck.
      public int getCardNum()
      {
            int cardsLeft = count + 1;
            return cardsLeft;
      }
      
      
      
      // Method to shuffle the deck of cards. Not quite sure how this one will work yet.
      /*public int Shuffle()
      {
            int cardvalue = generator.nextInt(13) + 1;
            int cardtype = generator.nextInt(4) + 1;
            card[count] = new Card(cardvalue, cardtype);
      }*/
      
      
      // The toString method. This gets called silently when the class needs to print anything out.
      public String toString()
      {
            String result;
      
      
      }

}




public class Card
{
private int type;
private int number;

public Card (int value, int suit)
{
number = value;
type = suit;
}
      public String toString()
            {
            String results = "";
            switch (number)
                        {
                  
                              case 1:
                                    results = "ACE";
                                    break;
                              case 2:
                                    results = "TWO";
                                    break;
                              case 3:
                                    results = "THREE";
                                    break;
                              case 4:
                                    results = "FOUR";
                                    break;
                              case 5:
                                    results = "FIVE";
                                    break;
                              case 6:
                                    results = "SIX";
                                    break;
                              case 7:
                                    results = "SEVEN";
                                    break;
                              case 8:
                                    results = "EIGHT";
                                    break;
                              case 9:
                                    results = "NINE";
                                    break;
                              case 10:
                                    results = "TEN";
                                    break;
                              case 11:
                                    results = "JACK";
                                    break;
                              case 12:
                                    results = "QUEEN";
                                    break;
                              case 13:
                                    results = "KING";
                        
            
            }
            
            
            results = results + " of ";
            
            
            switch (type)
            {
               case 1:
                      results = results + "SPADES";
                     break;
               case 2:
                        results = results + "CLUBS";
                        break;
                  case 3:
                        results = results + "DIAMONDS";
                        break;
                  case 4:
                        results = results + "HEARTS";
            }
return results;

}
}
romeotheriaultAsked:
Who is Participating?
 
petmagdyConnect With a Mentor Commented:
      return card[count--].toString();

brackets :)
0
 
petmagdyCommented:
    // Method to deal a card and take the appropriate card out of the deck.
     public String DealCard()
     {
          return card[count];
          count--;
     }

return statement must always the last statment in ur function, count-- will never be excecuted
0
 
petmagdyCommented:
also example of toString implemention:

     public String toString()
     {
          String result="";
          for(int i=0; i < DECK; i++)
          {
               result += card[i].toString();
                result += " ";  //Tab as a separator
           }
           return result;
     }

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sudhakar_koundinyaCommented:
The method should be some thing like this . Is n't it??

public String DealCard()
     {
          return card[count--];
     
     }
0
 
petmagdyCommented:
more like:

public String DealCard()
     {
          return card[count--].toString;
     
     }
0
 
sudhakar_koundinyaCommented:
Yeah,

That's true. I issed that   ;-)
0
 
sudhakar_koundinyaCommented:
Yeah,

That's true. I missed that   ;-)
0
 
sudhakar_koundinyaCommented:
Modifying petmagdy's Method

    public String toString()
     {
          String result="{ ";
          for(int i=0; i < DECK; i++)
          {
               result += "[Card "+i+":"+card[i].toString()+"]\t";
               
           }
          String result=" }";
           return result;
     }

This is my view

regards
Sudhakar
0
 
petmagdyCommented:
yes sure this will do it too, but it depends on how remeothereouilt intends to use this presentation later, or maybe if he consider to parse it back

BR
0
 
romeotheriaultAuthor Commented:
Compiler error when I try to implement the Deal Card method.


DeckofCards.java:43: cannot find symbol
symbol  : variable toString
location: class Card
            return card[count--].toString;
0
 
romeotheriaultAuthor Commented:
with the new

return card[count--].toString;
0
 
romeotheriaultAuthor Commented:
that works 8)
0
 
sudhakar_koundinyaConnect With a Mentor Commented:
follow petmagdy's suggestion. It works
0
 
romeotheriaultAuthor Commented:
Thanks a ton guys.
0
 
CEHJCommented:
You would find it easier to use a collection of Card as a deck, rather than an array. Don't forget that dealing a card means removing it from the deck. How are you going to do this with an array? You have two options

a. set the element (Card) to null
b. resize the array to remove it

Both of these are somewhat problematic - you're better off with a collection
0
 
sudhakar_koundinyaCommented:
:-)
0
 
CEHJCommented:
If you need another reason

>>Not quite sure how this one will work yet.

Collections.shuffle(deck);

is pretty straightforward
0
 
romeotheriaultAuthor Commented:
Thanks CEHJ, I will look into collections. I haven't worked with them yet.
0
All Courses

From novice to tech pro — start learning today.