Question

Complete the following assignment with the tester class included.

*If the text is too small in the pictures to read, open a new window and copy and paste the address below into the search bar, and go to that page for the same instructions in the pictures below:

This is the address for the same instructions in the pictures below:
https://www.chegg.com/homework-help/questions-and-answers/purpose-purpose-lab-design-write-many-complex-methods-working-arrays-array-list-objects-ma-q37244441?trackid=6abm6xXV

G e g Open A New Window On Your E purpose The Purpose Of Ths Le× + Get Homework Help With Chey I lero se ls ADHD M m 20 uesti

e Get Homework Help With Cheo.* e purpose The Purpose Of This Lat ! Open A New Window On Your B + × Chttps//www.chegg.com/hom

C Get Hamework Help With Chegs x P C Open A New Window On YourB + Furpose The Purpose Of This La X 四23 Books You Didn. G G CO

G Get Homework Help With Cheg/X e G Open A New Window On Your Purpose The Purpose Of This Let 、 -methods-working-arrays-array

e Get Hormework Help With Cheo × e Purpose The Purpose Of This Lei eOpenA NewWindowOn Your E (+ × С https://wwww.chegg.com/ho

G e Purpose The Purpose Of This Lex G Open A Nev Window On Your B [+ Cet Hamework Help With Chegs X X 四23 Books You Didn G G

Tester Class:

public class Tester
{
/**
* main() method
*/
public static void main(String[] args)
{
// No pair
System.out.println("No pair");
Hand noPair1 = new Hand(new Card(10, 3), new Card(3, 0), new Card(13, 2), new Card(5, 1), new Card(14, 3));
System.out.print(noPair1.toString());
System.out.print(printCategory(noPair1.category()));
System.out.println("\n");

// One pair
System.out.println("One pair");
Hand onePair1 = new Hand(new Card(12, 2), new Card(14, 3), new Card(10, 2), new Card(14, 0), new Card(11, 1));
System.out.print(onePair1.toString());
System.out.print(printCategory(onePair1.category()));
System.out.println("\n");

// Two pair
System.out.println("Two pair");
Hand twoPair1 = new Hand(new Card(12, 0), new Card(3, 0), new Card(14, 3), new Card(14, 1), new Card(3, 2));
System.out.print(twoPair1.toString());
System.out.print(printCategory(twoPair1.category()));
System.out.println("\n");

// Three of a kind
System.out.println("Three of a kind");
Hand threeOfAKind1 = new Hand(new Card(14, 3), new Card(14, 0), new Card(6, 2), new Card(2, 2), new Card(14, 1));
System.out.print(threeOfAKind1.toString());
System.out.print(printCategory(threeOfAKind1.category()));
System.out.println("\n");

// Straight
System.out.println("Straight");
Hand straight1 = new Hand(new Card(13, 3), new Card(10, 1), new Card(14, 1), new Card(12, 0), new Card(11, 2));
System.out.print(straight1.toString());
System.out.print(printCategory(straight1.category()));
System.out.println();
// aces low
System.out.println("Extra credit - aces low");
Hand straight2 = new Hand(new Card(4, 3), new Card(3, 1), new Card(5, 1), new Card(2, 0), new Card(14, 2));
System.out.print(straight2.toString());
System.out.print(printCategory(straight2.category()));
System.out.println("\n");

// Flush
System.out.println("Flush");
Hand flush1 = new Hand(new Card(8, 2), new Card(3, 2), new Card(11, 2), new Card(5, 2), new Card(14, 2));
System.out.print(flush1.toString());
System.out.print(printCategory(flush1.category()));
System.out.println("\n");

// Full house
System.out.println("Full house");
Hand fullHouse1 = new Hand(new Card(14, 3), new Card(9, 0), new Card(14, 0), new Card(14, 1), new Card(9, 3));
System.out.print(fullHouse1.toString());
System.out.print(printCategory(fullHouse1.category()));
System.out.println("\n");

// Four of a kind
System.out.println("Four of a kind");
Hand fourOfAKind1 = new Hand(new Card(14, 3), new Card(9, 3), new Card(14, 1), new Card(14, 2), new Card(14, 0));
System.out.print(fourOfAKind1.toString());
System.out.print(printCategory(fourOfAKind1.category()));
System.out.println("\n");

// Straight flush
System.out.println("Straight flush");
// (an aces high straight flush is a royal flush)
Hand straightFlush1 = new Hand(new Card(5, 1), new Card(2, 1), new Card(4, 1), new Card(6, 1), new Card(3, 1));
System.out.print(straightFlush1.toString());
System.out.print(printCategory(straightFlush1.category()));
System.out.println();
// aces low
System.out.println("Extra credit - aces low");
Hand straightFlush2 = new Hand(new Card(4, 3), new Card(3, 3), new Card(14, 3), new Card(5, 3), new Card(2, 3));
System.out.print(straightFlush2.toString());
System.out.print(printCategory(straightFlush2.category()));
System.out.println("\n");

// Royal flush
System.out.println("Royal flush");
Hand royalFlush1 = new Hand(new Card(13, 0), new Card(10, 0), new Card(14, 0), new Card(11, 0), new Card(12, 0));
System.out.print(royalFlush1.toString());
System.out.print(printCategory(royalFlush1.category()));
System.out.println("\n");

// Invalid hand
System.out.println("Invalid hand");
Hand invalidHand1 = new Hand(new Card(13, 0), new Card(7, 2), new Card(2, 3), new Card(10, 1), new Card(13, 0));
System.out.print(invalidHand1.toString());
System.out.println("\n");
}

/**
* Returns the category of a hand.
*
* @param category int category of a hand
* @return the category as a String
*/
public static String printCategory(int category)
{
String out = "";

switch (category) {
case Hand.ROYAL_FLUSH :
out = " Royal flush";
break;
case Hand.STRAIGHT_FLUSH :
out = " Straight flush";
break;
case Hand.FOUR_OF_A_KIND :
out = " Four of a kind";
break;
case Hand.FULL_HOUSE :
out = " Full house";
break;
case Hand.FLUSH :
out = " Flush";
break;
case Hand.STRAIGHT :
out = " Straight";
break;
case Hand.THREE_OF_A_KIND :
out = " Three of a kind";
break;
case Hand.TWO_PAIR :
out = " Two pair";
break;
case Hand.ONE_PAIR :
out = " One pair";
break;
}

return out;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

public class Tester
{
    public static void main(String[] args)
    {
        // No pair
        System.out.println("No pair");
        Hand noPair1 = new Hand(new Card(10, 3), new Card(3, 0), new Card(13, 2), new Card(5, 1), new Card(14, 3));
        System.out.print(noPair1.toString());
        System.out.print(printCategory(noPair1.category()));
        System.out.println("\n");

        // One pair
        System.out.println("One pair");
        Hand onePair1 = new Hand(new Card(7, 2), new Card(14, 3), new Card(2, 2), new Card(14, 0), new Card(5, 1));
        System.out.print(onePair1.toString());
        System.out.print(printCategory(onePair1.category()));
        System.out.println("\n");

        // Two pair
        System.out.println("Two pair");
        Hand twoPair1 = new Hand(new Card(12, 0), new Card(3, 0), new Card(14, 3), new Card(14, 1), new Card(3, 2));
        System.out.print(twoPair1.toString());
        System.out.print(printCategory(twoPair1.category()));
        System.out.println("\n");

        // Three of a kind
        System.out.println("Three of a kind");
        Hand threeOfAKind1 = new Hand(new Card(14, 3), new Card(14, 0), new Card(6, 2), new Card(2, 2), new Card(14, 1));
        System.out.print(threeOfAKind1.toString());
        System.out.print(printCategory(threeOfAKind1.category()));
        System.out.println("\n");

        // Straight
        System.out.println("Straight");
        Hand straight1 = new Hand(new Card(13, 3), new Card(10, 1), new Card(14, 1), new Card(12, 0), new Card(11, 2));
        System.out.print(straight1.toString());
        System.out.print(printCategory(straight1.category()));
        System.out.println();
        // aces low
        System.out.println("Extra credit - aces low");
        Hand straight2 = new Hand(new Card(4, 3), new Card(3, 1), new Card(5, 1), new Card(2, 0), new Card(14, 2));
        System.out.print(straight2.toString());
        System.out.print(printCategory(straight2.category()));
        System.out.println("\n");

        // Flush
        System.out.println("Flush");
        Hand flush1 = new Hand(new Card(8, 2), new Card(3, 2), new Card(11, 2), new Card(5, 2), new Card(14, 2));
        System.out.print(flush1.toString());
        System.out.print(printCategory(flush1.category()));
        System.out.println("\n");

        // Full house
        System.out.println("Full house");
        Hand fullHouse1 = new Hand(new Card(14, 3), new Card(9, 0), new Card(14, 0), new Card(14, 1), new Card(9, 3));
        System.out.print(fullHouse1.toString());
        System.out.print(printCategory(fullHouse1.category()));
        System.out.println("\n");

        // Four of a kind
        System.out.println("Four of a kind");
        Hand fourOfAKind1 = new Hand(new Card(14, 3), new Card(9, 3), new Card(14, 1), new Card(14, 2), new Card(14, 0));
        System.out.print(fourOfAKind1.toString());
        System.out.print(printCategory(fourOfAKind1.category()));
        System.out.println("\n");

        // Straight flush
        System.out.println("Straight flush");
        // (an aces high straight flush is a royal flush)
        Hand straightFlush1 = new Hand(new Card(5, 1), new Card(2, 1), new Card(4, 1), new Card(6, 1), new Card(3, 1));
        System.out.print(straightFlush1.toString());
        System.out.print(printCategory(straightFlush1.category()));
        System.out.println();
        // aces low
        System.out.println("Extra credit - aces low");
        Hand straightFlush2 = new Hand(new Card(4, 3), new Card(3, 3), new Card(14, 3), new Card(5, 3), new Card(2, 3));
        System.out.print(straightFlush2.toString());
        System.out.print(printCategory(straightFlush2.category()));
        System.out.println("\n");

        // Royal flush
        System.out.println("Royal flush");
        Hand royalFlush1 = new Hand(new Card(13, 0), new Card(10, 0), new Card(14, 0), new Card(11, 0), new Card(12, 0));
        System.out.print(royalFlush1.toString());
        System.out.print(printCategory(royalFlush1.category()));
        System.out.println("\n");

        // Invalid hand
        System.out.println("Invalid hand");
        Hand invalidHand1 = new Hand(new Card(13, 0), new Card(7, 2), new Card(2, 3), new Card(10, 1), new Card(13, 0));
        System.out.print(invalidHand1.toString());
        System.out.println("\n");
    }

    //return category of a hand.

    public static String printCategory(int category)
    {
        String out = "";

        switch (category) {
            case Hand.ROYAL_FLUSH :
                out = " Royal flush";
                break;
            case Hand.STRAIGHT_FLUSH :
                out = " Straight flush";
                break;
            case Hand.FOUR_OF_A_KIND :
                out = " Four of a kind";
                break;
            case Hand.FULL_HOUSE :
                out = " Full house";
                break;
            case Hand.FLUSH :
                out = " Flush";
                break;
            case Hand.STRAIGHT :
                out = " Straight";
                break;
            case Hand.THREE_OF_A_KIND :
                out = " Three of a kind";
                break;
            case Hand.TWO_PAIR :
                out = " Two pair";
                break;
            case Hand.ONE_PAIR :
                out = " One pair";
                break;
        }

        return out;
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Card
{
    private final int rank;
    private final int suit;
    private static final int LOW_RANK = 2;
    private static final int HIGH_RANK = 14;
    private static final int CLUBS = 0;
    private static final int SPADES = 3;


    public Card(int rankIn, int suitIn)
    {
        rank = rankIn;
        suit = suitIn;
        if(rank > HIGH_RANK || rank < LOW_RANK )
        {
            System.out.println(rankIn + " is an invalid rank...exiting...");
        }
        if(suit > SPADES || suit < CLUBS)
        {
            System.out.println(suitIn + " is an invalid suit...exiting...");
        }
    }


    public int getRank(){
        return rank;
    }

    public int getSuit()
    {
        return suit;
    }

    private String getStringRank()
    {
        String rOut = Integer.toString(rank);

        switch(rank)
        {
            case 11:
                rOut = "J";
                break;
            case 12:
                rOut = "Q";
                break;
            case 13:
                rOut = "K";
                break;
            case 14:
                rOut = "A";
                break;
        }

        return rOut;
    }


    private String getStringSuit()
    {
        String sOut = "";

        switch(suit)
        {
            case 0:
                sOut += '\u2663';
                break;
            case 1:
                sOut += '\u2662';
                break;
            case 2:
                sOut += '\u2661';
                break;
            case 3:
                sOut += '\u2660';
                break;
        }

        return sOut;
    }

    public String toString()
    {
        return String.format("%2s", getStringRank()) + String.format("%1s", getStringSuit());
    }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Arrays;

public class Hand
{
    private ArrayList<Card>cards;
    private static final int AMOUNT_CARDS = 5;
    private static final int SUITS = 4;
    private int suitCount[];
    public static final int NO_PAIR = 0;
    public static final int ONE_PAIR = 1;
    public static final int TWO_PAIR = 2;
    public static final int THREE_OF_A_KIND = 3;
    public static final int STRAIGHT = 4;
    public static final int FLUSH = 5;
    public static final int FULL_HOUSE = 6;
    public static final int FOUR_OF_A_KIND = 7;
    public static final int STRAIGHT_FLUSH = 8;
    public static final int ROYAL_FLUSH = 9;

    public Hand(Card first, Card second, Card third, Card fourth, Card fifth)
    {
        cards = new ArrayList<>();
        addCard(first);
        addCard(second);
        addCard(third);
        addCard(fourth);
        addCard(fifth);
        getDuplicates(cards);
    }

    public void addCard(Card c)
    {
        cards.add(c);
    }


    public Card getCard(int i)
    {
        return cards.get(i);
    }

    public void countSuits()
    {
        suitCount = new int [SUITS];
        for(int x = 0; x < AMOUNT_CARDS; x++)
            suitCount[getCard(x).getSuit()] += 1;
    }

    public int category()
    {
        int category = NO_PAIR;

        if(royalFlush())
            category = ROYAL_FLUSH;
        else if(straightFlush())
            category = STRAIGHT_FLUSH;
        else if(fourOfAKind())
            category = FOUR_OF_A_KIND;
        else if(fullHouse())
            category = FULL_HOUSE;
        else if(flush())
            category = FLUSH;
        else if(straight())
            category = STRAIGHT;
        else if(threeOfAKind())
            category = THREE_OF_A_KIND;
        else if(twoPair())
            category = TWO_PAIR;
        else if(onePair())
            category = ONE_PAIR;
        else
            category = NO_PAIR;

        return category;
    }


    public boolean noPair()
    {
        if(onePair() == false && twoPair() == false && threeOfAKind() == false && straight() == false && flush() == false && fullHouse() == false && fourOfAKind() == false && royalFlush() == false)
            return true;
        return false;
    }


    public boolean onePair()
    {
        CountRank cr = new CountRank(this);

        return cr.onePair();
    }

    public boolean twoPair()
    {
        CountRank cr = new CountRank(this);

        return cr.twoPair();
    }

    public boolean threeOfAKind()
    {
        CountRank cr = new CountRank(this);

        return cr.threeOfAKind();
    }

    public boolean straight()
    {
        CountRank cr = new CountRank(this);
        int min = 14;
        int max = 0;
        for (int x = 0; x < cards.size(); x++)
        {
            if(getCard(x).getRank() > max)
                max = getCard(x).getRank();
            if (getCard(x).getRank() < min)
                min = getCard(x).getRank();
        }

        return cr.straight(min,max);
    }

    public boolean flush()
    {
        final int FLUSH = 5;
        countSuits();
        for(int x = 0; x < suitCount.length; x++)
            if(suitCount[x] == FLUSH)
                return true;

        return false;
    }

    public boolean fullHouse()
    {
        CountRank cr = new CountRank(this);

        return cr.fullHouse();
    }

    public boolean fourOfAKind()
    {
        CountRank cr = new CountRank(this);

        return cr.fourOfAKind();
    }
    public boolean straightFlush()
    {
        if(flush() == true && straight() == true)
            return true;

        return false;
    }

    public boolean royalFlush()
    {
        CountRank cr = new CountRank(this);
        final int ROYAL_FLUSH = 5;
        final int CARD = 1;
        int count = 0;
        final int LAST_FIVE = cr.getRankCount().length;
        countSuits();
        for(int x = LAST_FIVE - 1; x >= LAST_FIVE - ROYAL_FLUSH; x--)
            if(cr.getRankCount()[x] == CARD)
                count++;
        if(flush() == true && count == ROYAL_FLUSH)
            return true;

        return false;
    }

    private boolean hasAce()
    {
        final int ACE = 14;

        return findRank(ACE);
    }


    private boolean findRank(int rank)
    {
        CountRank cr = new CountRank(this);
        cr.getRankCount();

        if(cr.getRankCount()[rank]>= 1)
            return true;

        return false;
    }

    public void getDuplicates(ArrayList<Card> array)
    {
        int count = 0;
        for(int x = 0; x < array.size(); x++)
            for(int y = x+1; y < array.size(); y++)
                if(array.get(x).getRank() == array.get(y).getRank() && array.get(x).getSuit() == array.get(y).getSuit())
                    count++;
        if(count > 0)
            System.out.println("Duplicate cards found!!!");
    }

    public String toString()
    {
        return cards.toString();
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Arrays;

public class CountRank
{
    private int rankCount[];
    private static final int RANKS = 15;
    private static final int CARDS = 5;
    private static final int PAIR = 2;
    private static final int TOAK = 3;
    private static final int STRAIGHT = 5;
    private static final int FOAK = 4;

    public CountRank(Hand h)
    {
        rankCount = new int [RANKS];
        for(int x = 0; x < CARDS; x++)
        {
            rankCount[h.getCard(x).getRank()] += 1;
        }

    }

    public int[] getRankCount()
    {
        return rankCount;
    }

    public Boolean onePair()
    {
        final int onePair = 1;
        int count = 0;

        for (int x = 0; x < rankCount.length; x++)
            if(rankCount[x] == PAIR)
                count++;

        if (count == onePair)
            return true;
        return false;
    }

    public Boolean twoPair()
    {
        int count = 0;

        for (int x = 0; x < rankCount.length; x++)
            if(rankCount[x] == PAIR)
                count++;

        if (count == PAIR)
            return true;

        return false;
    }

   public Boolean threeOfAKind()
    {
        for (int x = 0; x < rankCount.length; x++)
            if(rankCount[x] == TOAK)
                return true;
        return false;
    }

   public Boolean straight(int min,int max)
    {
        final int COUNT = 1;
        final int CONSEC = 4;
        int card = 0;

        if (max - min == CONSEC)
        {
            for(int x = 0; x < rankCount.length; x++)
                if(rankCount[x] == COUNT)
                    card++;
        }

        if(card == STRAIGHT)
            return true;

        return false;
    }

    public Boolean fullHouse()
    {
        if(threeOfAKind() == true && onePair() == true)
            return true;

        return false;
    }

    public Boolean fourOfAKind()
    {

        for (int x = 0; x < rankCount.length; x++)
            if(rankCount[x] == FOAK)
                return true;

        return false;
    }

    public String toString()
    {
        final int BEFORE = 2;
        final int AFTER = 3;
        final int SPACES = 3;
        String rankString = Arrays.toString(rankCount);
        StringBuilder sb = new StringBuilder(rankString);
        for (int x = BEFORE; x <sb.length(); x+=SPACES)
            sb.setCharAt(x,']');
        for (int x = AFTER; x < sb.length(); x+=SPACES)
            sb.setCharAt(x,'[');
        return sb.toString();
    }
}

血CardHandGame [-/ldeaProjects/CardHandGamel-,../src/CountRank.java [CardHandGame] src CountRank jxC Project Tester-java Card


Add a comment
Know the answer?
Add Answer to:
Complete the following assignment with the tester class included. *If the text is too small in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...

    need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards in a hand. * * @author (your name) * @version (a version number or a date) */ public class CountRank { // instance variables - replace the example below with your own private int rankCount[]; private Hand hand; private int count; /** * Constructor for objects of class CountRank */ public CountRank(Hand h) { // initialise instance variables hand = h; rankCount = new...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

  • Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods...

    Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods Program Specification: You are to develop a program to play a variation on a game of chance called single player Poker. Game Description: • There is a Player who starts with 100 chips, each worth $1.00. • There is a deck of 36 cards: – Each card has a number in the range of [1, 9] printed on it - 9 possible values /...

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT