1 Replies - 1804 Views - Last Post: 14 November 2015 - 06:04 PM Rate Topic: -----

#1 Nguyen13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-November 15

Creating a Deck of Cards

Posted 14 November 2015 - 05:36 PM

Hi! I am new to programming and am following directions on how to create a deck of cards. So far, I have a card class that's set up fine and has getter and setter methods and a toString method. One of the instructions in the deck class says to create a deck, using a Card object for every combination of rank and suit. I'm not really sure how to do this and I couldn't really understand much from googling. So far, I have this:
public class Deck
{
    public static final int NUM_CARDS = 52;
    
    public Deck();
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
    
    Card[] deck = NUM_CARDS;
	int i = 0;
	 
	for(String suit : suits) {
	     for(String rank : ranks) {
	        deck[i++] = new Card(suit, rank);
	     }
	 }
}



I'm unsure of how to set up the constructor for this. Right now all I have is public Deck() and don't know what else should be in this constructor.
Also, does the for loop make sense for this?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Deck of Cards

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 709
  • View blog
  • Posts: 5,420
  • Joined: 25-December 13

Re: Creating a Deck of Cards

Posted 14 November 2015 - 06:04 PM

Quote

does the for loop make sense for this?

What happens when you compile and execute it? Does it do what you want?

Note: To test the code:
add a main() method that calls the Deck constructor.
At the end of the constructor Print out the deck array's contents.

This post has been edited by NormR: 14 November 2015 - 06:14 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1