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

Program 1d: FiveCards in Java. Hello Chegg, I have been tyring to complete this

ID: 3744965 • Letter: P

Question

Program 1d: FiveCards in Java.

Hello Chegg,

I have been tyring to complete this lab for quite some time now but have seemed to get stuck and was wodnering if I could get assistants to finsh the class. I also attached (the bottom) my own code, and was wondering if we could use it to finsh the rest of the lab.

Thank You so much! ( I always rate positively ) ..... :)

Objectives

Work with arrays that correspond to a standard poker card deck.

Use Random number generator.

Write various methods to convert index to a card suit and rank.

Assignment

Write an application called FiveCards.java that randomly selects five unique numbers from 0 to 51 which will represent individual cards in a standard 52-card four-suited poker deck of cards.

Methods

public static void main (String[] args)

Call initHand to initialize the one-dimensional "hand" of cards with 52 card deck, 5 card hand and seed 1111.

Call showHand to print the string representation of the number in the hand of cards array.

Call sameSuit to print any cards in the hand that have the same suit.

public static int[] initHand(int deckSize, int handSize, long randomGeneratorSeed)

This method returns a one-dimensional array of handSize integer values, each between 0 and deckSize.

For this program, an example of a hand array is:

This method should create a Random object seeded with randomGeneratorSeed once BEFORE any loop is entered.

Random rand = new Random(randomGeneratorSeed)

After this, rand.nextInt(deckSize) inside the loop will generate the next successive value.

Unique cards: you must handle the case where number is randomly generated that has already been used in the hand array. For example, `initHand(52, 5, 31) generates, in order, 20, 12, 20, …

public static void showHand(int[] hand)

This method displays each card on one line in the order it was dealt (placed into the hand array) by calling getCardValue for each card in the array.

public static void sameSuit(int[] hand)

This method calls getCardValue to display any two or more cards having the same suit. This will be as few as two cards or as many as five. This method will require some strategy. It should be able to detect any of the following scenarios.

Two or more cards of the same suit. A hand may have more multiples of more than one suit, for example, two Hearts cards and two Clubs.

A "full house" would consist of two cards of one suit and three cards of another suit.

A "flush" would be five cards all of the same suit.

Cards should be printed in ascending rank order (sort the hand array from smallest to largest).

public static String getIdentificationString()

This method returns a string containing "Program 1, Student Name"

public static String getCardValue(int cardNumber)

This method rerturns the value of a card having the form " of ". For example, "Ace of Hearts", "Two of Diamonds", "Queen of Spades."

The rank is determined by calling getRank and suit is determined by calling getSuit.

public static String getSuit(int cardNumber)

Returns a string containing the suit of the card where

public static String getRank(int cardNumber)

Returns a string containing the rank of the card where

Notes

import java.util.Arrays to use Arrays.sort(myHandArray) if desired

import java.util.Random for random numbers

Testing

Your program should run with a variety of inputs. Test as many as you can to find unique outputs! Focus on changing the seed value. Here are some examples: 1, 8, 81, 111, 1111, 11111, 31.

-----[MY OWN CODE, PLEASE USE TO FINISH THE PROBLEM, THANKS!]----

import java.util.Random;
import java.util.Arrays;

public class FiveCards {
public static int[] initHand(int deckSize, int handSize, long randomGeneratorSeed) {
   Random rand = new Random(randomGeneratorSeed);
   int [] cardType = new int [deckSize];
   for (int i = 0; i < deckSize; i++) {
       cardType [i] = rand.nextInt(deckSize);
   }
       return cardType;
   }
   public static void showHand(int[] hand) {
      
   }
   public static String getCardValue(int cardNumber) {
       String toYes = "Yo";
       return toYes;
   }
   public static String getSuit(int cardNumber) {
       String suitType = "";
       if (cardNumber > 13) {
           suitType = "Clubs";
       }
       if (cardNumber > 26) {
           suitType = "Diamonds";
       }
       if (cardNumber > 39) {
           suitType ="Hearts";
       }
       else if (cardNumber > 52) {
           suitType = "Spades";
       }
       return (suitType);
   }
   public static String getRank(int cardNumber) {
       String rankType ="";
       switch (cardNumber) {
       case 0: rankType = "Ace";
       break;
       case 1: rankType = "Two";
       break;
       case 2: rankType = "Three";
       break;
       case 3: rankType = "Four";
       break;
       case 4: rankType = "Five";
       break;
       case 5: rankType = "Six";
       break;
       case 6: rankType = "Seven";
       break;
       case 7: rankType = "Eight";
       break;
       case 8: rankType = "nine";
       break;
       case 9: rankType = "Ten";
       break;
       case 10: rankType = "Jack";
       break;
       case 11: rankType = "Queen";
       break;
       case 12: rankType = "King";
       break;
       }
       return rankType;
   }
   public static String getIdentificationString() {
       return "Program 1c, Eric Guevara";
   }
   public static void main (String[] args) {
      
   }
}

Array Index Number Value Corresponds to String value 0 2 Three of Clubs 1 16 Seven of Clubs 2 21 Nine of Diamonds 3 13 Ace of Diamonds 4 46 Eight of Spades

Explanation / Answer

I have completed the solution and it is working perfectely fine. There were some mistakes in your code but I have corrected them. Please give me an upvote.

import java.util.Random;
import java.util.Arrays;

public class FiveCards {
public static void main(String[] args)
{
int[] hand1=initHand(52,5,1111); //randomGeneratorSeed is 1111
showHand(hand1);
System.out.println();
sameSuit(hand1);
}
  
public static int[] initHand(int deckSize, int handSize, long randomGeneratorSeed) {
Random rand = new Random(randomGeneratorSeed);
int [] cardType = new int [handSize];
cardType[0]=-1; //initialising the values of the array to zero
cardType[1]=-1;
cardType[2]=-1;
cardType[3]=-1;
cardType[4]=-1;
for (int i = 0; i < handSize;) {
int temp = rand.nextInt(deckSize);
if(check(cardType,temp)){
cardType[i]=temp;
i++;
}
}
return cardType;
}

public static boolean check(int[] hand,int num) // a function to check if the values exist already
{
for(int i=0;i<5;i++)
{
if(hand[i]==num)
return false;
}
return true;
}

public static void showHand(int[] hand) {
for(int i=0;i<5;i++)
{
System.out.println(getCardValue(hand[i]));
}
}

public static String getCardValue(int cardNumber) {
String value=getRank(cardNumber);
String suit=getSuit(cardNumber);
String final_str=value + " of " + suit;
return final_str;
}
public static String getSuit(int cardNumber) {
String suitType = "";
if (cardNumber < 13) {
suitType = "Clubs";
}
else if (cardNumber < 26) {
suitType = "Diamonds";
}
else if (cardNumber < 39) {
suitType ="Hearts";
}
else if (cardNumber < 52) {
suitType = "Spades";
}
return suitType;
}
public static String getRank(int cardNumber) {
String rankType ="";
cardNumber=cardNumber%13;
switch (cardNumber) {
case 0: rankType = "Ace";
break;
case 1: rankType = "Two";
break;
case 2: rankType = "Three";
break;
case 3: rankType = "Four";
break;
case 4: rankType = "Five";
break;
case 5: rankType = "Six";
break;
case 6: rankType = "Seven";
break;
case 7: rankType = "Eight";
break;
case 8: rankType = "Nine";
break;
case 9: rankType = "Ten";
break;
case 10: rankType = "Jack";
break;
case 11: rankType = "Queen";
break;
case 12: rankType = "King";
break;
}
return rankType;
}
public static String getIdentificationString() {
return "Program 1c, Eric Guevara";
}

public static void sameSuit(int[] hand)
{
Arrays.sort(hand);
int clubs=0;
int diamonds=0;
int spades=0;
int hearts=0;
int[] club=new int[5];
int[] diamond=new int[5];
int[] spade=new int[5];
int[] heart=new int[5];
for(int i=0;i<5;i++)
{
if(hand[i]<13)
{
club[clubs]=hand[i];
clubs++;
  
}
else if(hand[i]<26)
{
diamond[diamonds]=hand[i];
diamonds++;
}
else if(hand[i]<39)
{
heart[hearts]=hand[i];
hearts++;
}
else if(hand[i]<52)
{
spade[spades]=hand[i];
spades++;  
}
}
if(clubs>=2)
{
for(int i=0;i<clubs;i++)
System.out.println(getCardValue(club[i]));
}
if(diamonds>=2)
{
for(int i=0;i<diamonds;i++)
System.out.println(getCardValue(diamond[i]));
}
if(hearts>=2)
{
for(int i=0;i<hearts;i++)
System.out.println(getCardValue(heart[i]));
}
if(spades>=2)
{
for(int i=0;i<spades;i++)
System.out.println(getCardValue(spade[i]));
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote