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

2012/9/7

Showing messages from Classes and updated messages from Counter

1
2
3
4
ManiHallam ManiHallam

2012/9/10

#
this is the Money Class
public class Money extends Actor
{
    private int credit;
    
    /**
     * Act - do whatever the Money wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int getValue()
    {
        return credit;
    }
    }
ManiHallam ManiHallam

2012/9/10

#
the counter and Screen class don't have any image
ManiHallam ManiHallam

2012/9/10

#
oh, sorry. just now noticed that, the Money class should be like this
public class Money extends Actor
{
    private int value;
    
    /**
     * Act - do whatever the Money wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int getValue()
    {
        return value;
    }
    }
ManiHallam ManiHallam

2012/9/10

#
I do upload the Scenario with source code, then you can download it and have a better picture, but if you do not mind.
danpost danpost

2012/9/10

#
That looks better for the Money class. The Screen class does have an image, but the Counter class does not. But, since the Screen class is what displays the credit amount that the Counter is currently trying to track, lets get rid of the Counter class and move the credit variable to the Screen class, with the method to get its value and adding a method to add to its value. That is, in the Screen class, add the following:
// instance variable
int credit = 0;
//methods
public void addCredit(int addAmt)
{
    credit += addAmt;
    setText("Credit: " + credit);
}

public int getCredit()
{
    return credit;
}
If you are going to use the Screen object to display other data at some other point, we can cross that bridge when we get there together. Now, your 'credit' method in the Money classes can be
public void credit()
{
    ((Screen) getWorld().getObjects(Screen.class).get(0)).addCredit(value);
}
You can put this in the Money class itself and it will work for all your monies. In other words, you can remove the 'credit' methods from all the sub-classes of Money.
danpost danpost

2012/9/10

#
Dear ManiHallam, your source code is not available. Did you check the box to include the source when you uploaded the scenario?
ManiHallam ManiHallam

2012/9/10

#
it available now, sorry.
danpost danpost

2012/9/10

#
By placing a method in a super-class, you do not have to have the same code copied over and over in the sub-classes. This means you only have one place to look for the code, and less chance of typos. This could be done with your 'act' method in your Money sub-classes, but since coins and notes 'act' slightly different (I could be wrong about this) , I would have one in the Coins (which works for all the coins) and one in the Notes (which works for all the notes). If I am mistaken, you can just have the one in the Money class.
ManiHallam ManiHallam

2012/9/10

#
is that ok, in instance of coins and notes I put for example for FiftyPence: "int value = 50", because before it was "n"
danpost danpost

2012/9/10

#
That is fine. Much more understandable than 'n'.
ManiHallam ManiHallam

2012/9/10

#
the last code in Money Class gives error as: not a statement
danpost wrote...
Now, your 'credit' method in the Money classes can be
public void credit()
{
    ((Screen) getWorld().getObjects(Screen.class).get(0)).addCredit(value);
}
You can put this in the Money class itself and it will work for all your monies. In other words, you can remove the 'credit' methods from all the sub-classes of Money.
danpost danpost

2012/9/10

#
What is the error message and where is it highlighting?
ManiHallam ManiHallam

2012/9/10

#
exactly before <here>((Screen) getWorld().getObjects(Screen.class).get(0)).addCredit(value)); error: not a statement
danpost danpost

2012/9/10

#
Check your syntax and bracketing around the statement. NVM, you have one too many close parenthesis at the end of the statement.
ManiHallam ManiHallam

2012/9/10

#
this is the code I have and still same error
public void credit()
    {
        ((Screen) getWorld().getObject(Screen.class).get(0).addCredit(value));
    }
There are more replies on the next page.
1
2
3
4