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

2016/11/25

Int method's syntax incorrect? What am I missing?

alittle_bit alittle_bit

2016/11/25

#
As I was typing away an example for some work, I came across a syntax error with the 'getRandomInt()' method. When the method was a 'void' method, the latter 'addObject()' method would not accept the 'getRandomInt' method as it was... classified as void (I guess thats how to word it). As I had not practised nit methods before, I thought I would set the 'getRandomInt()' as an int method instead of void and thats where I came across an error suggesting the 'getRandomInt()' method was incomplete even with closed brackets... Any ideas how to fix this syntax error? Am I missing a whole different level of understanding in order to utilise classifying a method as an int method? Any fixes would be appreciated thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        createAnimals();
    }
     
    public int getRandomInt(int max)
    {
        int randomInt = Greenfoot.getRandomNumber(max);
    }
     
    private void createAnimals()
    {
        int count = 0;
         
        addObject(new Snake(), 295, 190);
         
        if(count >= 10)
        {
            addObject(new Camel(), getRandomInt(599), getRandomInt(399) );
        }
    }
Super_Hippo Super_Hippo

2016/11/25

#
You need to return an int. Change line 11 to
1
return Greenfoot.getRandomNumber(max);
danpost danpost

2016/11/25

#
By the way, the maximum value returned will be one less than the value given for 'max'. Therefore, line 22 can be:
1
addObject(new Camel(), getRandomInt(600), getRandomInt(400));
which would allow the camel to spawn at any and all possible locations in the world. Some improvements might be: (1) using 'getWidth()' and 'getHeight()' instead of the actual values of '600' and '400' (so if you change the size of the world, the spawning range automatically adjusts with it):
1
2
3
4
5
6
addObject(new Camel(), getRandomInt(getWidth()), getRandomInt(getHeight()));
 
// or
int x = getRandomInt(getWidth());
int y = getRandomInt(getHeight());
addObject(new Camel, x, y);
(2) removing the 'getRandomInt' method and just using 'Greenfoot.getRandomNumber' in place of it (it is just an extra method whose function is to call another method, which can be done directly):
1
2
3
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Camel(), x, y);
alittle_bit alittle_bit

2016/11/25

#
Thank you for the replies Hippo & danpost! I will remember to return the value given by the method if I am creating an int, boolean, etc, in future then.. On another note, the method was removed and the randomised position was achieved through placing the 'getRandomNumber()' method inside the 'addObject()' method. As always, I appreciate the hard work you do supporting others danpost, thanks for the thought put into other ways I could achieve my goal too and thanks hippo for the straightforward and clear reply too!
You need to login to post a reply.