Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

test case: public void testHandSequenceLength ( ) { deck = new Deck ( ) ; hand 1 = new Hand ( Amina ,

test case:
public void testHandSequenceLength(){
deck = new Deck();
hand1= new Hand("Amina",1, deck);
assertEquals(1, hand1.sequenceLength());
deck = new Deck();
hand1= new Hand("Nina",52, deck);
assertEquals(13, hand1.sequenceLength());
ArrayList cards = new ArrayList();
cards.add(new Card(new Rank("Two",'2',2), new Suit("Hearts",'H', "red", 1)));
cards.add(new Card(new Rank("Four",'4',4), new Suit("Hearts",'H', "red", 1)));
cards.add(new Card(new Rank("King",'K',13), new Suit("Hearts",'H', "red", 1)));
cards.add(new Card(new Rank("Eight",'8',8), new Suit("Hearts",'H', "red", 1)));
cards.add(new Card(new Rank("Three",'3',3), new Suit("Clubs",'C', "black", 3)));
cards.add(new Card(new Rank("Queen",'Q',12), new Suit("Hearts",'H', "red", 1)));
hand1= new Hand("Amina", cards);
assertEquals(3, hand1.sequenceLength());
cards.add(new Card(new Rank("Ace",'A',14), new Suit("Clubs",'C', "Black", 1)));
hand1= new Hand("Amina", cards);
assertEquals(4, hand1.sequenceLength());
cards.add(new Card(new Rank("Jack",'J',11), new Suit("Hearts",'H', "red", 1)));
cards.add(new Card(new Rank("Ten",'X',10), new Suit("Clubs",'C', "black", 3)));
cards.add(new Card(new Rank("Nine",'9',9), new Suit("Hearts",'H', "red", 1)));
hand1= new Hand("Amina", cards);
assertEquals(7, hand1.sequenceLength());
currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName();
}
my code isn't passing the test cases. the question says to return the length of the longest "sequence: of cards in the hand.
public int sequenceLength(){
if (cardsInHand.isEmpty()){
return 0;
}
arrange();
ArrayList ranks = new ArrayList<>();
for (Card card : cardsInHand){
ranks.add(card.rank.value);
if (card.rank.value ==14){
ranks.add(1);
}
}
ranks.sort(null);
int maxLength =1;
int currentLength =1;
for (int i =1; i < ranks.size(); i++){
if (ranks.get(i)== ranks.get(i -1)+1){
currentLength++;
} else if (ranks.get(i)!= ranks.get(i -1)){
maxLength = Math.max(maxLength, currentLength);
currentLength =1;
}
}
maxLength = Math.max(maxLength, currentLength);
return maxLength;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions