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

2021/10/6

using a variable from another class in the world class

1
2
3
xixEmilyxix xixEmilyxix

2021/10/6

#
Hi, i have a variable - weatherValue that is in my WeatherInput class and i need to use the value of weatherValue in my BeeWorld class (my world class). How would i go about this? This is the code to set the value of weatherValue in the WeatherInput class:
 weatherValue = Integer.parseInt(Greenfoot.ask("Please input a value for the weather"));
danpost danpost

2021/10/6

#
Please provide codes in BeeWorld class.
xixEmilyxix xixEmilyxix

2021/10/6

#
this is the code relating to the weather value
public class BeeWorld extends World
{
    //declare variables
    Flower PinkFlower;
    BeeInput beeValue;
    StartButton StartButton;
    WeatherInput WeatherInput;
    WeatherInput weatherValue;
and this is where i need im using it
    
    public void runSimulation()
    {
         int weatherValue = WeatherInput.weatherValue;

         if(weatherValue >= -40 || weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 || weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 || weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
danpost danpost

2021/10/6

#
Your fields (which I am not sure all are needed) in your BeeWorld class should be like this:
Flower pinkFlower;
BeeInput beeValue;
StartButton startButton;
WeatherInput weatherInput;
// WeatherInput weatherValue;
The last line was commented out as its value is contained in weatherInput (in the WeatherInput object) and can be obtained by using "weatherInput.weatherValue".
xixEmilyxix xixEmilyxix

2021/10/6

#
When i remove line 5. it then displays an error which is this: ava.lang.NullPointerException at BeeWorld.runSimulation(BeeWorld.java:52) at BeeWorld.<init>(BeeWorld.java:47) at StartButton.act(StartButton.java:26)
danpost danpost

2021/10/6

#
Please show full BeeWorld class.
xixEmilyxix xixEmilyxix

2021/10/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BeeWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BeeWorld extends World
{
    //declare variables
    Flower PinkFlower;
    BeeInput beeValue;
    StartButton StartButton;
    WeatherInput WeatherInput;
    /**
     * Constructor for objects of class BeeWorld.
     * 
     */
    public BeeWorld()
    {
        //set background to main screen when button is pressed
        super(864, 540, 1);
        setBackground(new GreenfootImage("Background.png"));
        
        //add the add day button
        AddDay AddDay = new AddDay ();
        addObject (AddDay, getWidth (), getHeight()); 
        AddDay.setLocation(820,500);
        
        //add the day counter
        showText("Day: 1",  800, 50);
        
        //add the bee counter
        showText("Bees: " + beeValue, 800, 25);
        
        //add the nectar counter
        
        //add the honey counter
        
        //add the help button
        HelpButton HelpButton = new HelpButton ();
        addObject (HelpButton, getWidth (), getHeight ()); 
        HelpButton.setLocation(100,190);
        
        //run the simulation
        runSimulation();
    }
    
    public void runSimulation()
    {
         int weatherValue = WeatherInput.weatherValue;

         if(weatherValue >= -40 || weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 || weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 || weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
    
    public void lowTemperature()
    {
        //add flowers to the bee world
        PinkFlower PinkFlower = new PinkFlower ();        
        addObject(PinkFlower, getWidth(), getHeight());
        PinkFlower.setLocation(500,500);
    }
    
    public void regularTemperature()
    {
    }
    
    public void highTemperature()
    {
    }
}
danpost danpost

2021/10/7

#
I do not see where you are creating a WeatherInput object:
weatherInput = new WeatherInput();
xixEmilyxix xixEmilyxix

2021/10/8

#
ive added this and it does not seem to be taking into account what i set the weather value at, as it is only running lowTemperature()
    public void runSimulation()
    {
         WeatherInput = new WeatherInput();
         int weatherValue = WeatherInput.weatherValue;

         if(weatherValue >= -40 || weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 || weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 || weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
danpost danpost

2021/10/8

#
xixEmilyxix wrote...
ive added this and it does not seem to be taking into account what i set the weather value at, as it is only running lowTemperature()
Please provide WeatherInput class codes. It appears that you may have misplaced a line or two.
xixEmilyxix xixEmilyxix

2021/10/8

#
public class WeatherInput extends Actor
{
    //create variable
    public int weatherValue;
    /**
     * Act - do whatever the weatherInput wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      //Create input box, ask for value, set bounds for weatherValue
      setImage(new GreenfootImage("Input.png"));
      
      //show weatherValue in the box on main menu
      getWorld().showText("" + weatherValue, 400, 250);
      
      //set value of weather based off the input
        if(Greenfoot.mouseClicked(this))
        {
            weatherValue = Integer.parseInt(Greenfoot.ask("Please input a value for the weather"));
        if(weatherValue < -40 || weatherValue > 40)
        {
            World world = getWorld();
            world.addObject(new ErrorMessageWeather(), 350, 200);
        } else {
            World world = getWorld();
            world.removeObjects(world.getObjects(ErrorMessageWeather.class));
        } 
      }
    }
}
danpost danpost

2021/10/8

#
Don't you still need to add the WeatherInput object into the world and then run the simulation and click on it so that you can change its value?
xixEmilyxix xixEmilyxix

2021/10/8

#
the weather input is in the MainMenu world i have, its at the start thats where you enter the value and then after you have entered it, you hit the start button and it goes to the BeeWorld world. This is my MainMenu class:
public class MainMenu extends World
{
    //declare variables
    ErrorMessageWeather ErrorMessageWeather;
    StartButton StartButton;
    WeatherInput WeatherInput;
    HelpButton HelpButton;

    /**
     * Constructor for objects of class mainMenu.
     * 
     */
    public MainMenu()
    {    
        // Create the main menu
        super(864, 540, 1);
        Greenfoot.start();
        setBackground(new GreenfootImage("MainMenu.png"));
        
        //add the help button
        HelpButton HelpButton = new HelpButton ();
        addObject (HelpButton, getWidth (), getHeight ()); 
        HelpButton.setLocation(100,190);       
        
        //add start button to main menu
        StartButton StartButton = new StartButton ();
        addObject (StartButton, getWidth (), getHeight ()); 
        StartButton.setLocation(150,350);
        
        //add weather input box to main menu
        WeatherInput WeatherInput = new WeatherInput ();
        addObject (WeatherInput, getWidth (), getHeight ()); 
        WeatherInput.setLocation(400,250);
    }
}
danpost danpost

2021/10/8

#
xixEmilyxix wrote...
the weather input is in the MainMenu world i have]
But that is not being passed to the other world.
xixEmilyxix xixEmilyxix

2021/10/8

#
how would i do that? i thought because i assigned it in the WeatherInput class it didnt matter?
 weatherValue = Integer.parseInt(Greenfoot.ask("Please input a value for the weather"));
There are more replies on the next page.
1
2
3