Looping method problem.

• December 29th, 2011, 06:09 AM
Swen
Looping method problem.
Hello everybody! I'm new to this forum and also not very experienced in Java. I follow it as a course in my study!

I'm currently working on a assignment wich is to create an ecosystem.
I need a method that moves my objects to a random empty(null) space in a two dimensional array(wich is the grid).
This is what I came up with, at the moment I let it print out the x and y step for testing purposes. It only needs to return an int[] with the x and y direction step. The argument it gets is a 3x3 grid containing an object form my IDier class in the middle. I let a for loop find al objects in the grid and put them into the move method. However it doesn't returns something for every object. Why is this?

Code :

```public int[] loop(IDier[][] omgeving){ boolean dierGeplaatst=false; Random generator = new Random(); int x= generator.nextInt(3)-1; int y= generator.nextInt(3)-1; int[] richting={0,0}; if(dierGeplaatst==false){ if(omgeving[1+x][1+y] == null){ System.out.println(x+","+y); richting[0]=x; richting[1]=y; dierGeplaatst=true; } else{ dierGeplaatst= false; }} return richting;   }```

• December 29th, 2011, 06:54 AM
Mr.777
Re: Looping method problem.
Code :

`if(dierGeplaatst==false){`
I am pretty sure that it will always return 0, 0.
• December 29th, 2011, 06:56 AM
Norm
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.
• December 29th, 2011, 06:59 AM
Mr.777
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.

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?
• December 29th, 2011, 07:40 AM
Swen
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!
• December 29th, 2011, 07:44 AM
Swen
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.
• December 29th, 2011, 07:46 AM
Norm
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?
• December 29th, 2011, 07:51 AM
Swen
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[][]?
• December 29th, 2011, 07:52 AM
Swen
Re: Looping method problem.
Maybe make a method getBoolean and setBoolean within ecosysteem.java?
• December 29th, 2011, 07:57 AM
Norm
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()
• December 29th, 2011, 08:03 AM
Swen
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.
• December 29th, 2011, 08:11 AM
Norm
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?
• December 29th, 2011, 08:13 AM
Swen
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;}
• December 29th, 2011, 08:15 AM
Norm
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()
• December 29th, 2011, 08:17 AM
Swen
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.
• December 29th, 2011, 08:18 AM
Norm
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.
• December 29th, 2011, 08:26 AM
Swen
Re: Looping method problem.
but the content of the IDier[][] ecosysteem are object of those classes?
• December 29th, 2011, 08:29 AM
Norm
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.
• December 29th, 2011, 08:31 AM
Swen
Re: Looping method problem.
ecosysteem's type is an IDier[][].

Quote:

ecosysteem = new IDier[h][b];
• December 29th, 2011, 08:32 AM
Swen
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.
• December 29th, 2011, 08:36 AM
Norm
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.
• December 29th, 2011, 08:39 AM
Swen
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?
• December 29th, 2011, 08:40 AM
Norm
Re: Looping method problem.
Put a method in the base class that will keep track.
• December 29th, 2011, 08:46 AM
Swen
Re: Looping method problem.
But won't that only work when I put it in the IDier interface?
• December 29th, 2011, 08:49 AM
Norm
Re: Looping method problem.
Yes, the IDier class needs to have those methods defined.