Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A deck of cards

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A deck of cards

    Create a class called CardDeck with a main method contents exactly as shown here:

    {
    String[] deck=new String[52];
    createDeck(deck);
    for(int i=0; i<52; i++)
    System.out.println(deck[i]);
    }


    You should create a method called createDeck which populates the array you created in the main method. You must use four loops, each creates the 13 cards for one of the four suits. Within the loop the card names should be automatically generated and inserted in the array. (e.g., “Ace of Spades”, “2 of Spades”…”King of Hearts”, etc.) You must use loops to generate the card names, you must not create 52 literal Strings.

    public class CardDeck
    {
    	public static void main(String[] args)
    	{
    	String[] deck=new String[52];
    	createDeck(deck);
    	for(int i=0; i<52; i++)
    	System.out.println(deck[i]);
    	}
    	public static void createDeck(String[] myDeck)
    	{
    	String[] num={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
    	int count=0;
    	for(int x=0; x<13; x++)
    	myDeck[x]=num[x]+" of Diamonds";
    	count++;
    	for(int x=13; x<26; x++)
    	myDeck[count]=num[count]+" of Spades";
    	count++;
    	for(int x=26; x<39; x++)
    	myDeck[count]=num[count]+" of Hearts";
    	count++;
    	for(int x=39; x<52; x++)
    	myDeck[count]=num[count]+" of Clubs";
    	} // end createDeck
    } // end class CardDeck


    What is wrong with my code?

    The desired output is:

    Ace of Diamonds
    2 of Diamonds
    3 of Diamonds
    4 of Diamonds
    5 of Diamonds
    6 of Diamonds
    7 of Diamonds
    8 of Diamonds
    9 of Diamonds
    10 of Diamonds
    Jack of Diamonds
    Queen of Diamonds
    King of Diamonds
    Ace of Spades
    2 of Spades
    3 of Spades
    4 of Spades
    5 of Spades
    6 of Spades
    7 of Spades
    8 of Spades
    9 of Spades
    10 of Spades
    Jack of Spades
    Queen of Spades
    King of Spades

    etc.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: A deck of cards

    maybe try creating a second array for the suits. Then create your deck by combining the 2 arrays.
    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] mydeck = new String[num.length * suit.length];
    for (int i = 0; i < num.length; i++) {
      for (int j = 0; j < suit.length; j++) {
       mydeck[suit.length * i + j] = rank[i] + " of " + suit[j];
    			}
    		}
    Last edited by mickey2012; February 29th, 2012 at 11:04 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A deck of cards

    public static void createDeck(String[] myDeck)
    {
    	String[] num={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
     
    	int count=0; //  count starts at zero
    	for(int x=0; x<13; x++)
    	    myDeck[x]=num[x]+" of Diamonds";
     
    	count++;   // count is now 1
     
    	for(int x=13; x<26; x++)
    	    myDeck[count]=num[count]+" of Spades";   // this always accesses myDeck[1] and num[1]
     
    	count++; // count is now 2
     
    	for(int x=26; x<39; x++)
    	    myDeck[count]=num[count]+" of Hearts";  // this always accesses myDeck[2] and num[2]
     
    	count++;  // count is now 3
     
    	for(int x=39; x<52; x++)
    	    myDeck[count]=num[count]+" of Clubs";   // this always accesses myDeck[3] and num[3]
    } // end createDeck

    What you need to do is keep track of the total number of cards generated, and use that to index through the deck:
    public static void createDeck(String[] myDeck)
    	{
    		String[] num={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
     
    		int count=0;
    		for(int x=0; x<13; x++, count++)
    		    myDeck[count]=num[x]+" of Diamonds";
     
     
    		for(int x=0; x<13; x++, count++)
    			myDeck[count]=num[x]+" of Spades";
     
    		for(int x=0; x<13; x++, count++)
    			myDeck[count]=num[x]+" of Hearts";
     
    		for(int x=0; x<13; x++, count++)
    			myDeck[count]=num[x]+" of Clubs";
    	} // end createDeck

    Or you could add another array to store the suits as suggested above (although that solution does not conform to the requirements).

    Edit: My initial post was incorrect -- I appologise if anyone saw it before I had a chance to correct.
    Last edited by rektiphyr; March 1st, 2012 at 10:31 AM. Reason: Code correction

Similar Threads

  1. I'm making a program that recognizes cards say for poker
    By Lurie1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 13th, 2012, 04:44 PM
  2. Adding cards back into deck
    By sp4ce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2011, 01:21 AM
  3. Sorting a deck of cards by suite or rank.
    By coyne20 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2010, 08:47 AM