Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A method that removes the first card from the deck and returns it?

 
Rene Rad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed some classes at school and I'm trying to catch up on homework. I wasn't there for the lecture so I'm a bit lost on what do here. Teacher doesn't really have time to help me individually. Can anyone help? Instructions below.

Create A method that removes the first card from the deck and returns it, or returns null if there is
nothing in the deck. It must check to make sure the deck is not empty before attempting to
remove a card. This is the method signature:
public Card takeCard()


Here are my classes:



 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like a very good start. You're on the wrong track with the iterator though. Iterators are great when you want to search for something, or perform an action on many elements of a collection. Here you only want to do one thing: remove the top card. Take a look at the ArrayList.remove() method and see if that gets you where you want to go.
 
Sun Tzu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good start. Try this on takeCard() method:



If you understand why the code works, great. If not, keep trying.
Advice: you might want to use a set instead of a list, so that you will have no repeats. This will require more code on Card class, but will make code more robust.
An ArrayList will do (if you're careful), but try to figure out if how to implement this app using a Set<Card> to hold your deck of cards.

Hope this helps.
peace.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Sun Tzu
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sun Tzu: Please do not post complete solutions to problems, rather let people DoYourOwnHomework. We aim to help people learn how to learn by providing links, hints, general advice, and so on, rather than absolving them of responsibility.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you ever build a deck of cards?

-Hunter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the Deck class in his first post?? Please read the topic first then ask questions.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That creates an empty arrayList of type Card, it doesn't fill the deck with cards. It would be difficult to remove the top card if you never build the deck.

-Hunter
 
Rene Rad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun Tzu wrote:Good start. Try this on takeCard() method:



If you understand why the code works, great. If not, keep trying.
Advice: you might want to use a set instead of a list, so that you will have no repeats. This will require more code on Card class, but will make code more robust.
An ArrayList will do (if you're careful), but try to figure out if how to implement this app using a Set<Card> to hold your deck of cards.

Hope this helps.
peace.


Thanks SunTzu, this is what I was looking for. Just to clarify for other people. Whenever I use code from coderanch.com I always give credit in the source using java doc. I don't steal code and pass it off as my own. Tzu you gave me the answer which is what I needed so that I can study the code and learn from it. Thanks for answering my question.

@Hunter, we manually create a couple cards and add it to the deck. This is an intro programming class so we are taking it one step at a time. Thanks for the tip tho.

Thanks to everyone else who replied as well. Much appreciated.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic