Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Returning variables and passing variables to other classes

    Code:
    public class LeaderTurtle extends Turtle
    {
      
      
      public LeaderTurtle(World w, int xStart, int yStart)
      {
      super (w);
      moveTo(xStart, yStart);
      }
      
      public FollowerTurtle[] makeFollowers(World w, int xStart, int yStart)
      {
        /* LeaderTurtle uses this method to create 5 FollowerTurtle objects. It places
    them in their initial locations – in a line, each 20 pixels apart. The first follower
    should be placed at xStart, yStart. You choose whether the line is horizontal
    or vertical. It returns newly created array of the 5 FollowerTurtles. */
       
       FollowerTurtle follower1 = new FollowerTurtle(w, xStart, yStart); 
       FollowerTurtle follower2 = new FollowerTurtle(w, xStart, yStart+20); 
       FollowerTurtle follower3 = new FollowerTurtle(w, xStart, yStart+40); 
       FollowerTurtle follower4 = new FollowerTurtle(w, xStart, yStart+60); 
       FollowerTurtle follower5 = new FollowerTurtle(w, xStart, yStart+80);
       
       FollowerTurtle[] followersArray = new FollowerTurtle[]
       {follower1,follower2,follower3,follower4,follower5};
       System.out.println(followersArray);
       return (followersArray);
       }
       
    public void followMe()
    {
      
    for ( int index=0; index<4; index++)
    
    {
      followersArray[2].follow(); 
    }
    }
      
      
    
    }
      /* loops through all FollowerTurtle objects created in makeFollowers(), telling
    them to “follow()”. Note that if this method is called before makeFollowers,
    then this method should do nothing since the FollowerTurtle objects have yet
    to be created.*/
    Code:
    public class FollowerTurtle extends Turtle
    {
    public FollowerTurtle(World w, int xStart, int yStart)
    {
      super(w);
      moveTo(xStart, yStart);
    }
    
    public void follow()
    {
      //int x=LeaderTurtle.getXPos();
      //int y=LeaderTurtle.getYPos();
      //double leaderHeading = LeaderTurtle.getHeading();
      //this.setHeading(leaderHeading);
    
      /* This method moves the FollowerTurtle to mimic the difference between the
    leader?s current position and its last known position. That is, if the leader
    moves five to the right, and two up, then the FollowerTurtle should do the
    same. Also, this method should turn the FollowerTurtle so it is facing the
    same direction as the leader. */
     //while(name.turnLeft()==true)
    
    }
    }
    These are my two classes. for some reason method followMe() isn't recognizing followersArray. Am I not returning correctly? Also is there a way which I can get the Turtle Leader's position and such?

    Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Does calling you're turtle constructor with super or moveto make a call to the makefollowers and store them in a protected variable?

    I'd put the followersArray as a vector of followerturtle in the LeaderTurtle class.
    Code:
    class LeaderTurtle extends Turtle
    {
        protected Vector<FollowerTurtle> followersVector = new Vector<FollowerTurtle>();
    }
    Then in makefollowers you'd simply add to the vector:
    Code:
    followersVector.add(new FollowerTurtle(w, xStart, yStart));
    Anyway, it looks like its just a scoping problem. I don't see where the makeFollowers method is being called and stored, so it has to be coming from the Turtle class and stored in a protected / public followersArray. Otherwise, you could simply call makeFollowers when you call the followMe method. You could actually do this as a linked list as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •