This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A method that removes the first card from the deck and returns it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A method that removes the first card from the deck and returns it?" Watch "A method that removes the first card from the deck and returns it?" New topic
Author

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

Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
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

Joined: Oct 01, 2001
Posts: 2834
    
  11

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

Joined: Mar 24, 2010
Posts: 1
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.


"Courage is grace under pressure" - Ernest Hemingway
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Welcome to the Ranch, Sun Tzu
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@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

Joined: Mar 13, 2009
Posts: 492

Do you ever build a deck of cards?

-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You mean the Deck class in his first post?? Please read the topic first then ask questions.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Feb 10, 2010
Posts: 15
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A method that removes the first card from the deck and returns it?
 
Similar Threads
variable returns "null" value in output
Constructor problem.
How to test my code for proper function?
check my code please?
newbie help please with arrays