public class PersonDemo
{
public static void main(String[] args)
{
Person sara = new Person("Sara", "sara.jpg", 10, 200);
Person chenghan = new Person("Cheng-Han", "cheng-han.png", 300, 0);
Person cay = new Person("Cay", "cay.gif", 250, 180);
}
}
// Bluej project: lesson3/friends4
public class Person
{
private String name;
private String friends;
private int x;
private int y;
public Person (String aName, String pictureName, int xCoord, int yCoord)
{
name = aName;
friends = "";
x = xCoord;
y = yCoord;
Picture picture = new Picture(pictureName);
picture.translate(xCoord, yCoord);
picture.draw();
}
public void addFriend(Person friend)
{
friends = friends + friend.name + " ";
SmallCircle circle = new SmallCircle(x, y);
circle.fill();
Line line = new Line(x, y, friend.x, friend.y);
line.draw();
}
public void unfriend(Person nonFriend)
{
friends = friends.replace(nonFriend.name + " ", "");
}
public String getFriends()
{
return friends;
}
}
The this Reference
public double drive(double distance)
{
this.mimlesDriven = this.milesDriven + distance
}
public Person (String name, String pictureName, int x, int y)
{
this.name = name;
this.friends = "";
this.x = x;
this.y = y;
Picture picture = new Picture(pictureName);
picture.translate(x, y);
picture.draw();
}