4 Replies - 8303 Views - Last Post: 03 May 2010 - 03:53 PM Rate Topic: -----

#1 tomgraham  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 26-March 10

Deck of Cards

Posted 03 May 2010 - 11:17 AM

I need to write a program that uses this class below called card. It needs to store 52 objects of the Card class. I need to include methods to shuffle deck, deal a card and report the number or cards left in the deck.

Then create a driver class with a main method that deals and prints each card from the deck.

I need some help setting up the basic idea of this. I don't know how to create the 52 objects.

Any help would be greatly appreciated!
import java.util.*;

public class Card {
	
	private String suit;
    private String face;
    
			public static void main (String []args){
				  // Make a CopyOfCard object
					Card co = new Card(); 

				  for (int i=1;i<=5;i++){
				    Random generator = new Random();
				    int facevalue = generator.nextInt(13) + 1;
				                     
				    String faceCard = co.faceCards(facevalue);
				    String cardSuit = co.getSuitname();

				    System.out.print(faceCard);
				    System.out.print(" of ");
				    System.out.println(cardSuit);

				  }
				}

		
		public Card(){
			 
		}
		
        //Assigns face card names.
        public String faceCards(int facevalue){
                        
                        if (facevalue == 11)
                        {
                                face = "Jack";
                        }
                        else if (facevalue == 12)
                        {
                                face = "Queen";
                        }
                        else if (facevalue == 13)
                        {
                                face = "King";
                        }
                        else if (facevalue == 1)
                        {
                                face = "Ace";
                        }
                        else {
                            face = String.valueOf(facevalue);
                        }
                        return face;
        		}
                        
                //Gets a random suit name.
                public String getSuitname(){
                Random generator = new Random();
                int randomsuitnum = generator.nextInt(4) + 1;
                				
                        if (randomsuitnum == 1)
                        {
                        		suit = "Hearts";
                        }
                        
                        else if (randomsuitnum == 2)
                        {
                        		suit = "Diamonds";
                        }
                        
                        else if (randomsuitnum == 3)
                        {
                        		suit = "Clubs";
                        }
                        
                        else if (randomsuitnum == 4)
                        {
                        		suit = "Spades";
                        }
                        return suit;
                }	
                
               
                
                
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: Deck of Cards

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Deck of Cards

Posted 03 May 2010 - 12:30 PM

You may find pbl's snippet Deck of Cards helpful.
Was This Post Helpful? 0
  • +
  • -

#3 tomgraham  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 26-March 10

Re: Deck of Cards

Posted 03 May 2010 - 01:03 PM

View Postmacosxnerd101, on 03 May 2010 - 11:30 AM, said:

You may find pbl's snippet Deck of Cards helpful.


         public void Deck()
	{
		int k = 0;
		for(int suit = 0; suit <= 3; suit++) {					
			for(int value = 1; value <= 13; value++) 
				cards[k++] = new Card(face, suit);
			}
		}
		clear();
}



I can't reference the variables face, and suit from my original Card class. I think it's because I have it set up so the number (0-3) corresponds to a string (Hearts, Diamonds, Clubs, Spades) and the other variable face corresponds to a string when over 10. (face cards - Jack, Queen, King, Ace)

How would I reference this cards[k++] = new Card(face, suit) ?
Was This Post Helpful? 0
  • +
  • -

#4 tomgraham  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 26-March 10

Re: Deck of Cards

Posted 03 May 2010 - 02:20 PM

View Posttomgraham, on 03 May 2010 - 12:03 PM, said:

View Postmacosxnerd101, on 03 May 2010 - 11:30 AM, said:

You may find pbl's snippet Deck of Cards helpful.


         public void Deck()
	{
		int k = 0;
		for(int suit = 0; suit <= 3; suit++) {					
			for(int value = 1; value <= 13; value++) 
				cards[k++] = new Card(face, suit);
			}
		}
		clear();
}



I can't reference the variables face, and suit from my original Card class. I think it's because I have it set up so the number (0-3) corresponds to a string (Hearts, Diamonds, Clubs, Spades) and the other variable face corresponds to a string when over 10. (face cards - Jack, Queen, King, Ace)

How would I reference this cards[k++] = new Card(face, suit) ?


anyone ? =[
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Deck of Cards

Posted 03 May 2010 - 03:53 PM

Actually, what macosxnerd101 showed in his post is a great way o create a deck of cards.
However, you say that you must stick to the Card class you posted.
so, let's go with it :)
you have to create 52 Card Objects.
notice that you generate Random suit and face, so there is a very small chance that you may create a deck of unique cards.
now, to have a Deck strcture, you can declare an array of 52 Card Objects.
Card[] deck = new Card[52];

have a for loop, and each iteration of the loop create a new Card.
you have to create a Card calling the constructor.
in your class, the constructor gets no parameters.
so it will look like:
//i is the current index of the loop
deck[i] = new Card();

now, i will admit that that way of creating a Card is not the best way.
the methods you have getFace and getSuit only generate a Random face\suit and returns it as a String Object.
But you never updates the suit and face members of the Card Object.
so to me, you must have set methods to set those members. (or pass these as parameters in the constructor).
then you would be able to call for each iteration:
deck[i].setFace(/*face string*/);
deck[i].setSuit(/*suit string*/);


so if you are going to apply the changes i suggested, you may want to check again pbl's deck of cards ;)

notice that the problem you can't create a Card the way that snippet creates it, is because a Card Constructor in the snippet gets a face and suit as parameters, while yours does'nt.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1