Yaay, my code words now. It all came to me last night/this morning. It's amazing how programming does that. I just don't get it, then later, I do, lol. But anyway here it is. By the way Greg, I appreciate your suggestion about a Deck class. That will actually help me a lot with the next exercise in the lesson, but this one was just about creating a Card class. Creating a "deck" and writing it to std/out was just my idea to test the code.
package cardsexercise;
class Card {
private static String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
private static String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack",
"Queen", "King"};
//Following two int vars will monitor position in the array and increment
//the card being created.
private static int suitCall = 0;
private static int rankCall = 0;
private String suit, rank;
public Card() { //First check if the suit needs to be advanced.
if(rankCall == ranks.length) {
rankCall = 0;
suitCall++;
}
suit = suits[suitCall];
rank = ranks[rankCall++];
}
public static int getTotalCards() {
return suits.length * ranks.length;
}
public String getSuit() {
return suit;
}
public String getRank() {
return rank;
}
}
public class CardsExercise {
public static void main(String[] args) {
int numCards = Card.getTotalCards();
Card[] cards;
cards = new Card[numCards]; //Create a Card array of a length equal
//to the total possible number of cards.
for(int i = 0; i < numCards; i++) { //Fill the card array.
cards[i] = new Card();
}
for(int i = 0; i < cards.length; i++) {
int h = i; //Buffer var to shield i from the incrementation by j.
int j = ++h;
String k = cards[i].getSuit();
String n = cards[i].getRank();
System.out.println("Card " + j + ": " + n + " of " + k);
}
}
}
-s45