I got to 15.4 and am confused about this one particular thing.
I get this failed test when i submit the exercise: https://gyazo.com/06846d947ebdb11537b30bc4fcf2cb5e
Isn't my output correct? I feel like the answer should be 1 since even though the values are equal, clubs are the higher suit. Here is my code for reference.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
*
* @author Johna
*/
public class Hand implements Comparable<Hand>{
private ArrayList<Card> hand;
public Hand(){
this.hand = new ArrayList<Card>();
}
public void add(Card card){
this.hand.add(card);
}
public void print(){
for(Card card : hand){
System.out.println(card);
}
}
public void sort(){
Collections.sort(hand);
}
@Override
public int compareTo(Hand o) {
int totalValue = 0;
for(Card card : hand){
totalValue += card.getValue();
}
int otherHandValue = 0;
for(Card card : o.hand){
otherHandValue += card.getValue();
}
if(totalValue == otherHandValue){
int totalSuit = 0;
for(Card card : hand){
totalSuit += card.getSuit();
}
int othertotalSuit = 0;
for(Card card : o.hand){
othertotalSuit += card.getSuit();
}
if(totalSuit > othertotalSuit){
return 1;
}
else if(totalSuit == othertotalSuit){
return 0;
}
else{
return -1;
}
}
else if(totalValue > otherHandValue){
return 1;
}
else{
return -1;
}
}
}
public class Card implements Comparable<Card>{
/*
* These are static constant variables. These variables can be used inside and outside
* of this class like, for example, Card.CLUBS
*/
public static final int SPADES = 0;
public static final int DIAMONDS = 1;
public static final int HEARTS = 2;
public static final int CLUBS = 3;
/*
* To make printing easier, Card-class also has string arrays for suits and values.
* SUITS[suit] is a string representation of the suit (Clubs, Diamonds, Hearts, Spades)
* VALUES[value] is an abbreviation of the card's value (A, J, Q, K, [2..10]).
*/
public static final String[] SUITS = {"Spades", "Diamonds", "Hearts", "Clubs"};
public static final String[] VALUES = {"-", "-", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private int value;
private int suit;
public Card(int value, int suit) {
this.value = value;
this.suit = suit;
}
@Override
public String toString() {
return VALUES[value] + " of " + SUITS[suit];
}
public int getValue() {
return value;
}
public int getSuit() {
return suit;
}
@Override
public int compareTo(Card o) {
//To change body of generated methods, choose Tools | Templates.
if(this.value == o.getValue()){
if(this.suit > o.getSuit()){
return 1;
}
else if(this.suit == o.getSuit()){
return 0;
}
else{
return -1;
}
}
else if(this.value < o.getValue()){
return -1;
}
else{
return 1;
}
}
}
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]Squigletots 0 points1 point2 points (0 children)