Write an equals method in the Card class. Two cards are equal if they have the same rank and the same suit. Your equals method must be the standard Java equals method:

computer science


Deck of Cards



The project includes two classes:


·         Card – a Card object is a standard (non-graphical) playing card.

·         Deck – maintains a list of Card objects.


You will only modify the Card class for Problem 1.  All other work will be done in the Deck class.


Problem 1: Write an equals method in the Card class.  Two cards are equal if they have the same rank and the same suit.  Your equals method must be the standard Java equals method:


public boolean equals(Object obj)


Problem 2: In the Deck class, I have already included an initDeck method to fill the list with 52 cards.  When you create a Deck object, the constructor will call the initDeck method to ensure that the deck starts will all 52 cards. 


Write the printDeck method.  This should be very easy.  All it does is traverse the list and print all the cards on the Terminal screen.


TEST YOUR METHOD by adding a main method to your Deck class that creates a Deck object and calls the printDeck method.  Be sure to test each method in this lab.


Problem 3: Write the shuffle method.  You may use the following algorithm:


            Traverse the list of cards:

                        For each card, move it to a randomly selected index


You move the card by removing it from the list and adding it back into the list at a different index.  You can use the ArrayList add method that lets you specify where to add the object.  (Google “Java ArrayList” and look at the Oracle page on the ArrayList to find the correct add method.)


Question: How do you generate random numbers using Java?

Answer: You don’t.  Computers can’t do anything randomly.  Ha!

Question: Okaaay…how do you generate pseudo-random numbers using Java?

Answer: Ah, that’s easy enough.  Just use the Random class.


You can read about the Random class and figure out how to use it to generate the random numbers you need: http://docs.oracle.com/javase/8/docs/api/java/util/Random.html.


Problem 4: Write the getCard method that just returns the card at the top of the deck.  Be sure to remove the card from the deck as well.


Problem 5: Write the getManyCards method that returns an ArrayList of cards.  The parameter specifies how many cards to include in the list.  The cards in the list should be taken from the top of the deck (you can call the getCard method for each card).  Be sure to remove the cards from the deck as well.


Note that this method creates a new list of cards.  It should not return the cardDeck list.


Related Questions in computer science category