• Status: Solved
• Priority: Medium
• Security: Public
• Views: 329

# How do I add sum of all card values?

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;

}
0
ryanbecker24
• 11
• 2
• 2
1 Solution

Commented:

this is how yo should computer:
``````public int computeHandValue(ArrayList <Card> hand)
{

int sum = 0;
int numAces = 0;

for(Card c: hand){
int value = c.getCardValue();
sum += value;
if(c.getRank().equals("Ace"))numAces++;

}
if(sum<12 && numAces >0)sum += 10;
}
return sum;;

}
``````
0

Commented:

removed one extra parenthesiis

``````public int computeHandValue(ArrayList <Card> hand)
{

int sum = 0;
int numAces = 0;

for(Card c: hand){
int value = c.getCardValue();
sum += value;
if(c.getRank().equals("Ace"))numAces++;

}
if(sum<12 && numAces >0)sum += 10;

return sum;;

}
``````
0

Author Commented:
Thanks, but for some reason my compiler says it can't find getCardValue(). The rest of the code is fine.
0

Commented:
Post your code for Card class exactly as you use it.
(when posting the code plese use the code tags - it is easier to cut and paste such code)

0

Author Commented:
What do you mean by post your code for Card class exactly as you use it? That is my Card class and DeckOfCards class. Do you want me to the computeHand method in the Card class?
0

Commented:
Sure post the code as it reaports you the error - I'll try to compile it myslef -
the code you posted above was different as you cahnged the computeHand  method
and may have distuvbed something in the meantime - maybe I made a mistajke in typing also - but I cannot check unless I have the full code
with the cwhole lasses
0

Commented:
In order to see what eeror you exeprience I need to have your code exactly as the one where compiler generates error for you
0

Commented:
The best thing is to post all public classes separately like that:

``````public class Card {

//contentsof class  ghere

}
``````

``````public class DeckOfCrads {
//contetsn of class here

}
``````

this is the simples way for me to curt and paste them into my IDE
0

Commented:
if compiler does not find soime method this is probably because you have wrong declaration format or
something wrong with the number of braces - I can't detect this error unless I have your full code and unless I can
paste it into .java files into my IDE
0

Commented:

try this variant:
``````public int computeHandValue(ArrayList <Card> hand)
{

int sum = 0;
int numAces = 0;

for(Card c: hand){
int value = getCardValue(c);
sum += value;
if(c.getRank().equals("Ace"))numAces++;

}
if(sum<12 && numAces >0)sum += 10;

return sum;;

}
``````
0

Commented:
In general, it is more logical to have the method getCardValue() (or maybe getValue()) as the method of the Card class - as it is the property of the
instance of the Card class which you are retrieving - as you have getRank() , getSuite() method - next to them you'll have
getValue()  method, as you may want to use this method not only in the class TwoPlayerBlackJack but maybe in some other class also.
As this is a property of the Card instance it is better to have such method in the Card class from the point of view of the design of the application.

0

Commented:
Ryan, your only signature for getCardValue is
``````public int getCardValue(Card c)
``````

However your messages to getCardValue are
``````String rank = hand.getCardValue();
for(rank.equals("Ace") >=1)&&(getCardValue()<12)
``````

The compiler can't find a signature of getCardValue that doesn't have any parameters.  Thus the error.
0

Commented:
@hmccurdy,
This was already corrected in the code ID:36519406
0

Commented:
@for_yan, yes it is.  However, in my experience as a tutor, I see this problem frequently.  It is the problem I looked for first.  I find that when I'm explicit about the problem with the student that the student almost never makes the same mistake again (at least for the remainder of the term).

The reason for the post is that I want the author to learn.   (My desire to help others learn is why I volunteer to be an unpaid tutor.)
0

Commented:
I agree with you and that's why I mentioned to the authtor that it is makes sense to make method getValue() as the method of the card class, rather than
to have getVlaue(Card c)  in the general class TwoPlayerBlackJack  which more represents the game rather than the Card (ID:36519440).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.