I do not know where to even start :'( can someone please write the code so I can see the process and I can do other problems that are similar to this...

For this programming project, you will create a program that will both design and implement a class that you can call from your main program (called printCards). The class you will create will be called Card, and it will represent a standard deck of playing cards (4 suits: Clubs, Diamonds, Hearts, Spades; and 13 cards per suit: Ace through 10, Jack, Queen, and King).

You will also create a main program that will call the Card class and have it deal 20 random cards to a player (this will print using a toString method in a list format: Nine of Spades, Three of Diamonds, and so forth). See the sample output below. Use a constant to represent the number 20, which is the number of cards that are dealt. You do not need to worry at this point about duplicate cards being generated in the 20 random cards.

Thanks guys!

Nick Evan commented: Do provide evidence of having done some work yourself if posting questions from school or work assignments -4

## All 11 Replies

Start small. Pick a small easy part of the program to do. Write the code for that and come here as you have problems.

``````public class Card {

public final static int SPADES = 0;   // Codes for the 4 suits, plus Joker.
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int JOKER = 4;

public final static int ACE = 1;      // Codes for the non-numeric cards.
public final static int JACK = 11;    //   Cards 2 through 10 have their
public final static int QUEEN = 12;   //   numerical values for their codes.
public final static int KING = 13;

/**
* This card's suit, one of the constants SPADES, HEARTS, DIAMONDS,
* CLUBS, or JOKER.  The suit cannot be changed after the card is
* constructed.
*/
private final int suit;

/**
* The card's value.  For a normal card, this is one of the values
* 1 through 13, with 1 representing ACE.  For a JOKER, the value
* can be anything.  The value cannot be changed after the card
* is constructed.
*/
private final int value;

/**
* Creates a Joker, with 1 as the associated value.  (Note that
* "new Card()" is equivalent to "new Card(1,Card.JOKER)".)
*/
public Card() {
suit = JOKER;
value = 1;
}

/**
* Creates a card with a specified suit and value.
* @param theValue the value of the new card.  For a regular card (non-joker),
* the value must be in the range 1 through 13, with 1 representing an Ace.
* You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING.
* For a Joker, the value can be anything.
* @param theSuit the suit of the new card.  This must be one of the values
* Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER.
* @throws IllegalArgumentException if the parameter values are not in the
* permissible ranges
*/
public Card(int theValue, int theSuit) {
if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
theSuit != CLUBS && theSuit != JOKER)
throw new IllegalArgumentException("Illegal playing card suit");
if (theSuit != JOKER && (theValue < 1 || theValue > 13))
throw new IllegalArgumentException("Illegal playing card value");
value = theValue;
suit = theSuit;
}

/**
* Returns the suit of this card.
* @returns the suit, which is one of the constants Card.SPADES,
* Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER
*/
public int getSuit() {
return suit;
}

/**
* Returns the value of this card.
* @return the value, which is one of the numbers 1 through 13, inclusive for
* a regular card, and which can be any value for a Joker.
*/
public int getValue() {
return value;
}

/**
* Returns a String representation of the card's suit.
* @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs"
* or "Joker".
*/
public String getSuitAsString() {
switch ( suit ) {
case HEARTS:   return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS:    return "Clubs";
default:       return "Joker";
}
}

/**
* Returns a String representation of the card's value.
* @return for a regular card, one of the strings "Ace", "2",
* "3", ..., "10", "Jack", "Queen", or "King".  For a Joker, the
* string is always numerical.
*/
public String getValueAsString() {
if (suit == JOKER)
return "" + value;
else {
switch ( value ) {
case 1:   return "Ace";
case 2:   return "2";
case 3:   return "3";
case 4:   return "4";
case 5:   return "5";
case 6:   return "6";
case 7:   return "7";
case 8:   return "8";
case 9:   return "9";
case 10:  return "10";
case 11:  return "Jack";
case 12:  return "Queen";
default:  return "King";
}
}
}

/**
* Returns a string representation of this card, including both
* its suit and its value (except that for a Joker with value 1,
* the return value is just "Joker").  Sample return values
* are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades",
* "Joker", "Joker #2"
*/
public String toString() {
if (suit == JOKER) {
if (value == 1)
return "Joker";
else
return "Joker #" + value;
}
else
return getValueAsString() + " of " + getSuitAsString();
}

} // end class Card
``````

how do I do the second part, now that I have the class?

Can you describe what the "second part" is?
Break it down to a bunch of small, simple steps and do one at a time.

Please edit your post, select the code and wrap it in code tags by using the [code] icon above the input box.

Does anyone else believe that OP wrote that code in the 3 minutes between Norms' reply and his posting it?

I hope his instructor will give him the appropriate grade for whatever he turns in.

``````public class printCards
{
public static void main (String []args)
{
int x = 0; // keeps count of how many card
final int MAXCARDS = 20; // maximum number of cards

while (x < MAXCARDS)
{

Card card = new Card(); //creates a card called card
String result = card.cardGeneration(); //generates a random card
System.out.println(result); //prints name of card
x++;
}
}
}``````

You need to setup your IDE to properly indent your code. What you have posted is hard to read (and understand) because there is no indentation for each nesting level of the logic. For example:

``````public class PrintCards
{
public static void main (String []args)
{
int x = 0; // keeps count of how many card
final int MAXCARDS = 20; // maximum number of cards
...``````

You did NOT include any comments or questions with your last post.
What did you post it for?

Hi, I'm so sorry I wanted to try out what you suggested, wrapping it in code.

How do I set up IDE?

How do I set up IDE?