0% found this document useful (0 votes)
9 views2 pages

Hw#7

The document provides instructions for modifying a card game's ranking system to prioritize Aces over Kings in the compareTo method and suggests an alternative approach by changing the RANKS array. It also includes methods to determine if a hand of cards is a flush or a royal flush, detailing the conditions for each case. The hasFlush method checks if all cards share the same suit, while the hasRoyal method verifies the presence of a specific combination of cards in the same suit.

Uploaded by

2cdg5bbkvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Hw#7

The document provides instructions for modifying a card game's ranking system to prioritize Aces over Kings in the compareTo method and suggests an alternative approach by changing the RANKS array. It also includes methods to determine if a hand of cards is a flush or a royal flush, detailing the conditions for each case. The hasFlush method checks if all cards share the same suit, while the hasRoyal method verifies the presence of a specific combination of cards in the same suit.

Uploaded by

2cdg5bbkvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Exercise 12-2) In some card games, Aces are ranked higher than Kings.

Modify the
compareTo method to implement this ordering.

Instead of modifying the compareTo method, you can also change the RANKS array so Aces
are ranked higher than Kings. Show me how you would do that.

public class Card implements Comparable<Card>{


public static final String[] RANKS = {
null, "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"
};
public static final String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};

public int compareTo(Card other) {


int rankIndex = Arrays.asList(RANKS).indexOf(this.rank);
int otherRankIndex = Arrays.asList(RANKS).indexOf(other.rank);

if (rankIndex == otherRankIndex) {
return this.suit.compareTo(other.suit);
} else if (rankIndex == 1 && otherRankIndex != 1) {
return 1;
} else if (rankIndex != 1 && otherRankIndex == 1) {
return -1;
} else {
return Integer.compare(rankIndex, otherRankIndex);
}
}
}

2.A flush is a hand of 5 cards when the suit is the same. Write a method hasFlush that takes in
an array of 5 cards and return 1 when the hand is a flush.

public static int hasFlush(Card[] cards) {


String suit = cards[0].suit;
for (int i = 1; i < cards.length; i++) {
if (!cards[i].suit.equals(suit)) {
return 0;
}
}
return 1;
}

If any card has a different suit, the method returns 0 (false). If all cards have the same suit, the
method returns 1 (true).

3. A “royal flush” includes the Ace, King, Queen, Jack, and 10 (all in the same suit). Write a
method called hasRoyal that determines whether an array of cards contains a royal flush.

public static int hasRoyal(Card[] cards) {


Arrays.sort(cards);
String suit = cards[0].suit;
if (cards[0].rank.equals("Ace") &&
cards[1].rank.equals("10") &&
cards[2].rank.equals("Jack") &&
cards[3].rank.equals("Queen") &&
cards[4].rank.equals("King") &&
cards[0].suit.equals(suit)) {
return 1;
} else {
return 0;
}
}

You might also like