Question: Main.java import java.util. * ; public class Main { private static String formatHandTypeName ( HandType type ) { return type.name ( ) . replaceAll (

Main.java
import java.util.*;
public class Main {
private static String formatHandTypeName(HandType type){
return type.name()
.replaceAll("([a-z])([A-Z])","$1 $2")
.replaceAll("([A-Z])([A-Z][a-z])","$1 $2")
.replace("Of A","of a");
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the seed value : ");
int seed = scanner.nextInt();
Deck deck = new Deck(seed);
System.out.println();
for (int i =0; i 5; i++){
deck.shuffle(); // Updated to match the new shuffle signature
Hand hand = deck.deal();
System.out.println(""+ hand);
}
System.out.print("Enter the number of hands : ");
int numHands = scanner.nextInt();
if (numHands =0){
System.out.println();
for (HandType type : HandType.values()){
System.out.printf("%-15s %10.5f%%%10d%n", formatHandTypeName(type),0.0,0);
}
} else {
Map handTypeCounts = new EnumMap>(HandType.class);
for (HandType type : HandType.values()){
handTypeCounts.put(type,0);
}
for (int i =0; i numHands; i++){
deck.shuffle(); // Updated to match the new shuffle signature
Hand hand = deck.deal();
HandType type = hand.getHandType();
handTypeCounts.put(type, handTypeCounts.get(type)+1);
}
System.out.println();
for (HandType type : HandType.values()){
int count = handTypeCounts.get(type);
double percentage =(count *100.0)/ numHands;
System.out.printf("%-15s %10.5f%%%10d%n", formatHandTypeName(type), percentage, count);
}
}
scanner.close();
}
}
Card.java
public class Card implements Comparable {
private final int rank;
private final int suit;
// Constructor to initialize card with a value between 0 and 51
public Card(int value){
this.rank = value /4+1; // A =1, J =11, Q =12, K =13
this.suit = value %4; // Spades =0, Hearts =1, Diamonds =2, Clubs =3
}
// Getter for suit
public int getSuit(){
return suit;
}
// Getter for rank
public int getRank(){
return rank;
}
// Override compareTo for sorting cards
@Override
public int compareTo(Card other){
return this.rank != other.rank ? this.rank - other.rank : this.suit - other.suit;
}
// Convert card to string representation (e.g.,"AS" for Ace of Spades)
@Override
public String toString(){
String[] suits ={"S","H","D","C"}; // Spades, Hearts, Diamonds, Clubs
String[] ranks ={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
return ranks[rank -1]+ suits[suit];
}
}
Deck.java
import java.util.Random;
import java.util.Arrays;
public class Deck {
private Card[] cards;
private Random random;
private int currentIndex; // Tracks the current position in the deck
public Deck(int seed){
this.random = new Random(seed); // Seeded random instance
initializeDeck();
}
private void initializeDeck(){
this.cards = new Card[52];
for (int i =0; i 52; i++){
cards[i]= new Card(i);
}
this.currentIndex =0; // Reset the index
}
public void shuffle(){
for (int i = cards.length -1; i >0; i--){
int j = random.nextInt(i +1); // Deterministic random number
Card temp = cards[i];
cards[i]= cards[j];
cards[j]= temp;
}
this.currentIndex =0; // Reset after shuffle
}
public void shuffle(int numSwaps){
for (int i =0; i numSwaps; i++){
int pos1= random.nextInt(52); // Deterministic random indices
int pos2= random.nextInt(52);
Card temp = cards[pos1];
cards[pos1]= cards[pos2];
cards[pos2]= temp;
}
this.currentIndex =0; // Reset after shuffle
}
public Hand deal(){
// Reset and shuffle the deck if there aren't enough cards to deal
if (currentIndex +5> cards.length){
initializeDeck();
shuffle();
}
Card[] dealtCards = Arrays.copyOfRange(cards, currentIndex, currentIndex +5);
currentIndex +=5; // Move the index forward by 5
return new Hand(dealtCards);
}
}
```
import java.util.*;
import java.util.stream.Collectors;
public class Hand {
private final Card[] cards;
public Hand(Card[] cards){
this.cards = Arrays.copyOf(cards, cards.length);
Arrays.sort(this.cards);
}
public Card[] getCards(){
return Arrays.copyOf(cards, cards.length);
}
public HandType getHandType(){
boolean isFlush = Arrays.stream(cards).map(Card::getSuit).distinct().count()==1;
boolean isStraight = Arrays.stream(cards).map(Card::getRank).distinct().count()==5&&
(cards[4].getRank()- cards[0].getRank()==4);
if (isFlush && isStraight) return HandType.StraightFlush;
if (isFlu ```
public enum HandType{
StraightFlush,
FourOfAKind,
FullHouse,
Flush,
Straight,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
\l|
```
Output differs. See highlights below.
Expected output 2:Compare output
Output differs. See highlights below.
Expected output
Main.java import java.util. * ; public class Main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!