/** DECK OF CARDS
* created by Arutha2321
*/
class Card { // A playing card, with variables i - number of card, color - color of card, value - value of card.
int i;
String color;
String value;
public Card (int j, StringBuffer f, StringBuffer h) { // A constructor of a card.
this.i = j;
this.color = f.toString();
this.value = h.toString();
}
public String showCard() { // A method that returns the color and value of a card in a string.
return (this.color + this.value + "\n");
}
}
class Deck { // A deck of card, with one variable cards, which is an array of instances of class Card.
Card[] cards;
public Deck() {
String[] colors = {"heart", "spade", "diamond", "club"};
String[] values = {"7", "8", "9", "10", "J", "Q", "K", "A"};
StringBuffer co = new StringBuffer();
StringBuffer va = new StringBuffer();
for (int i = 0; i < colors.length*values.length; i++) { // This creates one Card for every combination of color and value.
co.append(colors[i%colors.length]);
va.append(values[i%values.length]);
this.cards[i] = new Card(i,co,va);
}
}
public void showDeck() { //A method that prints out all of the cards in the deck.
for (int i = 0; i < colors.length*values.length; i++) {
System.out.println(this.cards[i].showCard());
}
}
}
public class DeckOfCards { // A class with the main function, that creates a deck and shows its content.
public static void main(String[] args) {
Deck deck1 = new Deck();
deck1.showDeck();
}
}