Results 1 to 10 of 10
  1. #1
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default How to write a Main to test this...

    Okay, this is my first program with ArrayLists, and I have my classes done. I need to write a main() in my DeckOfCards class and I don't know how to do it. It needs to test all the methods. I know how to test everything in the Card class, and the size() method in DeckOfCards, but the rest I'm not sure about.

    DeckOfCards CODE:

    import java.util.*;
    import java.lang.String;

    public class DeckOfCards
    {
    ArrayList<Card> deck = new ArrayList<Card>(); // an ArrayList to hold card objects

    public DeckOfCards() // default constructor creates an empty deck
    {
    deck = new ArrayList<Card>();
    }
    /**
    Puts 52 cards into the deck and shuffles them.
    */
    public void resetDeck()
    {
    deck.clear();
    Card card;
    int i;
    for (i = 0; i < DECK_SIZE; i++)
    {
    card = new Card(i);
    deck.add(card);
    }
    shuffle();
    }
    /**
    Randomizes the cards currently in the deck.
    */
    public void shuffle()
    {
    Card random;
    int position;
    Random generator = new Random();
    int i;
    for (i = 0; i < DECK_SIZE; i++)
    {
    position = generator.nextInt(DECK_SIZE - i);
    random = deck.remove(position);
    deck.add(random);
    }
    }
    /**
    Removes and returns the card currently on the top of the deck.
    */
    public Card dealOneCard()
    {
    if (deck.isEmpty())
    return null;
    else
    return deck.remove(deck.size()-1);
    }
    /**
    Returns the card currently on the top of the deck - deck is unchanged.
    */
    public Card peek()
    {
    if (deck.isEmpty())
    return null;
    else
    return (deck.get(deck.size()-1));
    }
    /**
    Returns an integer indicating the number of cards currently in the deck.
    */
    public int size()
    {
    return deck.size();
    }
    /**
    Returns the entire deck of cards as one string.
    */
    public String toString()
    {
    String currentDeck = " ";
    int i;
    for(i = deck.size() - 1; i <= 0; i--)
    {
    currentDeck += deck.get(i).toString() + "\n";
    }
    return currentDeck;
    }
    private final int DECK_SIZE = 52;

    public static void main(String args[])
    {
    ArrayList<Card> deck = new ArrayList<Card>();
    deck = deck.resetDeck();
    System.out.print(deck);




    }
    }


    Card class CODE:

    import java.lang.String;

    public class Card
    {
    /**
    Constructor - creates a card with the indicated number
    */
    public Card(int number)
    {
    String[] suits = {"Clubs", "Diamonds", "Spades", "Hearts"};
    char[] values = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};

    this.number = number;
    this.suit = suits[(number/13)%4];
    this.value = values[number%13];
    }
    public int getNumber() // typical getter
    {
    return number;
    }
    public String getSuit() // typical getter
    {
    return suit;
    }
    public char getValue() // typical getter
    {
    return value;
    }
    /**
    Compares this Card to the parameter (aCard) for equality, greaterthan, lessthan.
    Returns negative int if cardA < cardB, zero if cardA == cardB and
    positive if int cardA > cardB.
    */
    public int compareTo(Card aCard)
    {
    int cardA;
    int cardB;
    cardA = getNumber() % 13;
    cardB = aCard.getNumber() % 13;
    if(cardA != cardB)
    return cardA - cardB;
    else
    return getNumber() - aCard.getNumber();
    }
    /**
    Returns a String in this format: 2 of Clubs or T of Diamonds.
    */
    public String toString()
    {
    return String.valueOf(value) + " of " + suit;
    }
    /**
    An integer instance field holding the card's unique number between 0-51.
    */
    int number;
    /**
    A String instance field holding the card's suit (Clubs, Diamonds, Spades, Hearts).
    */
    String suit;
    /**
    The card's value using a single character (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K).
    */
    char value;

    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    public static void main(String[] args)
    {
        DeckOfCards dc = new DeckOfCards();
        dc.doStuff();//
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    I do that for all of them and get these errors.

    DeckOfCards.java:88: cannot find symbol
    symbol : method resetDeck()
    location: class java.util.ArrayList<Card>
    deck.resetDeck();
    ^
    DeckOfCards.java:89: cannot find symbol
    symbol : method shuffle()
    location: class java.util.ArrayList<Card>
    deck.shuffle();
    ^
    DeckOfCards.java:90: cannot find symbol
    symbol : method dealOneCard()
    location: class java.util.ArrayList<Card>
    deck.dealOneCard();
    ^
    DeckOfCards.java:91: cannot find symbol
    symbol : method peek()
    location: class java.util.ArrayList<Card>
    deck.peek();
    ^
    4 errors

  4. #4
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    OH! I just got it. It now compiles, Thanks a bunch Nicholas!

  5. #5
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Quick question though, how do I get my methods to print out with System.out.println();? I keep getting this error: "DeckOfCards.java:92: peek() in DeckOfCards cannot be applied to (int)
    System.out.println(test.peek(0));"
    And if I just put System.out.println(test); It compiles without printing...?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    "DeckOfCards.java:92: peek() in DeckOfCards cannot be applied to (int)
    The error message says that your peek method does NOT take an int as arg. try test.peek();
    It compiles without printing...?
    What does that mean? Do you mean there is no output when you EXECUTE the program?
    Add more println() statements to see where the execution flow is going.

  7. #7
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    This is what I have now, it compiles, but when I run it doesn't print anything.

    Java Code:
    public static void main(String args[])
          {
    			DeckOfCards deck = new DeckOfCards();
    			System.out.println(deck);
    			deck.resetDeck();
    			System.out.println(deck);
    			deck.shuffle();
    			System.out.println(deck);
    			deck.dealOneCard();
    			System.out.println(deck);
    			deck.peek();
    			System.out.println(deck);
    			deck.size();
    			System.out.println(deck);
    			deck.toString();
    			System.out.println(deck);
          }

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    You should really try to test one method at a time with println's just about anywhere something might go wrong. Work from the bottom up to make sure that the most basic methods work, starting with the constructor, and so forth. If your basic methods don't work, then those using them won't work either (toString for one).

  9. #9
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Well, it compiles with no errors. Shouldn't at least something print? It's blank lines.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    It's blank lines.
    To see what you are printing, change your println() to something like this:
    System.out.println("deck=" + deck);

    What is the value of deck that you want to print? The usage of the deck variable above will call its toString() method. Whatever the toString() method returns is what you will see.

    You call many methods for the deck object but do NOT save the return values. Do any of those methods return something that you might want to save? If so you need to assign those values to a variable:

    String aVar = deck.<method>();

Similar Threads

  1. Connectivity Test
    By jimhaddon in forum Java Applets
    Replies: 9
    Last Post: 08-16-2008, 04:40 PM
  2. Replies: 0
    Last Post: 04-05-2008, 12:38 AM
  3. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  4. Replies: 1
    Last Post: 08-06-2007, 09:11 AM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •