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

2013/8/28

Help Please

1
2
3
4
mattjames mattjames

2013/9/4

#
Hi again guys. Just putting that in, firstly, what does typecast mean? And when i compile what Danpost and then davmac put it tells me that worldWorld (sorry about name will change that) is not a variable. Do I have to make it first?
mattjames mattjames

2013/9/4

#
This is what I looks like if that helps
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor
{
   public void addedToWorld(World w)  
{  
    worldWorld = (world) w;
    counter3 = w.getCounter3();  
    choice = counter3.getCount();  
}  
    private Counter2 theCounter2;
        private Counter theCounter;
    private int maximumcarrying=0; 
    private int score=0;
    private int scorelevelone=150;//this sets the score
    private int scoreleveltwo=150;
    private boolean win=false;
    private int targetscore; //may need to remove this
    private int level=1;
    private int passengersleft=4;
    private int passengerscarrying=0;
    //need to use this to set maximum bay size!!!!!!!!!!!
    


    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        scoretester();// Add your action code here.
    }  
    //maybe try using a boolean to see whether the game has been won

    public void setcounter(){ //this will decide whether to keep the counter at 150 or lower it to 130
        if (level==2){

            world worldWorld = (world) getWorld();
            Counter counter = worldWorld.getCounter();
            counter.bumpCount(-20);
            scoretester();
        }
        else{
            scoretester();

        }

    }
davmac davmac

2013/9/4

#
A "typecast" is when you put a class name in parentheses before some expression to tell the compiler that you know the specific type that it will return. So, in:
world worldWorld = (world) getWorld();  
... there is a typecast - (world) - to tell the compiler that you know that getWorld() will return a world (normally it returns a World). Without the typecast you get an error when you try to compile, because you can't assign a World value to a world variable. Please, please, rename your 'world' class. When you get an error and want help with it, please say which line the error occurs on.
danpost danpost

2013/9/4

#
Change lines 13 and 14 in you last code post to:
world worldWorld = (world) w;
counter3 = worldWorld.getCounter3();
The first line creates a field that holds a 'world' object and assigns it the parameter value that is an object of type 'World' typecast to a 'world' object. The second line would fail if you tried to call 'getCounter3' on an object of type 'World' because the compiler does not know to look in the 'world' class for that method.
mattjames mattjames

2013/9/4

#
I'm sorry I will change the world name in a second. However there is still a problem with the code (indicated by the comment):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor
{
   public void addedToWorld(World w)  
{  
world worldWorld = (world) w;  
counter3 = worldWorld.getCounter3();     //error displays "cannot find variable counter 3"
  
    choice = counter3.getCount();  
}  
danpost danpost

2013/9/4

#
You need to instruct the compiler what type of data the field 'counter3' is to hold. This is done any time you declare a new field. 'counter3' is to be a new field in the robot class 'addedToWorld' method (because it is not declared in the class itself, it will be localized to the method).
// change line 14 to
Counter counter3 = worldWorld.getCounter3();
mattjames mattjames

2013/9/4

#
New error (sorry about this!)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor
{
   public void addedToWorld(World w)  
{  
world worldWorld = (world) w;  
Counter counter3 = worldWorld.getCounter3(); //incompatible types, found Counter3 but expected Counter
  
    choice = counter3.getCount();  
} 
danpost danpost

2013/9/4

#
Yeah, I forgot you created separate classes for each type counter.
//change line 14 to
Counter3 counter3 = worldWorld.getCounter3();
mattjames mattjames

2013/9/4

#
That worked but there is a new problem now:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor
{
   public void addedToWorld(World w)  
{  
world worldWorld = (world) w;  
Counter3 counter3 = worldWorld.getCounter3(); //fine now!
  
    choice = counter3.getCount();  //cannot find symbol - variable choice
danpost danpost

2013/9/4

#
That will also need to be declared as it also is a new field for the method.
int choice = counter3.getCount();
mattjames mattjames

2013/9/4

#
Thank you that worked however this has now popped up: further on in the same actor I have this code
public void scoring(){
        //insert code here to check modification and then reduce accordingly
        if (choice==1){ //it says it cannot find choice
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
}
and it says it cannot find the variable choice. Is this because they are in different {public void...} brackets? How do I combat this?
danpost danpost

2013/9/4

#
Is it not possible that the Counter3 object value could change? and when it does, the scoring will be different?
mattjames mattjames

2013/9/4

#
No it doesn't change it only changes at the start before the actor with scoring in is initialised.
danpost danpost

2013/9/4

#
In that case, it will not hurt to add an instance field in the 'robot' class to hold the value. Remove the 'int' we added to the 'choice' assignment line and add
private int choice;
outside any method, but still inside the class. You are correct, fields declared in one method are not available for use in others unless somehow passed between the two. Once a method is finished executing, any and all fields declared within it that are not stored anywhere else are no longer accessible. All fields declared in the class outside of any method can be accessed by any method within the class. Their accessibility by other classes and scope (when they are destroyed or reset) are determined by the modifiers you place in the field declaration statement. The section on Classes and Objects in the Java tutorials might be something to review.
mezboycalday mezboycalday

2013/10/21

#
matt james need help with my report asap!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! plz email me mez1998@aol.com
You need to login to post a reply.
1
2
3
4