This site requires JavaScript, please enable it in your browser!
Greenfoot back
drpotts2000
drpotts2000 wrote ...

2012/12/3

Binary project

drpotts2000 drpotts2000

2012/12/3

#
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? } }
Lboogie0208 Lboogie0208

2014/7/23

#
did you ever figure this out?
danpost danpost

2014/7/23

#
@Lboogie0208, the code given above is incomplete as neither the Counter class code nor the Card class code was provided. However, from what I can tell, it does not appear that the attempted code for updating the counter is optimal. That is, it looks like the thought was to add all the values of all the shown cards to update the counter each time a card is flipped. Better would be to pass the value of the flipped card to the method that updates the counter and have the counter add that to its current value and update its image. As long as the current value of the counter contains the sum of the values of all exposed cards, you do not need to sum them up again and again.
Lboogie0208 Lboogie0208

2014/7/23

#
Im trying to do this same thing i am asked to • Use a for loop to iterate over each card returned by getCards() o On each iteration use it’s ‘isShown’ method to check if it is visible. o If it is then call the card’s ‘getValue’ method and add this to ‘total’ • After the for loop return the value of total When I read this stuff I don't know what its talking about it just frustrates me and my brain shuts down. This is what I have so far
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()
    {
        int total = 0;
        
        
    }
    
    /**
     * 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()
    {
        // write your code here
    }
}
danpost danpost

2014/7/23

#
Oh, I see. This is an assignment and being practical and efficient is not what you are looking for. The getCard() method in the CardSet class has an example of using a 'for' loop that iterates over a list of objects. If that does not help as far as creating the loop you need to put in the calculateTotal() method, then maybe the second half of this page of the Java tutorials can help.
Lboogie0208 Lboogie0208

2014/7/23

#
So do I just use the same loop in the CardSet and insert it under int total? like this
 private int calculateTotal()
    {
        int total = 0;
        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;
        }
    }
danpost danpost

2014/7/23

#
(1) How does the 'for' loop on line 4 accomplish this: * Use a for loop to iterate over each card returned by getCards() (2) Where is there any reference to a card that is already in the world; (3) Where are you using 'isShown' and 'getValue' to complete this: * On each iteration use it’s ‘isShown’ method to check if it is visible. * If it is then call the card’s ‘getValue’ method and add this to ‘total’ (4) The 'calculateTotal' method needs to return an 'int' value (as per the return type given in the method declaration line. Where is the return statement that satisfies this: * After the for loop return the value of total
You need to login to post a reply.