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

Java Program** Write a program that reads in a hand of cards and stores it in an

ID: 3738565 • Letter: J

Question

Java Program**

Write a program that reads in a hand of cards and stores it in an array of sets, one set for each suit.

Each card is represented using a pair of characters.

The first character represents the rank of the card: the digits 2 through 9 stand for themselves, and the letters T, J, Q, K, A stand for ten, jack, queen, king, and ace, respectively.

The second character denotes the suit: C, D, H, or S. Check for invalid cards and duplicate cards.

Your program must use function(s) and must be Object Oriented. Your function(s) must use arguments.

Explanation / Answer

//Main Class

import java.util.*;

class Main1{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

Set<Cards> cardRead = new HashSet<>();

while(true){

System.out.println("Select a card to add in deck(Rank,Suit).If finished enter 0.");

String read = s.nextLine();

if(read.compareTo("0")==0)

break;

Cards newCard = new Cards(read);

if(cardRead.contains(newCard))

System.out.println("Dupilcate Card");

else cardRead.add(newCard);

}

}

}

//Cards Class

import java.util.*;

public class Cards {

private char rank;

private char suit;

public void cardCheck(char a, char b){

if(a!= 'A'&& a!='2' && a!='3' && a!='4' && a!='5' && a!='6' && a!='7' && a!='8' && a!='9' && a!='T' && a!='J'&& a!='Q' && a!='K' )

{

System.out.println("Invalid Card");

}

else if(b!='C'&& b!='D' && b!='H' && b!='S')

{

System.out.println("Invalid Card");

}

else

{

rank = a;

suit = b;

}

}

public Cards(String input){

cardCheck(input.charAt(0),input.charAt(1));

}

public Cards(){

}

public char getRank(){

return rank;

}

public char getSuit(){

return suit;

}

@Override

public boolean equals(Object o){

Cards card = (Cards) o;

if(hashCode()==card.hashCode()){

return true;

}

else

return false;

}

@Override

public int hashCode(){

return Objects.hash(rank,suit);

}

}

// I hope it is understandable and met your expectations. You can use try catch statements for more stability. I didnt //have enough time to add them but the program works just fine if the user inputs in the given format. All the best !!