File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to test my code for proper function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to test my code for proper function?" Watch "How to test my code for proper function?" New topic

How to test my code for proper function?

Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
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,
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.


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);

If you could give me some insight on what i am doing wrong I would be truly grateful.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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! is all about a framework to make this kind of testing easy and fun. Test Infected is a great paper on the topic.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I agree. Here's the link:
subject: How to test my code for proper function?
It's not a secret anymore!