Question
For Java SortCard class [3 marks] Complete the comparator method compare so that the card objects are compared as follows: Where o1 has a higher
For Java
SortCard class [3 marks] Complete the comparator method compare so that the card objects are compared as follows: Where o1 has a higher value property than o2 return 1. Where o1 has a lower value property than o2 return -1; Where the value property of both o1 and o2 is the same return 0. Hint: Card has a method getNumericValue that will convert the String value of the card to an integer for easy comparison. In this implementation Aces are the highest value. Hand class [17 marks] Complete the getHandRank method. This should return the correct string from the ranks array of Hand based on the cards that have been dealt. Base your determination of the rank on the classic poker hands. See the code and the image below for a guide to examples of the 10 ranks these. Using the SortCards class you completed above the cards of the hand are sorted. This will make this task much easier! Again, use the main method in CardMain to test your code as you develop your solution. You should also make use of the dealSpecificCard method in deck. This will help you to deal specific hands that you can check your code against. Marks will be awarded for the correctness of your solution and the elegance of the code. When marked your code will be run against a test set of hands.
-------------------------------------------------------------------------------------
card.java
public class Card {
private String value;
private String suit;
public Card(String value, String suit) {
this.value = value;
this.suit = suit;
}
public int getNumericValue(){
switch (value){
case "K":
return 13;
case "Q":
return 12;
case "J":
return 11;
case "A":
return 14;
default:
return Integer.parseInt(value);
}
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
}
-------------------------------------------------------------------------------------
CardMain.java
public class CardMain {
public static void main(String[] args) {
Deck d = new Deck(true);
System.out.println(d);
// Hand h = new Hand();
// for(int i = 0; i
// h.addCard(d.deal());
// }
// System.out.println(h);
// System.out.println(h.getHandRank());
}
} ------------------------------------------------------------------------------------- Deck.java
import java.util.Collections;
import java.util.LinkedList;
public class Deck {
private LinkedList
Deck(boolean shuffled){
String[] suits = {"Clubs", "Hearts", "Diamonds", "Spades"};
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for(String s: suits){
for(String v: values){
deck.add(new Card(v, s));
}
}
if(shuffled){
Collections.shuffle(deck);
}
}
public Card deal(){
return deck.pop();
}
public Card dealSpecificCard(String value, String suit){
for(int i = 0; i Card c = deck.get(i); if(c.getValue().equals(value) && c.getSuit().equals(suit)){ return deck.remove(i); } } return null; } public String toString(){ String output = ""; for(Card c: deck){ if(c.getSuit().equals("Hearts") || c.getSuit().equals("Diamonds")) { output += "\u001B[31m[ " + c.getValue() + " , " + c.getSuit() + " ] \u001B[0m"; } else{ output += "[ " + c.getValue() + " , " + c.getSuit() + " ] "; } } return output; } } ------------------------------------------------------------------------------------- Hand.java import java.util.ArrayList; public class Hand { private ArrayList private final String[] ranks = {"Royal Flush", "Straight Flush", "4-of-a-kind", "Full House", "Flush", "Straight", "3-of-a-kind", "2 Pair", "1 Pair", "High Card"}; public void addCard(Card c){ if(hand.size() hand.add(c); } } public String getHandRank(){ if(hand.size() != 5){ return "Incorrect hand size"; } //sort the hand hand.sort(new SortCards()); //high card (None of the other hands match, the highest value of the card) //one pair ( a pair of cards with the same value e.g. 7D, 7H, 4S, 6H, 8H) //two pair (2 pairs of matched values e.g. 7D, 7H, 4S, 4C, 2D) //3 of a kind (3 cards with the same value and two others e.g. 7D, 7H, 7C, 2H, KS) //straight (A run of values in different suits e.g. 3H, 4D, 5H, 6C, 7S) //flush (All cards are in the same suit e.g. 3H, 7H, 9H, JH, KH) //full house (3 of a kind and a pair e.g. 7S, 7H, 7D, 4C, 4H) //4 of a kind (4 cards with the same value e.g. 9S, 9C, 9H, 9D, 7D) //straight flush (5 cards in a row all of the same suit e.g. 3S, 4S, 5S, 6S, 7S) //royal flush (J,Q,K,A,10 all of the same suit) return ranks[9]; } public String toString(){ String output = ""; for(Card c: hand){ if(c.getSuit().equals("Hearts") || c.getSuit().equals("Diamonds")) { output += "\u001B[31m[ " + c.getValue() + " , " + c.getSuit() + " ] \u001B[0m"; } else{ output += "[ " + c.getValue() + " , " + c.getSuit() + " ] "; } } return output; } } ------------------------------------------------------------------------------------- SortCards.java import java.util.Comparator; public class SortCards implements Comparator @Override public int compare(Card o1, Card o2) { return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started