Solved

DeckofCards.class  (Basic Programming)

Posted on 2004-10-31
226 Views
Last Modified: 2010-03-31
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;

}
}
0
Question by:romeotheriault
    18 Comments
     
    LVL 13

    Expert Comment

    by:petmagdy
        // 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
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     
    LVL 14

    Expert Comment

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

    public String DealCard()
         {
              return card[count--];
         
         }
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    more like:

    public String DealCard()
         {
              return card[count--].toString;
         
         }
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    Yeah,

    That's true. I issed that   ;-)
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    Yeah,

    That's true. I missed that   ;-)
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     

    Author Comment

    by:romeotheriault
    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
     
    LVL 13

    Accepted Solution

    by:
          return card[count--].toString();

    brackets :)
    0
     

    Author Comment

    by:romeotheriault
    with the new

    return card[count--].toString;
    0
     

    Author Comment

    by:romeotheriault
    that works 8)
    0
     
    LVL 14

    Assisted Solution

    by:sudhakar_koundinya
    follow petmagdy's suggestion. It works
    0
     

    Author Comment

    by:romeotheriault
    Thanks a ton guys.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    :-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    If you need another reason

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

    Collections.shuffle(deck);

    is pretty straightforward
    0
     

    Author Comment

    by:romeotheriault
    Thanks CEHJ, I will look into collections. I haven't worked with them yet.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now