Results 1 to 16 of 16
Thread: [SOLVED] Hum.
- 09-23-2008, 05:36 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
[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);
}
}
}
}
- 09-23-2008, 07:43 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 09-23-2008, 01:54 PM #3
What does "reading" mean?arraylist isn't reading my Card file
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.
- 09-23-2008, 05:37 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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.
- 09-23-2008, 05:52 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 09-23-2008, 09:50 PM #6
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; } }
- 09-24-2008, 01:08 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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)
}
- 09-24-2008, 01:50 AM #8
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
And I'm not really sure how...
- 09-24-2008, 03:07 AM #9
That sound right..?
Yes.
And I'm not really sure how...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; }
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? }
- 09-24-2008, 03:26 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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
- 09-24-2008, 06:15 AM #11
What's a member variable?
Declaration(s) made in class scope so that everyone in the class can see the variable.
And I get these errors and not sure whyJava 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; } }
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); } } }
- 09-24-2008, 06:24 PM #12
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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...)
- 09-24-2008, 07:16 PM #13
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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;
}
- 09-24-2008, 10:17 PM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, hardwired, you going to continue doing his (home)work for him?
- 09-24-2008, 10:29 PM #15
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
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.
-


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks