The Assignment Card players typically like to keep the cards in their hand sorte
ID: 3744027 • Letter: T
Question
The Assignment Card players typically like to keep the cards in their hand sorted by suit and within each suit by face value Assuming that a hand consists of 13 cards, write a Java program to insert each card into an initially empty hand in the proper order, so that there is no need to sort the entire hand. The order of the suits is unimportant but within each suit the order of the cards should be 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A (i.e. the Ace is always the high card) To receive credit for this assignment, you must use the algorithm given below. II. The Card Class Begin by creating a class to model a playing card. Card objects know their face value (aka: "rank") and their suit (Spades, Hearts, Diamonds, Clubs). Your Card class will have: 1. a constructor that creates a Card with a given rank and suit 2. accessor methods that return the rank and suit 3 a tostring method that returns a String representation of a card as shown in these examples As, 10*, 7*, QV To get th symbols for the suits, use the escape sequences for the Unicode characters: u2660" (Spade) u2663" (Club) "u2665" (Heart) "u2666" (Diamond)
Explanation / Answer
ANS:
CODE IS BELOW"
------------------------------------------------Card Class--------------------------------------------------------------
public class Card {
String faceValue;
String suit;
public Card(String faceValue, String suit) {
super();
this.faceValue = faceValue;
this.suit = suit;
}
public String getFaceValue() {
return faceValue;
}
public String getSuit() {
return suit;
}
public String tostring(Card c){
String faceValue = c.getFaceValue();
String suit = c.getSuit();
if(suit.equals("Spade"))
suit="u2660";
if(suit.equals("Club"))
suit="u2663";
if(suit.equals("Heart"))
suit="u2665";
if(suit.equals("Diamond"))
suit="u2666";
return faceValue + suit;
}
}
------------------------------------------Deck Class------------------------------------------------------------
class Deck {
public static final int NCARDS = 52;
private Card[] deckOfCards;
public Deck(){
deckOfCards = new Card[NCARDS];
String suit="";
String faceValue;
int count =0;
for(int i=1;i<=4;i++){
if(i==1)
suit="Spade";
if(i==2)
suit="Club";
if(i==3)
suit="Heart";
if(i==4)
suit="Diamond";
for(int j=1;j<=13;j++){
switch (j) {
case 1:
faceValue = "2";
break;
case 2:
faceValue = "3";
break;
case 3:
faceValue = "4";
break;
case 4:
faceValue = "5";
break;
case 5:
faceValue = "6";
break;
case 6:
faceValue = "7";
break;
case 7:
faceValue = "8";
break;
case 8:
faceValue = "9";
break;
case 9:
faceValue = "10";
break;
case 10:
faceValue = "J";
break;
case 11:
faceValue = "Q";
break;
case 12:
faceValue = "K";
break;
case 13:
faceValue = "A";
break;
default:
faceValue = "Invalid";
break;
}
deckOfCards[count++] = new Card(faceValue,suit);
}
}
}
public void printDeck(){
for(int i=0;i<NCARDS;i++){
System.out.println(deckOfCards[i].tostring(deckOfCards[i]));
}
}
public Card getCard(int num){
return deckOfCards[num];
}
}
---------------------------------------------------Hand Class---------------------------------------------------------
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
class Hand{
private Card[] handOfCards;
int number;
public Hand(int number){
this.number=number;
Deck d= new Deck();
handOfCards = new Card[number];
Random random = new Random();
Set set = new HashSet<Integer>(number);
while(set.size()< number) {
while (set.add(random.nextInt(52)) != true);
}
assert set.size() == number;
Iterator<Integer> itr = set.iterator();
int count = 0;
while(itr.hasNext()){
handOfCards[count++] = d.getCard(itr.next());
}
}
public void printHandOfCards(){
for(int i=0;i<number;i++)
System.out.println(handOfCards[i].tostring(handOfCards[i]));
}
}
-----------------------------------------------------------------------------------------------------------------------------
create one Sorting method in Hand class.
THANK YOU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.