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

2018/11/27

how to create instance variables

ericnjaramba ericnjaramba

2018/11/27

#
The following is part of my homework that i need help with, i just need help with the second paragraph i put in the first to show how the assignment starts. Create a world that is 1000 x 600 in size. Write code in the World subclass constructor (or in the prepare() method called from the constructor) to create and place all the objects in the world. There should be 2 Person objects positioned at (100,300) and (900,300). There should be 5 Cactus objects positioned at (100,100), (900,100), (100,500), (900,500), and (500,300). There should be 5 Bee objects positioned at (500,100), (500,500), (300,300), (700,300), and (500,300). The Person class needs three instance variables to keep track of whether the instance is keyboard controlled or moves randomly, how many bees have been caught (initialized to 0), and the name of the person. A Person constructor needs to be created and have two parameters, so that when you create a new instance you can pass in true or false as to whether the person is keyboard controlled or not, and assign the name of the person. One of the Persons you instantiate should be keyboard controlled and have the name of "User 1". The other Person instance should not be keyboard controlled and should have the name of "Computer".
danpost danpost

2018/11/27

#
ericnjaramba wrote...
The following is part of my homework that i need help with, i just need help with the second paragraph i put in the first to show how the assignment starts. << Part 1 Omitted >> The Person class needs three instance variables to keep track of whether the instance is keyboard controlled or moves randomly, how many bees have been caught (initialized to 0), and the name of the person. A Person constructor needs to be created and have two parameters, so that when you create a new instance you can pass in true or false as to whether the person is keyboard controlled or not, and assign the name of the person. One of the Persons you instantiate should be keyboard controlled and have the name of "User 1". The other Person instance should not be keyboard controlled and should have the name of "Computer".
See this page of the Java tutorials.
ericnjaramba ericnjaramba

2018/11/28

#
so i checked out that page about variables and ,my question is how to create a variable to keep track of wether the instance is keyboard controlled or moves randomly.
danpost danpost

2018/11/28

#
ericnjaramba wrote...
so i checked out that page about variables and ,my question is how to create a variable to keep track of wether the instance is keyboard controlled or moves randomly.
Well, it is either keyboard controlled or not (two possibilities). So, a boolean field in the class of the controlled actor seems to be what is needed. If there is a possibility that the controlled actor might change to be a different instance, then an Actor field in your World subclass could do the trick (or a static one in the class of the controlled actor).
You need to login to post a reply.