Question: I'm having trouble figuring out what's wrong with this code. error: incompatible types: Object cannot be converted to Card for(Card c: cards)- the enhanced for
I'm having trouble figuring out what's wrong with this code. error: incompatible types: Object cannot be converted to Card for(Card c: cards)- the enhanced for loop. Also, is this normal or do I need to do someting about it: Note: Hand.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Also, I need help adding a method to get a card and calculate the hand's total value.
import java.util.*; public class Hand { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList cards;
//This constructor sets up our hand by initializing our ArrayList. public Hand() { cards = new ArrayList(); }
//This adds a card to our hand. public void addCard(Card c) { cards.add(c); }
//This returns the value of the hand as an integer. //The value of the hand is the sum of the values of the individual cards. //There is also an adjustment made for the value of an ace //which can be 11 or 1 depending on the situation. public int getValue() { int sum = 0; int aceCount = 0;
for(Card c: cards) { sum += c.getValue(); if(c.getRank() == ACE) { aceCount++; } } while(sum > 21 && aceCount > 0) { sum -= 10; aceCount--; } return sum; }
//Return if this hand has a blackjack. public boolean hasBlackJack() { return getValue() == 21 && cards.size() == 2; }
//Return if the hand is busted, which means it has value //greater than 21. public boolean busted() { return getValue() > 21; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
