• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Sun Tzu
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you ever build a deck of cards?

-Hunter
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic