troubleshooting Question

# How do I sum all numbers in a BlackJack game?

Java
I need to count the value of the hand, with each Ace value being 1, count the number of aces, if the number of aces is greater than or equal to 1 and the current hand value is less than 12 then increment the hand value by 10 and of course return it.

Card class:

``````public class Card
{

private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
private char suit;//card suit: S,C,H,or D
private static String[] Ranks={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
private static char[] Suites={'C','H','D','S'};

/**
* Constructor for objects of class Card
*/
public Card()
{
rank="2";
suit='H';

}

public Card(String rank, char suit)
{
this.rank=rank;
this.suit=suit;
}
public String getRank()
{
return rank;
}

public char getSuit()
{
return suit;
}

public void setRank(String rank)
{
this.rank=rank;
}

public void setSuit(char suit)
{
this.suit=suit;
}
//Returns an array of possible card ranks
public static String[] getPossibleRanks()
{
return Ranks;
}
//Returns an array of possible card suits
public static char[] getPossibleSuits()
{
return Suites;
}

public String toString()
{String suitName = "";
switch (suit){
case 'H':
suitName = rank + " of Hearts";
break;
case 'S':
suitName = rank + " of Spades";
break;
case 'D':
suitName = rank + " of Diamonds";
break;
case 'C':
suitName = rank + " of Clubs";

}
return suitName;
}
}
``````

DeckOfCards class:

``````import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck;
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
deck = new ArrayList<Card>();
resetDeckOfCards();
}

public void resetDeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < Card.getPossibleRanks().length ; i++)
{
for (int j = 0; j < Card.getPossibleSuits().length; j++)
{
}
}
}

public Card getCard()//remove random card of arraylist of cards and return value from method
{
if (deck.isEmpty())
resetDeckOfCards();
return deck.remove(getRandom.nextInt(deck.size()));
}

public ArrayList<Card> getHand(int size)
{
ArrayList<Card> hand = new ArrayList<Card>();
for (int i = 0; i < size; i++)
{
}
return hand;

}

}
``````

TwoPlayerBlackJack class so far:

``````import java.util.ArrayList;
/**
* Write a description of class TwoPlayerBlackJack here.
*
* @version (a version number or a date)
*/
public class TwoPlayerBlackJack
{
// instance variables - replace the example below with your own
private String rank;
private ArrayList<Card> blackJack;
private char suit;
/**
* Constructor for objects of class TwoPlayerBlackJack
*/
public TwoPlayerBlackJack()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
blackJack = new ArrayList<Card>();
for (int i = 0; i < Card.getPossibleRanks().length ; i++)
{
for (int j = 0; j < Card.getPossibleSuits().length; j++)
{
}
}
}

/**
* An example of a method - replace this comment with your own
*
* @param  y   a sample parameter for a method
* @return     the sum of x and y
*/
public int getCardValue(Card c){
String rank = c.getRank();

int value = -1;
try {
value = new Integer(rank);
}catch(Exception ex){
if(rank.equals("Ace"))value = 1;
else{ value = 10;

}
return value;
}
}
public computeHandValue(ArrayList <Card> hand)
{
String rank = hand.getCardValue();
for(rank.equals("Ace") >=1)&&(getCardValue()<12)){
n = n+10;

}
return hand;

}
``````
Join the community to see this answer!
###### Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.