• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Cannot find symbol error with ArrayList method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! Newbie programmer here

I'm trying to make a class that works with two other classes to take in a boolean array and an object of type Deck (detailed below). The ultimate idea is to replace a dealt ArrayList of 5 cards and to replace each card (if the corresponding boolean  value in the boolean array is true) with a card from a shuffled array (all while adding to an ArrayList as a discard pile), but I'm getting the following errors:



Here's my Card class:




Here's my Deck class:



And here's my FiveCardHand class (which is what I'm working on):





For context (especially with the boolean array) this is my ideal output :

Player's Hand: FIVE CARDS:
Card # 1: Nine of  Clubs
Card # 2: Four of  Hearts
Card # 3: Three of  Hearts
Card # 4: Five of  Spades
Card # 5: Ace of  Clubs


Would you like to replace card # 1 ? y
Would you like to replace card # 2 ? n
Would you like to replace card # 3 ? n
Would you like to replace card # 4 ? n
Would you like to replace card # 5 ? n

Replacing card at position(s) 1--> with Nine of Diamonds

Player's Hand: FIVE CARDS:
Card # 1: Nine of  Diamonds
Card # 2: Four of  Hearts
Card # 3: Three of  Hearts
Card # 4: Five of  Spades
Card # 5: Ace of  Clubs



Thank you -- sorry this was so long! Would really appreciate some help
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your deckCards variable is of type Deck. In that class you don't have get(int) and remove(int) methods. You have an ArrayList as field, but you are not obtaining that field and then calling get() and remove(), you are calling those methods on Deck object, not on it's property (which is ArrayList)
On Deck object you can call only shuffle, deal, toString, sort (declared in your class) and other methods from Object class. You also don't have getter for that cardsArray field at this point, so you cannot obtain that ArrayList
 
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Consider whether you actually want a set() method at all. If you have a hand of cards, you will obviously want to do this sort of thing (‍):-Whoever wrote that Unicode page obviously has played bridge, because the suits have a specific order in bridge.
You can implement a remove() method in a hand class like this (and two other methods):-Remember that those three methods of the List interface aren't “void”. But I still think it isn't good design to expose all those methods. I would also worry about returning a reference to the List field; that would allow other code to make all sorts of alterations to your cards without you knowing anything about it.
 
Sheriff
Posts: 13569
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

angelina shreave wrote:
I'm trying to make a class that works with two other classes to take in a boolean array and an object of type Deck (detailed below). The ultimate idea is to replace a dealt ArrayList of 5 cards and to replace each card (if the corresponding boolean  value in the boolean array is true) with a card from a shuffled array (all while adding to an ArrayList as a discard pile):


One of the problems I see from this description alone is that you're mixing intent with implementation, abstract ideas with concrete ideas. "Deck" is an intent/abstract idea. "ArrayList" is an implementation detail. This mixing of levels of abstraction causes some of the confusion you're experiencing. Here's how I would keep the ideas separate:

Abstractions

Intent: I want to be able to replace any, all, or none of the Cards in a Hand with different Cards dealt from a Deck. Additionally, any Card that is taken from the Hand will be placed in a "Discarded" pile.

Definitions:

Card - a single Card with rank and suit
Hand - a collection of five Cards
Deck - standard collection of 52 unique Cards
Discarded pile - a collection of Cards that were removed from a Hand and replaced with Cards that were dealt from a Deck
"dealing" a card: Removing the first Card in a Deck and putting it into another (possibly empty) collection of Cards.

Design (high-level implementation):

Deck - represented by a List<Card> that contains Card objects
Deck.deal(n) - deal N number of Cards from the "top" of a Deck; when dealt, a Card is removed from the Deck. The "top" of a Deck is the first element of the List<Card>
Deck.shuffle() - randomize the order of the cards in a deck
Deck.put(Card) - put the given Card at the "top" of a deck
Deck.replace(n, Card) - replace the Nth card in the Deck with the given Card; returns the Card that was replaced.

----

Now, with the ideas organized neatly and clearly this way, it might be easier to see why I would do this to implement the functionality described:

In the above code, there's only a conceptual difference between a Hand, StandardDeck, and a DiscardPile. They are all "collections" of cards that you can do basically the same things with. So, implementation-wise, you only have one representation, a Deck object. The differentiation now falls down to the semantics, which is why I used variable names that reflected the intent for each Deck object: hand, standardDeck, and discardPile. Cards are dealt from  standardDeck and put into hand, and discardPile is used to hold all the Card objects removed from hand.

The implementation to accomplish each of the above operations now becomes relatively trivial once you have a clear idea of what you're trying to accomplish.

Does this make sense?
 
Junilu Lacar
Sheriff
Posts: 13569
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, one might think that using the name "Deck" to represent any collection of Card objects might be confusing. When this kind of consideration arises, I usually like to play around with the names I'm using. I might try this to address the concern:
 
Campbell Ritchie
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are we playing poker? I was thinking more along the lines of a whist‑like game, but in either case adding A♠, A♡, A♢, and A♣ looks like a very good game strategy
 
Junilu Lacar
Sheriff
Posts: 13569
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To summarize: because you mixed ideas related to intent with implementation details, you mistakenly thought you could call the ArrayList.get() and ArrayList.remove() methods, which were on the implementation object of ArrayList, through your conceptual object, the "Deck".

If you keep abstract ideas clearly separated from implementation details both in your mind and in your code, you'll see that at the high-level, you really needed only call methods on the Deck object that represented the ideas of dealing, putting, and removing cards (respectively, deal(int), put(Card), and remove(Card), for example).
 
Junilu Lacar
Sheriff
Posts: 13569
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As a matter of form, whenever you have an object that implements an interface, prefer using the interface type in the declaration rather than the implementation type. In this case, the implementation type ArrayList actually has an interface type, List, that defines the contract/intent of such an object. Given that ArrayList implements the List interface, these declarations are preferred:

This again keeps intent and implementation separate: The intent (use a List, any List) is on the left side while the implementation (Ok, I'll specifically use an ArrayList for this) is on the right side of the declaration.
 
angelina shreave
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow -- thank you so much for all your help and for taking the time to reply! My code works now
 
Campbell Ritchie
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

angelina shreave wrote:. . . thank you . . .

That's a pleasure

My code works now

Do show us what you have achieved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!