I currently working on a project that when a card is flipped over, a total is updated. I cant seem to figure out how to get the total to update. Can anyone please help? Below is the cardSet and workingCardSet classes. I know I need to edit the calculateTotal() and the updateCounter methods, but I am not sure exactly what to do here. Can anybody please point me in the right direction. The more frustrated I get, the more I seem to shut down.
CardSet
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
* This is an Actor that wraps multiple Cards together. It updates the counter
* in this world with a display value, but this occurres on demand.
* updateCounter must be called for it to be updated.
*
* Beware that if the number of cards asked for is greater then the number of
* card images available.
*
* @author Joseph Lenton
* @version 10/11/2009
*/
public abstract class CardSet extends Actor
{
private static final int CARD_X = 60;
private static final int CARD_Y = 80;
private static final int CARD_WIDTH = 120;
private final int numCards;
/**
* Creates a new card set that holds the given number of cards.
* These cards will be laid out on the same Y position as this
* CardSet, but will be padded out towards either side along
* the X axis.
* @param numCards The number of cards in this card set, must be greater then 0.
*/
public CardSet(int numCards)
{
if ( numCards <= 0 ) {
throw new IllegalArgumentException("The number of cards must be greater then 0, was given: " + numCards);
}
this.numCards = numCards;
setImage( new GreenfootImage(1, 1) );
}
/**
* This will update the counter with the starting total.
*/
/* Note that this places cards starting from the right of the cardset,
* and then moving across to the left. This is so the largest cards are
* on the left side and the smallest on the right. */
public void addedToWorld(World world)
{
float startX = CARD_WIDTH * (numCards-1)/2f;
int cardX = getX() + Math.round( startX );
for (int i = 0; i < numCards; i++) {
final int cardNum = 1 << i;
final boolean isCardShown = (i % 2 != 0);
world.addObject(
new Card(cardNum, this, isCardShown),
cardX, CARD_Y );
cardX -= CARD_WIDTH;
}
updateCounter();
}
/**
* Gathers together and returns all of the cards that belong to this CardSet.
* @return A list of cards within this CardSet.
*/
public List<Card> getCards()
{
final List<Card> cards = new ArrayList<Card>();
for (Object obj : getWorld().getObjects( Card.class )) {
final Card card = (Card) obj;
if ( card.getCardSet() == this ) {
cards.add( card );
}
}
return cards;
}
/**
* This will find and return the Counter you can use to display the total
* of this CardSet.
*
* @return The Counter used to display the value for this CardSet in the World.
*/
public Counter getCounter()
{
return (Counter) getWorld().getObjects( Counter.class ).get( 0 );
}
/**
* Updates the counter in this world with the total of all cards
* currently shown.
*/
public abstract void updateCounter();
}
WorkingCardSet
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* In implementation of a CardSet with it's update logic implemented. This
* is where your programming needs to be done.
*
* @author Joseph Lenton
*/
public class WorkingCardSet extends CardSet
{
/**
* Constructor for the WorkingCardSet. This passed the number of cards
* in this set up to it's parent class.
*
* You don't need to edit this method.
* @param numCards The number of cards in this CardSet, must be greater then 0.
*/
public WorkingCardSet(int numCards)
{
super( numCards );
}
/**
* Calculates the total of the cards. This is the total of the values for
* all of the cards that are shown. The cards that are not shown will be
* ignored.
*
* @return The total value of all of the cards shown.
*/
private int calculateTotal()
{
///What do I put here???
}
return total;
}
/**
* Updates the counter in this world with the total of all cards
* currently shown.
*
* This is called automatically when a card is flipped.
*/
public void updateCounter()
{
// what do I put here?
}
}

