java assistance Create a class named Card (this will represent a standard playin
ID: 3860762 • Letter: J
Question
java assistance
Create a class named Card (this will represent a standard playing card). The fields for the Card are the suit, the face (what you see, ie: “2”,”3” “K”) and a value. The Jack, Queen and King have a value of 10, the Ace has a value of 11.
Write an equals( ) method based on the suit and face, a compareTo( ) method based on the value field and a toString( ) method along with the constructor method, that takes a value for each of the three fields, and get( ) methods.
Then write a class named Deck. This class will represent a deck of 52 playing cards. The constructor method must create 52 Card instances to populate the array.
Write a toString( ) method that returns the contents in a textual format of the entire deck of cards. Use the StringBuilder class to build the string, then convert to a String by using the constructor of the String class that accepts a StringBuilder reference.
Test your class by writing a short driver that creates a Deck instance and prints the contents of the Deck instance.
Then, in the Deck class write a shuffle method that swaps elements in the array in a loop that repeats 1000 times.
Test this in the driver again by printing the deck after calling the shuffle( ) method.
Explanation / Answer
// Please find the Required solution import java.util.Random; //Card class class Card { // instance variables String suit; String face; Integer value; // constructor public Card( String suit, String face, Integer value ) { this.suit = suit; this.face = face; this.value = value; } // get methods public String getSuit() { return suit; } public String getFace() { return face; } public Integer getValue() { return value; } // equals method based on suit and face @Override public boolean equals( Object o ) { if( this == o ) return true; if( o == null || getClass() != o.getClass() ) return false; Card card = (Card) o; if( suit != null ? !suit.equals(card.suit) : card.suit != null ) return false; return face != null ? face.equals(card.face) : card.face == null; } // compare based on value public int compareTo( Card other ) { return this.getValue().compareTo(other.getValue()); } // to string method @Override public String toString() { return "Card{" + "suit='" + suit + ''' + ", face='" + face + ''' + ", value='" + value + ''' + '}'; } } class Deck { Card[] cards = new Card[52]; public Deck( Card[] cards ) { this.cards = cards; } @Override public String toString() { StringBuffer buffer = new StringBuffer(); for( Card card : cards ) { buffer.append(card).append(" "); } return "Deck{" + "cards=" + new String(buffer) + '}'; } // shuffle cards randomly Random random; public void shuffle() { if( random == null ) random = new Random(); int count = this.cards.length; for( int i = count; i > 1; i-- ) { swap(this.cards, i - 1, random.nextInt(i)); } } private void swap( Card[] array, int i, int j ) { Card temp = array[i]; array[i] = array[j]; array[j] = temp; } } public class Driver { public static void main( String[] args ) { String[] suits = { "diamonds", "clubs", "hearts", "spades" }; Card[] cards = new Card[52]; // for each suit for( int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.