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

2013/11/10

GetState problem

1
2
Arrinao Arrinao

2013/11/10

#
Hello, I need a hand with a getState method. I have it publicly declared in one class and want to use it in another but it keeps throwing an error message saying that a nonstatic method getState() cannot be referenced from a static concept. I've pretty much run out of ideas on what to to do with that. I'm gonna add bits of the code from Guest class and Waiter class (both descending from Actor class) where I'm most certain an error could have happened. Also if you can find some more errors in the code, please give me a hint on how to fix them. The goal is to make a Waiter to change the number of Guest (+1) if their numbers match and if the Waiter is in vicinity.
Waiter class
 List <Guest> guests = getObjectsInRange(1,Guest.class);
for (int o = 0; o < guests.size(); o++){
                Guest guest = guests.get(o);
               Guest.getState();
                if (h == waiter.getState)
                Guest.setState = h+1;
                World.removeObject(waiter);
 } 
Guest class

 public Guest() {
        guestCount++;
    }

    public Integer getState() {
        return this.state;
    } 
                
bourne bourne

2013/11/10

#
You are calling getState() on the class Guest where you have: Guest.getState(); Because you are calling the method on the Class instead of an instance of the class, the method would need to be static.
Arrinao Arrinao

2013/11/10

#
ah. I guess the parameter instanceof should be used somewhere then? Would you be so kind and provide me with the guide on how to properly correct that?
danpost danpost

2013/11/10

#
In line 5, you are using the Class name to call the 'getState' method on instead of the object that you previously declare as 'guest' on line 4. Also, that line will return a value (some state of the actor, which I believe should be of int type), but are not doing anything with it (not saving the value in a field or using it in an expression); hence its value is lost after execution of that line. I think you wanted to set its value to 'int h' which you are using on line 6. Line 6 has 'waiter.getState' which seems off. If 'getState' is a method, you need to use 'getState()' (with the parenthesis). Also, if 'waiter' refers to the actor being acted on (whose act method is currently running), then you either need to use 'this' instead of 'waiter' or drop it altogether because it is the default object (instead of 'waiter.getState', use either 'this.getState()' or just 'getState()'). I will assume by line 13 that the 'guestCount' field in the Guest class is declared 'static'. Finally, line 16 shows you returning an Integer object from the method, where an 'int' type would probably do ('int' is a primitive type; where 'Integer' is not).
Arrinao Arrinao

2013/11/10

#
I apologize, the code is a mess.
danpost wrote...
Line 6 has 'waiter.getState' which seems off.
. No, but you're right it seems like it, because i forgot to declare h. h is supposed to be guest.getState and I want the program to compare the value it gives, with the value of waiter.getState. Like I said, the goal is to make Waiter with the same number and close proximity to Guest, to dissapear and increase the value of Guest.
Arrinao Arrinao

2013/11/10

#
for (int o = 0; o < guests.size(); o++){
                Guest guest = guests.get(o);
               int h=guest.getState();
                if (h == this.getState)
                guest.setState = h+1;
                World.removeObject(waiter); 
                
                

            
"cannot find symbol variable getState". Any thoughts?
danpost danpost

2013/11/10

#
If the lines 2 through 9 (which is in the Waiter class code) are located in the 'act' method or a method it calls, then you need to change it like I said.
danpost danpost

2013/11/10

#
Arrinao wrote...
"cannot find symbol variable getState". Any thoughts?
I explained that above.
danpost danpost

2013/11/10

#
danpost wrote...
If 'getState' is a method, you need to use 'getState()' (with the parenthesis). Also, if 'waiter' refers to the actor being acted on (whose act method is currently running), then you either need to use 'this' instead of 'waiter' or drop it altogether because it is the default object (instead of 'waiter.getState', use either 'this.getState()' or just 'getState()').
Here is where it was explained.
Arrinao Arrinao

2013/11/10

#
for (int o = 0; o < guests.size(); o++){
                Guest guest = guests.get(o);
               int h=guest.getState();
      ***      if (h == this.getState());  ***
                guest.setState = h+1;
                World.removeObject(waiter); 
Damn it. Still the same error. I'm sorry maybe i'm just dumb... I highlighted the area where the error shows. Can you show me how it should look correctly? With a correct code?
danpost danpost

2013/11/10

#
There is probably some lack of information here. Please post the entire code for the Waiter class.
Arrinao Arrinao

2013/11/10

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;  // Potrebujeme pracovat se seznamem (java.util.List)
import greenfoot.Actor;
/*
 * Trida, predstavujici cisniky
 */
public class Waiter extends Actor
{    
    //Celkovy plat, reprezentovany jako pocet kol vsech cisniku
    private static Integer wages = 0;

    /*
     * Vynulovani statistik (pri vytvareni nove instance sveta)
     */
    public static void resetStatistics() {
        wages = 0;
    }

    /*
     * Vraci celkovy plat vsech cisniku
     */
    public static Integer getWages() {
        return wages;
    }

    //Typ cisnika
    private Integer type;

    //Cisnik je vytvaren primo z restaurace, ktera rozhoduje o jeho typu
    public Waiter(Integer type) {
        this.type = type;
        //Na disku jsou soubory waiter_0.png, waiter_1.png a waiter_2.png
        setImage("waiter_"+type+".png");
    }

    public void act() 
    {
        wages++;        //penize za kazde kolo aktivniho cisnika
        Integer offsetX = Greenfoot.getRandomNumber(3) - 1;
        Integer offsetY = Greenfoot.getRandomNumber(3) - 1;
        Integer newX = getX() + offsetX;
        Integer newY = getY() + offsetY;
        if (newX<0 || newX>=getWorld().getWidth()) newX = getX();
        if (newY<0 || newY>=getWorld().getHeight()) newY = getY();

        //TODO: Doplnit interakci s ostatnimi objekty (podle zadani)

        List <Waiter> waiters = getObjectsInRange(3,Waiter.class);
        List <Table> tables = getObjectsInRange(3,Table.class);
        List <Guest> guests = getObjectsInRange(1,Guest.class);

         boolean colisionFlag = false;
        for (int n = 0; n < waiters.size(); n++){
            Waiter waiter = waiters.get(n);
            if ((waiter.getX() == newX) && (waiter.getY() == newY)) {
                colisionFlag = true;
                break;
            }
        }

        for (int m = 0; m < tables.size(); m++){
            Table table = tables.get(m);
            if ((table.getX() == newX) && (table.getY() == newY)){
                colisionFlag = true;
                break;
            }
        }

        if (colisionFlag == false)
            setLocation (newX,newY);
       
        if (Greenfoot.getRandomNumber(1000)==0) {
            ((BlueWorld)getWorld()).removeWaiter();
            getWorld().removeObject(this);
        } 
        
        for (int o = 0; o < guests.size(); o++){
                Guest guest = guests.get(o);
               int h=guest.getState();
                if (h == this.getState()){
                guest.setState = h+1;
                World.removeObject(waiter); 
               break;
            }
    }
}
}
danpost danpost

2013/11/10

#
Line 3 is not necessary with line 1. Line 10 should probably be:
private int wages = 0;
The main problem is that at line 59 (at the closing of the 'for' loop that iterates through each waiter object), the field 'waiter' goes out of scope (it no longer exists beyond that line). Would it be possible for you to break down the code into segments and explain what each part is supposed to do?
danpost danpost

2013/11/10

#
I do not see a 'getState' (or 'getType') method in this class. The lines 80 through 83 should probably be more like (this is just a guess):
if (h == type){
    guest.setState(h+1);
    getWorld().removeObject(this);
}
Arrinao Arrinao

2013/11/10

#
I should emphasize that this is kinda like school work, where we are provided with part of the code (that translates to empty restaurant with no waiters) and are supposed to program the movement, collision detection and behavior of the waiters. Therefore my own code starts from line 36.
Integer offsetX = Greenfoot.getRandomNumber(3) - 1;  
        Integer offsetY = Greenfoot.getRandomNumber(3) - 1;  
        Integer newX = getX() + offsetX;  
        Integer newY = getY() + offsetY;  
        if (newX<0 || newX>=getWorld().getWidth()) newX = getX();  
        if (newY<0 || newY>=getWorld().getHeight()) newY = getY();  
This part of the code serves to make waiters move randomly and also provides collision detection for borders.
List <Waiter> waiters = getObjectsInRange(3,Waiter.class);  
        List <Table> tables = getObjectsInRange(3,Table.class);  
        List <Guest> guests = getObjectsInRange(1,Guest.class);  
  
         boolean colisionFlag = false;  
        for (int n = 0; n < waiters.size(); n++){  
            Waiter waiter = waiters.get(n);  
            if ((waiter.getX() == newX) && (waiter.getY() == newY)) {  
                colisionFlag = true;  
                break;  
            }  
        }  
  
        for (int m = 0; m < tables.size(); m++){  
            Table table = tables.get(m);  
            if ((table.getX() == newX) && (table.getY() == newY)){  
                colisionFlag = true;  
                break;  
            }  
        }  
        if (colisionFlag == false)  
            setLocation (newX,newY);  
This part of the code serves as a collision detection for both the waiter waiter interaction and waiter table interaction
 if (Greenfoot.getRandomNumber(1000)==0) {  
            ((BlueWorld)getWorld()).removeWaiter();  
            getWorld().removeObject(this);  
This part of the code serves to removes waiters that doesn't attend any table for too long.
  
for (int o = 0; o < guests.size(); o++){  
                Guest guest = guests.get(o);  
               int h=guest.getState();  
                if (h == this.getState()){  
                guest.setState = h+1;  
                World.removeObject(waiter);   
               break;  
            }  
    }  
}  
}  
Last part serves to make certain Waiters (those that shares number with the number of the Guests sitting by the table) to attend the table -> dissapear and increase the number of the table (guest) by 1
There are more replies on the next page.
1
2