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

2015/10/8

HElP.

makconas makconas

2015/10/8

#
I am new studying Greenfoot. And I have a problem on my assignment. I put this as follows in Actor class public static final int COLOR_RED = 0; public static final int COLOR_GREEN = 1; public static final int RANK_RAT = 1; public static final int RANK_CAT = 2; public static final int RANK_DOG = 3; public static final int RANK_WOLF = 4; public static final int RANK_LEOPARD = 5; public static final int RANK_TIGER = 6; public static final int RANK_LION = 7; public static final int REAL_RANK_LION = 6; public static final int RANK_ELEPHANT = 8; while this in world: private void initializeChess(){ Chess ch; ch = new Chess (Chess.COLOR_GREEN,Chess.RANK_TIGER ); GWorld.addOneObject( ch, 0, 8); ch.setImage("green_tiger.png"); } the complier says " constructor Chess in class Chess cannot be applied to given types; Wrong part : "ch = new Chess (Chess.COLOR_GREEN,Chess.RANK_TIGER );" [Disallowed URL]
danpost danpost

2015/10/8

#
Apparently, you do not have a constructor in the Chess class for which you are trying to use: public Chess(int color, int rank)
makconas makconas

2015/10/8

#
danpost wrote...
Apparently, you do not have a constructor in the Chess class for which you are trying to use: public Chess(int color, int rank)
What's the return type? (I 've tried "public int Chess()" ) but sorry it doesn't work, What I want to do is building a jungle (board game) Maybe there's sth wrong with my act. I build it as " public class Chess extends Actor. public class Tiger extends Chess"
danpost danpost

2015/10/8

#
makconas wrote...
What's the return type? (I 've tried "public int Chess()" )
Constructors do not have a return type. When using the 'new' keyword, the object created is returned by default. When you use the following:
ch = new Chess (Chess.COLOR_GREEN,Chess.RANK_TIGER );
The object, if created, is returned and assigned to the variable 'ch'.
I build it as " public class Chess extends Actor. public class Tiger extends Chess"
This information leads me to believe that you need to create a 'new Tiger()' object instead of a new Chess object. If you create a Chess object from the Chess class using 'new Chess', you will not be able to make that object any specific type of Chess object. On the other hand, if you create a Tiger object using 'new TIger', it is automatically a Chess object as well because the Tiger class is a subclass of the Chess class. So, your class structure seems okay, but how you try to create objects of the subclasses does not. You should be able to just do the following:
ch = new Tiger();
makconas makconas

2015/10/8

#
Thank you for your help . However, public Chess(int Color, int Rank) reports that " missing method body, or declare abstract". ( this statement is added in Actor Class") I have also given Class " Chess" a "tiger image" and removed the subclass of tiger.
danpost danpost

2015/10/8

#
You probably do not need to pass the values of color and rank to the Chess constructor. This means you do not need a 'Chess(int color, int rank)' constructor. I was just saying that the way you were trying to create a Chess object, you needed a constructor that is declare like that. You can just create one now with:
ch = new Chess();
makconas makconas

2015/10/8

#
but I am building sth like a international chess . In this programme , I need to tell Greenfoot that tiger can eat leopard while leopard can not eat tiger , this require a rank method to instruct Greenfoot. One the other hand , I also have to distinguish the colour of each corresponding chess(ie. red rat to green rat) whereas the logs always reports that failure to find symbol of " variables of "COLOR_RED" " RANK" My thought is that set parameter in it and afterwards tell Greenfoot the Colour to setImage. Should I set other classes to illustrate that ?
danpost danpost

2015/10/8

#
You should not have any problems with those variables as they are "class constants", provided that when used outside their declared class you prefix them with the class name (i.e. Chess.COLOR_RED). It is okay for you to pass the rank when creating a Chess object, but you will need to write a constructor in the Chess class to receive and save that value:
// IN 'Chess' CLASS

// instance field
private int rank;

// constructor
public Chess(int rank)
{
    this.rank = rank;
}
You could also expand this to accept the color and set the proper image to the actor in this constructor; however, the constructor must be declared to receive each piece of data you wish to provide to it. You can also add a method to return the "rank" of a Chess object:
public int getRank()
{
    return rank;
}
This will allow one Chess object to acquire the rank of another one.
makconas makconas

2015/10/8

#
I finally make it ,thank you very much! would you please be so kind to help me check the source code? thanks again.
public class TestWorld extends GWorld
{
    static {      
        // Initialize the world
        GWorld.setWidth(7);  // 
        GWorld.setHeight(9);
        GWorld.setCellSize(60);
    }
     
    
    public TestWorld() {           
       initialize();
    }
    
    public void initialize() {                 
       initializeLandscape();
       initializeChess();
       
    }
    private void initializeChess(){
        Chess ch;
        ch = new Chess (Chess.COLOR_GREEN,Chess.RANK_TIGER);
        GWorld.addOneObject( ch, 0, 8);
        ch.setImage("green_tiger.png");
        
    }
    private void initializeLandscape(){
        GWorld.addOneObject ( new Cave(), 3, 0 );
        GWorld.addOneObject ( new Cave(), 3, 8 );
        GWorld.addOneObject ( new Trap(), 2, 8 );
    }
    

    
    
   
}
public class Chess extends Actor
{
    /**
     * Act - do whatever the Chess wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }   
    public static final int COLOR_RED = 0;
    public static final int COLOR_GREEN = 1;
    
    public static final int RANK_RAT = 1;
    public static final int RANK_CAT = 2;
    public static final int RANK_DOG = 3;
    public static final int RANK_WOLF = 4;
    public static final int RANK_LEOPARD = 5;
    public static final int RANK_TIGER = 6;
    public static final int RANK_LION = 7;
    public static final int REAL_RANK_LION = 6;
    public static final int RANK_ELEPHANT = 8;
    
    public Chess (int COLOR, int RANK){}
    
    
    
}
danpost danpost

2015/10/8

#
It does not good to pass the color and rank values to the Chess constructor unless you do something with them in that constructor. Add two instance int fields to the Chess class to hold those values and set those fields in the constructor.
You need to login to post a reply.