Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Looping method problem.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    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;
     
    }

    Thanks in advance!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Looping method problem.

    if(dierGeplaatst==false){
    I am pretty sure that it will always return 0, 0.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Looping method problem.

    Quote Originally Posted by Mr.777 View Post
    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?

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    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?

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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[][]?

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping method problem.

    Maybe make a method getBoolean and setBoolean within ecosysteem.java?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    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()

  11. #11
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    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?

  13. #13
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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;}

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

    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()
    Last edited by Norm; December 29th, 2011 at 09:17 AM.

  15. #15
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    These methods are only in Konijn and Vos.
    Then you can only use references to those two classes to call those methods.

  17. #17
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping method problem.

    but the content of the IDier[][] ecosysteem are object of those classes?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  19. #19
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping method problem.

    ecosysteem's type is an IDier[][].

    ecosysteem = new IDier[h][b];

  20. #20
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  22. #22
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    Put a method in the base class that will keep track.

  24. The Following User Says Thank You to Norm For This Useful Post:

    Swen (December 29th, 2011)

  25. #24
    Junior Member
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping method problem.

    But won't that only work when I put it in the IDier interface?

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping method problem.

    Yes, the IDier class needs to have those methods defined.

Similar Threads

  1. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 05:44 PM
  2. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  3. Looping / OutOfBounds code problem
    By thisbeme in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2011, 07:10 AM
  4. looping method
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 09:19 PM
  5. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM