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

JAVA BASED QUESTION. public void Result() { //Arrays for suits and cardValues Ar

ID: 647405 • Letter: J

Question

JAVA BASED QUESTION.

public void Result() {
        //Arrays for suits and cardValues
        ArrayList<Integer> cardValuesList = new ArrayList<>();
        ArrayList<String> suitsList = new ArrayList<>();
      
        //enhanced For loop to figure out what kind of hand we have
        for (String card : Hand)
        {
            char data[] = card.toCharArray();
            //Variables to convert so we can compare
            int cardValue = 0;
            String suit = "";
            //Case Structure to assign the card a value since i used a string for my array
            switch (data[0]) {
                case 'J':
                    cardValue = 11;
                    /*I GOOGLED MY ASS OFF ON HOW I SHOULD COMPARE THE CARDS! Learned
                    that break will "break" you out of the loop so if the char = J it will break
                    from the case structure.
                    */
                    break;
                case 'Q':
                    cardValue = 12;
                    break;
                case 'K':
                    cardValue = 13;
                    break;
                case 'A':
                    cardValue = 14;
                    break;
                default:
                /*Another thing i googled found the information at:
                        http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java
                    in simple terms is saying tha cardValue = the STRING value that was in the array.*/
                    cardValue = Integer.parseInt(card.substring(0, 1));
                    break;
            }
            //if statement if the card was a 10... Didnt know how else to write it.
            if (data[1] == '0') {
                cardValue = 10;
                data[1] = data[2];
            }
            //Case Structure to turn the data from the array into usable information so they can be compared.
            switch (data[1]) {
                case '?':
                    suit = "Heart";
                    break;
                case '?':
                    suit = "Black";
                    break;
                case '?':
                    suit = "Dimand";
                    break;
                case '?':
                    suit = "Spade";
                    break;

            }
           /*Loading Seperate arrays... Probably a way i should have done it from the
            beginning but I thought 1 step at a time.
            */
            cardValuesList.add(cardValue);
            suitsList.add(suit);
        }

       HashMap<Integer, Integer> cardValue = new HashMap<>();
        for (int index : cardValuesList)
        {
            if (cardValue.get(index) == null)
            {
                cardValue.put(index, 1);
            }//if statement
            else {
                cardValue.put(index, cardValue.get(index) + 1);
            }//else statement
        }//foor loop


        /*Initialized payout and message for the kind of hand you have
        if you dont have any hand your payout will be 0 and message will be no pair.
        */
        int payOut = 0;
        String message="no pair";
       
        if (isSame(cardValue.values(), 2)) {
            payOut = 1;
            message="one pair";
        }
        if (isTwoPair(cardValue.values())) {
            payOut = 2;
            message="two pairs";
        }

        if (isSame(cardValue.values(), 3)) {
            payOut = 3;
            message="Three of Kind";
        }
        if (isSame(cardValue.values(), 4)) {
            payOut = 25;
            message="Four of Kind";
        }

        Collections.sort(cardValuesList);
        if (isStraight(cardValuesList)) {
            payOut = 4;
            message="Straight";
        }
        if (isFlush(suitsList)) {
            payOut = 5;
            message="Flush";
        }

        if (isSame(cardValue.values(), 2) && isSame(cardValue.values(), 3)) {
            payOut = 6;
            message="Full House";
        }
        if (isStraight(cardValuesList) && isFlush(suitsList)) {
            payOut = 50;
            message="Straight Flush";
        }
        if (isRoyal(cardValuesList) && isFlush(suitsList)) {
            payOut = 250;
            message="Royal Flush";
        }
        System.out.println("You have : " + message);
        System.out.println("Payout : " + payOut);

    }

This is a poker program. this paricular method is to compare the cards to see what kind of hand the user has.

I bolded out the hashmap in this program. could anyone tell me what this is doing? What is the benefit to using a hashmap?

Could there be a different structure or is this the easiest way.

Explanation / Answer

Here,we are looking at all the values stored in the arrayliost CardvaluesList one by one.
If, the element is not mapped to any value in the HashMap cardValue, we add the element as key mapped to value 1.
If the element is present already in the HashMap, we add the element as key mapped to the value 'previous value + 1'

A benefit of using HashMaps is that Hashmaps makes search faster by sorting the data stored.


An alternative for hashmaps here can be Hashtables