Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to test my code for proper function?

 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing he shoe, deck and card class for blackjack. I am in a slum trying to test the code. i have written a testclass but I am not getting any ouput.I was wondering if you could look at my classes and tell me what I am doing wrong.

public class Card {
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;

public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;

private final int suit;

private final int value;

public Card(int theValue, int theSuit) {



value = theValue;
suit = theSuit;
}

public int getSuit() {

return suit;
}

public int getValue() {

return value;
}

public String SuittoString() {


switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "Invalid?";
}
}

public String getSValue() {

// If the card's value is invalid, "Invalid?" is returned.
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";
case 13: return "King";
default: return "Invalid?";
}
}

public String toString() {
// Return a String representation of this card, such as
// "10 of Hearts" or "Queen of Spades".
return getSValue() + " of " + SuittoString();
}

} // end class Card

Shoe class

public class CardTest {

//public static void main(String[] args) {

// A card dealt from the deck.

CardTest deck = new CardTest(); // Create the deck.


deck.shuffle();
deck.drawCard();


System.println("Deck contains:");
for ( int i = 0; i <= deck; i++ ) {
// Get a card from the deck, print it out,

card = Card.getValue();

System.println(" " + card);
}


}
}
[CODE]
If you could give me some insight on what i am doing wrong I would be truly grateful.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try making some very small tests. Create a card with (1,1) and see if it returns "Ace of Spades" or whatever you expect. Create a deck and see if it has 52 cards, if the 1st, 26th and 52nd cards are what they should be. Shuffle the deck and see if the deck is no longer in perfect order. Draw one card and see if it is no longer in the deck. (Hmmm, that might suggest a new method!) Once you get in this "tiny test" mode, I'm sure you will think of many more. And if the code doesn't lend itself to a test you think of, it may be a sign that you should refactor the code into smaller, simpler, testable methods.
Then do a google search on "test first" or "test driven development" and discover the wonders of writing the test first. A test that fails is your friend, because it tells you what to write. A test for a method that doesn't exist yet certainly qualifies as a test that fails. For a good time, build your new code one test at a time!
JUnit.org is all about a framework to make this kind of testing easy and fun. Test Infected is a great paper on the topic.
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic