Results 1 to 16 of 16

Thread: [SOLVED] Hum.

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

    Default [SOLVED] Hum.

    My arraylist isn't reading my Card file and I don't know why. Here are my two .java files. Any suggestions?

    public class Card
    {
    public Card(int number)
    {
    char[] value = new char[]{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
    String[] suit = new String[]{"Clubs", "Diamonds", "Spades", "Hearts"};
    }
    }


    And...


    import java.util.ArrayList;

    public class DeckOfCards
    {
    ArrayList<Card> deck = new ArrayList<Card>();
    {
    for (int i = 0; i < value.length; i++)
    {
    for (int j = 0; j < suit.length; j++)
    {
    String card = value[i] + " of " + suit[j];
    deck.add(card);
    }
    }
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You don't have a file reader. Or are you saying it is not importing the class? Well, give them a package, you should not make things in the "default" package, importing does not always work correctly from there. Also, make sure you compile Card before you compile DeckOfCards.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    arraylist isn't reading my Card file
    What does "reading" mean?
    Can you explain the problem a bit more?
    Reading usually means getting data from a file or a stream, not retrieving an element from an array or collection.

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

    Default

    Let's see...I need two seperate classes. The Card class is where I create a card. And the DeckOfCards class is suppose to create an ArrayList of a deck with the card in Card. More than likely I'm just really confused. Later I'll be creating more methods in Card that will compare two cards and peek at the top of the deck, and in DeckOfCards I'll shuffle and such. Just can't get this part straight to move on.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So what, exactly, is wrong. Post your compiler messages and/or exceptions, or give a complete, but brief description of what is suppossed to happen and what is happening and how they differ.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class DeckTest
    {
        public static void main(String[] args)
        {
             DeckOfCards test = new DeckOfCards();
             List<Card> deck = test.deck;
             Card card = deck.get(0);
             System.out.println("card.value = " + card.value);
             System.out.println("card.suit = " + card.suit);
             // You'll discover an easier way to do this
             // when you study the [i]toString[/i] method.
    
             // Another way to get a [i]char[][/i]:
             char[] chars = "A23456789TJQK".toCharArray();
             System.out.println("chars = " + Arrays.toString(chars));
        }
    }
    
    class DeckOfCards
    {
        static char[] value =
        {
            'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'
        };
        String[] suit = { "Clubs", "Diamonds", "Spades", "Hearts" };
    
        // The way you are doing this will require access to these
        // two arrays above.
        ArrayList<Card> deck = new ArrayList<Card>();
        {
            for (int i = 0; i < value.length; i++)
            {
                for (int j = 0; j < suit.length; j++)
                {
                    Card card = new Card(value[i], suit[j]);
                    deck.add(card);
                } 
            }
        }
    }
    
    class Card
    {
        char value;
        String suit;
    
        public Card(char value, String suit)
        {
            this.value = value;
            this.suit = suit;
        }
    }

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

    Default

    Okay I've read some more into this and think I'm gonna do something a little different. (Might make some more sense to you pros.) Well...this is gonna be my Card class for the most part. All the things I want it to do. I think I need to create just one card with this using mod division (%) to divide the deck size (52) by 13 to determine the suit and the remainder determine the value. Then in the DeckOfCards class build the ArrayList with a for loop creating the deck. (That sound right..?)

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

    }
    public String getSuit() // typical getter
    {

    }
    public char getValue() // typical getter
    {

    }
    public int compareTo(Card aCard) // compares this card to the parameter for equality, greaterthan, lessthan
    {

    }
    public String toString() // returns a String in this format: 2 of Clubs or T of Diamonds
    {

    }

    int number; // an integer instance field holding the card's unique number between 0-51
    String suit; // a String instance field holding the card's suit (Clubs, Diamonds, Spades, Hearts)
    char value; // the card's value using a single character (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K)
    }

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

    Default

    And I'm not really sure how...

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    That sound right..?
    Yes.
    Java Code:
    public class Card
    {
        // Consider making these member variables.
        String[] suit =
        {
            "Clubs", "Diamonds", "Spades", "Hearts"
        };
        char[] value =
        {
            'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'
        };
    
        public Card(int number)    
        {
    
        }
    
        public int getNumber() // typical getter
        {
    
        }
    
        public String getSuit() // typical getter
        {
    
        }
    
        public char getValue() // typical getter
        {
    
        }
    
        public int compareTo(Card aCard)    
        {
    
        }
    
        /**
         * returns a String in this format: 2 of Clubs or T of Diamonds
         */
        public String toString()
        {
    
        }
    
        /**
         * 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;
    }
    And I'm not really sure how...
    using mod division (%) to divide the deck size (52) by 13 to determine the suit
    Java Code:
        public Card(int number)    
        {
            // How can you work with the argument "number"
            // to assign a value to each of the member
            // variables "number", "suit" and "value"
            // for this particular card instance that is
            // being created in this constructor?
        }

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

    Default

    Well, I think it should make a single array for one card which is assigned a number. Based on that number you get the suit by division(?) then the % should determine the value. Then String toString() stores value + "of " + suit for the number in the array. Heh, this may be jiberish, not sure is this is what you asked. Here's what I have so far... (Stupid question: What's a member variable?)

    CODE:

    import java.lang.String;

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

    public int getNumber() // typical getter
    {
    Number = suit.length * value.length;
    return Number;
    }
    public String getSuit() // typical getter
    {
    Suit = Number / 13;
    return Suit;
    }
    public char getValue() // typical getter
    {
    Value = Number % 13;
    return Value;
    }
    public int compareTo(Card aCard) // compares this card to the parameter for equality, greaterthan, lessthan
    {

    }
    public String toString() // returns a String in this format: 2 of Clubs or T of Diamonds
    {
    card = Value + "of " + Suit; //Dunno what to put exactly...
    }
    }
    int number; // an integer instance field holding the card's unique number between 0-51
    String suit; // a String instance field holding the card's suit (Clubs, Diamonds, Spades, Hearts)
    char value; // the card's value using a single character (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K)
    }

    And I get these errors and not sure why. (Prolly will get error for .length stuff when it compiles that far.)

    Card.java:10: illegal start of expression
    public int getNumber() // typical getter
    ^
    Card.java:33: ';' expected
    }
    ^
    2 errors

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    What's a member variable?
    Declaration(s) made in class scope so that everyone in the class can see the variable.
    Java Code:
    class Pseudo {
        int totalMinutes = 25;  // member/instance variable
    
        public void add(int minutes) {
            // Add the value of the local variable "minutes"
            // to the member/instance variable "totalMinutes."
            totalMinutes += minutes;
        }
    
        public int getMinutes() [
            return totalMinutes;
        }
    }
    And I get these errors and not sure why
    You had all the other method inside the constructor.
    Java Code:
    public class CardRx
    {
        // Member variable declarations.
        int number;
        String suit;
        char value;
    
        public CardRx(int number)
        {
            String[] suits = {"Clubs", "Diamonds", "Spades", "Hearts"};
            char[] values = {
                'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'
            };
            // [i]this.XXX[/i] refers to the member variable XXX.
            // Assign a value to each member variable so this
            // Card instance can remember the data.
            // member variable = local variable value
            this.number = number;
            this.suit = suits[(number/13)%4];
            this.value = values[number%13];
        }
    
        public int getNumber()
        {
            return number;
        }
        public String getSuit()
        {
            return suit;
        }
        public char getValue()
        {
            return value;
        }
        public int compareTo(Card aCard)
        {
            return 0;  // for now, so it will compile
        }
        public String toString()
        {
            return String.valueOf(value) + " of " + suit;
        }
    
        public static void main(String[] args) {
            // test this class
            for(int i = 0; i < 52; i++) {
                CardRx card = new CardRx(i);
                System.out.println(card);
            }
        }
    }

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

    Default

    Wow. You're awesome hardwired! my java teacher wasn't very good so I didn't get much out of the class. But you make it sound easy! Gonna hold off on the compareTo part till I can read up some more on it. Gonna start the DeckOfCards side of this and I'll post it when I get it done so you can check it out. (Heh, or if I start to crash again...)

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

    Default

    Okay, here's my other class. Not really confident in my codes here. The errors I'm getting are that values and suits cannot be found and for peek, I can't figure out how to return it.


    CODE:

    import java.util.*;

    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>();
    }
    public void resetDeck() // puts 52 cards into the deck and shuffles them
    {
    for (int i = 0; i < values.length; i++)
    {
    for (int j = 0; j < suits.length; j++)
    {
    Card card = new Card(values[i], suits[j]);
    deck.add(card);
    }// Not sure about this...
    // I guess implement shuffle()?
    }
    }
    public void shuffle() // randomizes the cards currently in the deck
    {
    // No clue...
    }
    public Card dealOneCard() // removes and returns the card currently on the top of the deck
    {
    if (deck.isEmpty())
    return null;
    else
    return deck.remove(deck.size()-1);
    }// note - return null if the deck is empty
    public Card peek() // returns the card currently on the top of the deck - deck is unchanged
    {
    if (deck.isEmpty())
    return null;
    else
    return (deck.card(i));// Not sure how to return this right.
    }// note - return null if the deck is empty
    public int size() // returns an integer indicating the number of cards currently in the deck
    {
    System.out.println("Cards in deck: " + deck.size()); //Not sure...
    }
    public String toString() // returns the entire deck of cards as one string
    {

    }// note - the first card in the string should be the next card to be dealt
    private final int DECK_SIZE = 52;
    }

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, hardwired, you going to continue doing his (home)work for him?

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

    Default

    Really? I'm just asking for a little help. I don't understand the errors and can't figure it out on my own. My programming is pretty weak, and I hope to get better, but I'm writing the code and he just tweaked it a little, or explained it, and helped me get it done.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Really. He's completed your previous assignment which is not going to help you get stronger.

Posting Permissions

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