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
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);
}
}
Get Answers For Free
Most questions answered within 1 hours.