PDA

View Full Version : Returning variables and passing variables to other classes


mickeyho
12-15-2008, 07:37 PM
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.*/


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.

Fou-Lu
12-16-2008, 12:30 AM
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.

class LeaderTurtle extends Turtle
{
protected Vector<FollowerTurtle> followersVector = new Vector<FollowerTurtle>();
}

Then in makefollowers you'd simply add to the vector:

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.