Question

in java Write a test class with two unit tests that test different cases for the...

in java

Write a test class with two unit tests that test different cases for the hasSeq() function described below:
class Hand {
    // Add Card to Hand
    public void add(Card card);
    // Returns true if hand has 3 Card’s of consecutive ranks of the same suit
    // “consecutive” would mean something like 9, 10 and 11
     public boolean hasSeq();
}
class Card {
    // return suit of card: Spades, Hearts, Diamonds or Clubs
    public String suit();
    // return rank of card: rank of 2-10 is just the number,
    // 11 for Jack, 12 for Queen, 13 for King, 14 for Ace
    public int rank();
}
class TestHand { // Your test code goes here

Homework Answers

Answer #1

1.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Hand {
        private List<Card> handCardList = new ArrayList<>();
    // Add Card to Hand
    public void add(Card card) {
        this.handCardList.add(card);
    }
    
    // Returns true if hand has 3 Card’s of consecutive ranks of the same suit
    // “consecutive” would mean something like 9, 10 and 11
     public boolean hasSeq() {
         Collections.sort(handCardList);
         for(int i = 0 ; i < handCardList.size() -2 ; i++) {
                         if(handCardList.get(i).suit().equals(handCardList.get(i+1).suit()) && 
                                 handCardList.get(i).suit().equals(handCardList.get(i+2).suit()) &&
                                 (handCardList.get(i+1).rank() == (handCardList.get(i).rank()+1)) &&
                                                (handCardList.get(i+2).rank() == (handCardList.get(i).rank()+2))) {
                         return true;
                         }
                 }
         
         return false;
     }
}

2.

class Card implements Comparable<Card>{
        private String suit;
        
        private int rank;
        
        public Card(String suit,int rank) {
                this.suit = suit;
                this.rank = rank;
        }
        
    // return suit of card: Spades, Hearts, Diamonds or Clubs
    public String suit() {
                return this.suit;
        }
    // return rank of card: rank of 2-10 is just the number,
    // 11 for Jack, 12 for Queen, 13 for King, 14 for Ace
    public int rank() {
        return this.rank;
    }
    
    @Override
    public int compareTo(Card card) {
        if (this.suit == card.suit) {
            return Integer.valueOf(this.rank).compareTo(Integer.valueOf(card.rank));
        } else {
            return card.rank - card.rank;
        }
    }
}

3.

class TestHand { // Your test code goes here
        
        public static void main(String args[]) {
                TestHand testHand = new TestHand();
                testHand.testHandHasSeqTrue();
                testHand.testHandHasSeqFalse();
                
        }
        
        public void testHandHasSeqTrue() {
                Hand hand = new Hand();
                Card card1 = new Card("Spade",2);
                Card card2 = new Card("Spade",3);
                Card card3 = new Card("Spade",4);
                hand.add(card1);
                hand.add(card3);
                hand.add(card2);
                boolean result = hand.hasSeq();
                System.out.println("UnitTest Case 1 Hand hasSeq returning true :" + result);
        }
        
        public void testHandHasSeqFalse() {
                Hand hand = new Hand();
                Card card1 = new Card("Spade",1);
                Card card2 = new Card("Spade",5);
                Card card3 = new Card("Spade",6);
                hand.add(card1);
                hand.add(card3);
                hand.add(card2);
                boolean result = hand.hasSeq();
                System.out.println("UnitTest Case 2 Hand hasSeq returning false :" + result);
        }
}
Know the answer?
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
Similar Questions
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
A deck of cards has 52 cards with 4 suits (Hearts, Diamonds, Spades, and Clubs) and...
A deck of cards has 52 cards with 4 suits (Hearts, Diamonds, Spades, and Clubs) and 13 cards in each suit (Ace thru 10, Jack, Queen, and King; the last three are considered face cards). A card is drawn at random from a standard 52-card deck.   What is the probability that the card is a number card given the card is black (Spades and Clubs)? Group of answer choices 6/26 1 - 10/26 20/52 10/13
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards....
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards. There are 4 suits (Clubs, Hearts, Diamonds, and Spades) and there are 13 cards in each suit. Clubs/Spades are black, Hearts/Diamonds are red. There are 12 face cards. Face cards are those with a Jack (J), King (K), or Queen (Q) on them. For this question, we will consider the Ace (A) card to be a number card (i.e., number 1). Then for each...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class. Rules are the following: - Deck and PockerHand should be Iterable (note that you may just reuse one of the underlying iterators to provide this functionality) - Poker should implement 2 methods checking for two possible poker hands , see the comments inside the classes for details. Hint: remember how one can count the frequency of words in text? - whenever you...
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: (Round to 4 decimal places where possible) a) A 9 of...
he following question involves a standard deck of 52 playing cards. In such a deck of...
he following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: 1) A(n) 8 of Heart s? 2) A Club or Spade?...
Complete the implementation of the Card class. The two methods that you need to complete are...
Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals. CopareTo API: Compares this card to another card for order. This method imposes the following order on cards: Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
A standard deck of cards contains 4 suits (Hearts, Diamonds, Spades, and Clubs) each containing 13...
A standard deck of cards contains 4 suits (Hearts, Diamonds, Spades, and Clubs) each containing 13 ranks (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) for a total of 52 cards. In a typical game of poker, you are dealt five cards (without replacement) from a deck of 52 cards. How many Full Houses are possible? (A full house is a hand consisting of three of one rank and two of another. For instance, three...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT