The getHandValue() method treats all Aces as having a value of 1.
I need anyone can check of my Hand class codes: "The getHandValue() method treats all Aces as having a value of 1. This needs to be corrected in the final version"
Deck Class:
import java.util.ArrayList;
public class Hand {
ArrayList<Card> hand;
public Hand() {
hand = new ArrayList<Card>();
}
public void add(Card c) {
hand.add(c);
}
public String toString() {
String temp = "[";
for (int i = 0; i < hand.size(); i++) {
temp += hand.get(i);
if (i < hand.size() - 1) {
temp += ",";
}
}
temp += "]";
return temp;
}
/*
The getHandValue() method treats all Aces
as having a value of 1. This needs to be
corrected in the final version.
*/
public int getHandValue() {
int total = 0;
boolean hasAces = false;
for (int i = 0; i < hand.size(); i++) {
int value = hand.get(i).getValue();
if (value > 10) {
total += 10;
}
else if (value == 1) {
hasAces = true;
total += 1;
}
else {
total += value;
}
} // end of for loop
if (hasAces) {
}
return total;
}
}
Deck Class:
import java.util.ArrayList;
public class Hand {
ArrayList<Card> hand;
public Hand() {
hand = new ArrayList<Card>();
}
public void add(Card c) {
hand.add(c);
}
public String toString() {
String temp = "[";
for (int i = 0; i < hand.size(); i++) {
temp += hand.get(i);
if (i < hand.size() - 1) {
temp += ",";
}
}
temp += "]";
return temp;
}
Step by step
Solved in 2 steps