Question: Update the deck class to include the following override methods: equals(otherObject:Object):equals compareTo(otherDeck:Deck):int clone():Deck For the compareTo override method, you should simply compare by the lengths
Update the deck class to include the following override methods:
equals(otherObject:Object):equals
compareTo(otherDeck:Deck):int
clone():Deck
For the compareTo override method, you should simply compare by the lengths of the decks. (The deck with more cards is 'larger')
For the equals override method, you can implement how you think Decks should be compared, just be sure to include in comments for the method:
A. equal if the same cards, IN THE SAME EXACT ORDER. Two 'new' unshuffled decks are equal. If one is shuffelled, then they are not equal.
B. equal if the two Deck objects being compared have the SAME cards, even if different order. Two full decks would be equal, even if one is shuffled. ex. A deck missing a King of Clubs is not the same (not equal) as a deck missing a Queen of Diamonds. However, two decks both missing a Queen of Hearts would be equal.
The clone method should be a deep copy and not a shallow copy.
Build a tester application to test these new methods.
Submit BOTH
a. an updated Deck.java file and
b. a tester application file (e.g. DeskTester.java).
Deck Class:
import java.util.ArrayList; import java.util.Collections; public class Deck { private ArrayList deck; public Deck(){ deck = new ArrayList<>(); for(int suit = Card.CLUBS; suit <= Card.SPADES; suit++) { for(int rank = Card.ACE; rank <= Card.KING; rank++) { deck.add( new Card(rank, suit) ); } } } public String toString() { String deckString = deck.size() + " cards in the deck: "; int count = 1; //for each Card c in the deck for( Card c: deck) { deckString += count++ + ": " + c + " "; //put in groups of 13 // if( count > 1 && count % 13 == 1 ) { // deckString += " "; // } } return deckString; } /** * Go through the entire deck and swap a card with another at a random location. */ public void shuffle() { Collections.shuffle(deck); /** Homebrewed version of the shuffle class //if deck does not have more than one card ... shuffle it if( deck.size() > 1) { //go through entire deck for(int i = 0; i < this.deck.size()-1; ++i ) { //pull card out at each card index i Card temp = deck.remove(i); //note - deck has one less card in it ... int randIndex = (int) (Math.random()*deck.size()); //move the random card to index i deck.add(i, deck.remove(randIndex) ); //put the card back into the deck at random index deck.add(randIndex, temp); } } */ } public Card draw() { return this.deck.remove(0); } public Card[] deal(int numCardsToDeal) { Card[] temp = new Card[numCardsToDeal]; for(int i = 0; i < temp.length; i++) { temp[i] = this.draw(); } return temp; } public int size() { return this.deck.size(); } public void addCard(Card c) { this.deck.add( c ); } public void addCards(Card[] cards) { for(Card c: cards) { this.deck.add( c ); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
