Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DeckofCards.class  (Basic Programming)

Posted on 2004-10-31
18
Medium Priority
?
231 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
Comment
Question by:romeotheriault
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 5
  • +1
18 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12458675
    // 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
ID: 12458690
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
ID: 12458691
The method should be some thing like this . Is n't it??

public String DealCard()
     {
          return card[count--];
     
     }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12458701
more like:

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

Expert Comment

by:sudhakar_koundinya
ID: 12458724
Yeah,

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

Expert Comment

by:sudhakar_koundinya
ID: 12458727
Yeah,

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

Expert Comment

by:sudhakar_koundinya
ID: 12458752
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
ID: 12458763
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
ID: 12458783
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:
petmagdy earned 1200 total points
ID: 12458793
      return card[count--].toString();

brackets :)
0
 

Author Comment

by:romeotheriault
ID: 12458794
with the new

return card[count--].toString;
0
 

Author Comment

by:romeotheriault
ID: 12458797
that works 8)
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 400 total points
ID: 12458803
follow petmagdy's suggestion. It works
0
 

Author Comment

by:romeotheriault
ID: 12458812
Thanks a ton guys.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12458818
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
ID: 12458826
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12458850
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
ID: 12458860
Thanks CEHJ, I will look into collections. I haven't worked with them yet.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

610 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