Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I NEED help with this code. This is how the output is supposed to come out: Plac

ID: 3744231 • Letter: I

Question

I NEED help with this code.

This is how the output is supposed to come out:

Place the first card in the hand

For each additional card, do the following:

If the card is of a suit that has not appeared previously

      Place it to the right of all the other cards

Otherwise, do the following:

      If the value is higher than all the previous cards of that suit

             Place it to the right of the other cards of that suit

      Otherwise

Place it immediately to the left of the first card of that suit with a higher value

This is my code:

Card.java:

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() {

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.java:

import java.util.ArrayList;

class Deck {

public static final int NCARDS = 52;

private ArrayList<Card> deckOfCards;

public Deck() {

deckOfCards = new ArrayList<Card>();

String suits[] = {"Spade", "Club","Heart", "Diamond"};

String faceValue;

int count = 0;

for (int i = 0; i < 4; i++) {

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.add(new Card(faceValue, suits[i]));

}

}

}

public void printDeck() {

for (int i = 0; i < NCARDS; i++) {

System.out.println(deckOfCards.get(i).tostring());

}

}

public Card getCard(int num) {

return deckOfCards.get(num);

}

}

*********************************************************************************************

Hand.java:

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Random;

import java.util.Set;

class Hand {

private ArrayList<Card> handOfCards;

int number;

public Hand(int number) {

this.number = number;

Deck d = new Deck();

handOfCards = new ArrayList<Card>();

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.add(d.getCard(itr.next()));

}

}

public void printHandOfCards() {

for (int i = 0; i < number; i++)

System.out.println(handOfCards.get(i).tostring());

}

}

*******************************************************************************************

TestHand.java:

import java.util.Scanner;

public class TestHand {

static final int NUMBER_OF_CARDS_IN_HAND=5;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String printAnother = "Y";

while(printAnother.equalsIgnoreCase("Y")) {

printHand();

System.out.println("Do you want to see another hand? <Y/N>");

printAnother=sc.next();

}

}

public static void printHand(){

Hand hand = new Hand(NUMBER_OF_CARDS_IN_HAND);

System.out.println("Hand: ");

hand.printHandOfCards();

}

}

Explanation / Answer

import java.util.ArrayList;


import java.util.Random;


class Hand {

private ArrayList<Card> handOfCards;

int number;
int cardsInHand;

public Hand(int number) {

this.number = number;

Deck d = new Deck();
Card cardToAdd;

handOfCards = new ArrayList<Card>();

Random random = new Random();
cardsInHand=0;
  
//Set set = new HashSet<Integer>(number);
boolean added=false;//keeps track whether card is added or not
int posToInsert=-1;
//int cardToAdd;
while (cardsInHand < number)
{
added=false;
posToInsert=-1; //pos to insert
cardToAdd=d.getCard(random.nextInt(52));
int pos;
  
  
//while (set.add(random.nextInt(52)) != true);
if(cardsInHand==0)//if the first card
handOfCards.add(cardToAdd);
else
{
  
for(pos=0;pos<cardsInHand;pos++)//iterate to find exact position to insert
{
Card tempCard=handOfCards.get(pos);
if(tempCard.getSuit().equals(cardToAdd.getSuit()))//if same suit
{
if(getValue(tempCard.getFaceValue())<=getValue(cardToAdd.getFaceValue()))//if facevalue of previous card is less
{
  
//handOfCards.add(pos+1,cardToAdd); //add to next position
posToInsert=pos+1;
}
else
{
posToInsert=pos;
//shiftCards(pos);   
break;
}
}else if(posToInsert>=0) //to sort within suit eq: 9 9 10
/*
if cards in hand are 9 9 .We want to insert 10 .If this statement is not there card sequence becomes 9 10 9.
So we need to iterate and find the exact location to make the sequence 9 9 10
*/
break;
}
if(!added)
{
handOfCards.add(pos,cardToAdd);
added=true;
}
}
cardsInHand++;

}

  
}

public void printHandOfCards() {

for (int i = 0; i < number; i++)

System.out.println(handOfCards.get(i).tostring());

}

  
private int getValue(String fv) { //get the exact numerical value of a card   
switch(fv){
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
return Integer.parseInt(fv);
}
}

}