In Chapter 7 you modified a previously created Card class so
In Chapter 7, you modified a previously created Card class so that each Card would hold the name of a suit (\"Spades\", \"Hearts\", \"Diamonds\", or \"Clubs\") as well as a value (\"Ace\", \"King\", \"Queen\", \"Jack\", or a number value). Now, create an array of 52 Card objects, assigning a different value to each Card, and display each Card. Save the application as FullDeck.java
Solution
public class Card {
/**
* suit for the Card objects
*/
private String suit;
/**
* number (string) for the Card objects (for display)
*/
private String number;
/**
* number (int) for the Card objects (for evaluation and comparison)
*/
private int intNumber;
/**
* Default constructor for the Card class
*/
public Card() {
suit = \"Clubs\";
number = \"Ace\";
intNumber = 1;
}
/**
* Overloaded constructor for the Card class (assignment #3)
*
* @param totalCardSuits
* @param totalCardNumbers
*/
public Card(int totalCardSuits, int totalCardNumbers) {
if ((totalCardSuits >= 0 && totalCardSuits < 4)
&& (totalCardNumbers >= 0 && totalCardNumbers < 13)) {
String[] suitArray = new String[] { \"Clubs\", \"Hearts\", \"Spades\",
\"Diamonds\" };
suit = suitArray[totalCardSuits]; // suit of the cards
String[] numArray = new String[] { \"Ace\", \"Two\", \"Three\", \"Four\",
\"Five\", \"Six\", \"Seven\", \"Eight\", \"Nine\", \"Ten\", \"Jack\",
\"Queen\", \"King\" };
this.number = numArray[totalCardNumbers];
this.intNumber = totalCardNumbers;
}
else {
System.out
.println(\"Possible Error: \'SUIT\' can only accept numbers (0-3) and \"
+ totalCardSuits + \" was sent in the constructor\");
System.out
.println(\"Possible Error: \'NUMBER\' can only accept number (0-12) and \"
+ totalCardNumbers + \" was sent in the constructor\");
System.out
.println(\"Please make sure to only send the approvaite values in range\");
}
}
/**
* Method returns the value of the Card\'s number variable (String) to the
* calling method
*
* @return
*/
public String getCardNum() {
return number;
}
/**
* Method returns the value of the Card\'s number variable (int) to the
* calling method
*
* @return
*/
public int getCardIntNumber() {
return intNumber;
}
/**
* Method returns the value of the Card\'s suit variable to the calling
* method
*
* @return
*/
public String getCardSuit() {
return suit;
}
}
import java.util.ArrayList;
public class FullDeck {
/**
* this.class is the main Class of this application (when playing FullDeck)
*/
public FullDeck FiftyTwoPickup;
/**
* Array that holds the 52 Card objects
*/
public ArrayList<Card> deckOfCards;
/**
* Main method() for this class. Instantiates this.class
*
* @param args
*/
public static void main(String[] args) {
FullDeck FiftyTwoPickup = new FullDeck();
FiftyTwoPickup.buildTheDeck();
FiftyTwoPickup.displayTheDeck();
}
/**
* Default constructor for the Class.
*/
public FullDeck() {
// to do not used
}
/**
* Method that creates and displays the array of Card objects. This method
* is invoked from the War3 class.
*/
public void buildTheDeck() {
int totalCardSuits;
int totalCardNumbers;
deckOfCards = new ArrayList<Card>(52);
for (totalCardSuits = 0; totalCardSuits < 4; ++totalCardSuits) // suits
{
for (totalCardNumbers = 0; totalCardNumbers < 13; ++totalCardNumbers) // numbers
{
Card TempCard = new Card(totalCardSuits, totalCardNumbers);
deckOfCards.add(TempCard);
}
}
}
/**
* Method that displays the array of Card objects
*/
public void displayTheDeck() {
System.out.println(\"Playing Cards\");
System.out.println(\"=====================\");
for (int x = 0; x < 52; ++x) {
System.out.println(\"Card \" + (x + 1) + \" : \"
+ deckOfCards.get(x).getCardNum() + \" of \"
+ deckOfCards.get(x).getCardSuit());
}
}
}
OUTPUT:
Playing Cards
=====================
Card 1 : Ace of Clubs
Card 2 : Two of Clubs
Card 3 : Three of Clubs
Card 4 : Four of Clubs
Card 5 : Five of Clubs
Card 6 : Six of Clubs
Card 7 : Seven of Clubs
Card 8 : Eight of Clubs
Card 9 : Nine of Clubs
Card 10 : Ten of Clubs
Card 11 : Jack of Clubs
Card 12 : Queen of Clubs
Card 13 : King of Clubs
Card 14 : Ace of Hearts
Card 15 : Two of Hearts
Card 16 : Three of Hearts
Card 17 : Four of Hearts
Card 18 : Five of Hearts
Card 19 : Six of Hearts
Card 20 : Seven of Hearts
Card 21 : Eight of Hearts
Card 22 : Nine of Hearts
Card 23 : Ten of Hearts
Card 24 : Jack of Hearts
Card 25 : Queen of Hearts
Card 26 : King of Hearts
Card 27 : Ace of Spades
Card 28 : Two of Spades
Card 29 : Three of Spades
Card 30 : Four of Spades
Card 31 : Five of Spades
Card 32 : Six of Spades
Card 33 : Seven of Spades
Card 34 : Eight of Spades
Card 35 : Nine of Spades
Card 36 : Ten of Spades
Card 37 : Jack of Spades
Card 38 : Queen of Spades
Card 39 : King of Spades
Card 40 : Ace of Diamonds
Card 41 : Two of Diamonds
Card 42 : Three of Diamonds
Card 43 : Four of Diamonds
Card 44 : Five of Diamonds
Card 45 : Six of Diamonds
Card 46 : Seven of Diamonds
Card 47 : Eight of Diamonds
Card 48 : Nine of Diamonds
Card 49 : Ten of Diamonds
Card 50 : Jack of Diamonds
Card 51 : Queen of Diamonds
Card 52 : King of Diamonds



