Re: Looping method problem.
Code :
if(dierGeplaatst==false){
I am pretty sure that it will always return 0, 0.
Re: Looping method problem.
Play computer with your code, do the statements one by one, keeping track of the values of the variables as you proceed and see what the code does.
Re: Looping method problem.
Quote:
Originally Posted by
Mr.777
Code :
if(dierGeplaatst==false){
I am pretty sure that it will always return 0, 0.
My bad :(
So, what are the test cases you apply on it and it behaves different?
Do you know how it behaves?
And how do you want it to behave?
Re: Looping method problem.
I have figured it out. The problem is that it doesnt generate new random numbers so it won't be true. Solved! Haha thanks anyways!
Re: Looping method problem.
I have an other question however:
I got an interface, IDier(Ianimal), wich is implemented by Dier(animal), wich is extended by Vos(fox) and Konijn(rabbit). I need to keep track of the Foxes and Rabbits that I moved. So I need a boolean! But how do I reach the boolean since the Foxes and Rabbit are in a IDier[][] array.
Re: Looping method problem.
Quote:
how do I reach the boolean since the Foxes and Rabbit are in a IDier[][] array.
Can you post a code structure that shows the problem?
Where is the boolean variable?
From where do you need to see the value of the variable?
Re: Looping method problem.
Alright I'll sum, it up.
Two interfaces,
Idier.java - This is the interface for the animals
Dier.java - implements the IDier interface
Konijn.java - extends Dier.java
Vos.java - extends Dier.java
IEcosysteem.java -This is the interface to create the world
Ecosysteem.java - implements IEcosysteem and is used to create the world. The world is a IDier[][].
Now what I want is to have boolean in every instance of Dier(so in every Vos of Konijn). How can I reach that trough the IDier[][]?
Re: Looping method problem.
Maybe make a method getBoolean and setBoolean within ecosysteem.java?
Re: Looping method problem.
Quote:
How can I reach that through the IDier[][]?
If the array IDier contains objects with the get/setBoolean methods, you use indexes to get the one element in the array you want to work with:
IDier[i][j].get/setBoolean()
Re: Looping method problem.
I've put the methods in Konijn and Vos. When compilating I get this error:
estrun.java:16: cannot find symbol
symbol : method getVerplaatst()
location: interface IDier
if(w.ecosysteem[q][t].getVerplaatst()=false){
^
Testrun.java:17: cannot find symbol
symbol : method setVerplaats()
location: interface IDier
w.ecosysteem[q][t].setVerplaats();
^
W is an Ecosysteem class.
Re: Looping method problem.
Quote:
cannot find symbol
symbol : method getVerplaatst()
Where are the methods in the error messages defined?
What data type is the variable: ecosysteem?
Does it have a getVerplaatst() method?
Re: Looping method problem.
ecosysteem is an Idier[][] created in Ecosysteem.java.
It doenst have a getVerplaatst method. I put that in Vos.java and Konijn.java.
These are the methods:
public boolean getVerplaatst(){return verplaatst;}
public void setVerplaatst(){verplaatst=!verplaatst;}
Re: Looping method problem.
What data type is the variable: ecosysteem?
Is it an int or a String or one of your classes or what?
Quote:
It doenst have a getVerplaatst method
Then you can not use the ecosysteem variable as a reference to call that method. The following will not work unless getVerplaatst() is a method in whatever type/class ecosysteem is.
ecosysteem[q][t].getVerplaatst()
Re: Looping method problem.
Ecosysteem.java is a class that has a constructor for the IDier[][] ecosysteem. These methods are only in Konijn and Vos.
Re: Looping method problem.
Quote:
These methods are only in Konijn and Vos.
Then you can only use references to those two classes to call those methods.
Re: Looping method problem.
but the content of the IDier[][] ecosysteem are object of those classes?
Re: Looping method problem.
You still have not answered my question:
What data type is the variable: ecosysteem?
Does that data type/class have the methods that are not being found?
If you do not know the datatype of ecosysteem, show the definition for that variable.
Re: Looping method problem.
ecosysteem's type is an IDier[][].
Quote:
ecosysteem = new IDier[h][b];
Re: Looping method problem.
But I cannot change the interface. I put the boolean in dier wich implements IDier. so on a ecosysteem[x][y] there should be a Dier class found.
Re: Looping method problem.
The base class IDier does not have the method so you can not use an IDier object to get to methods in an sub class.
You could cast the object from the array to be the class that has the method, but if the array has a mixture of classes this will fail.
Re: Looping method problem.
It does... that's to bad. Thank you!
So what's a solution to keep track wich IDier[x][y] has already gone trough the move method?
Re: Looping method problem.
Put a method in the base class that will keep track.
Re: Looping method problem.
But won't that only work when I put it in the IDier interface?
Re: Looping method problem.
Yes, the IDier class needs to have those methods defined.