...
Section |
---|
|
Column |
---|
| Code Block |
---|
|
public class Cat {
private int age;
private String name;
private Cat friend;
public Cat(String name, int age) {
this.name = name;
this.age = age;
this.friend = null;
}
public String getName() {
return name;
}
public Cat getFriend() {
return friend;
}
public void setFriend(Cat friend) {
this.friend = friend;
}
public String sayHello(String aboutMe) {
return "Hello World! My name is " + name
+ " and I am " + age
+ " years old."
+ " I am " + aboutMe + ".";
}
}
|
|
Column |
---|
| Code Block |
---|
|
public class CatsTheMusical {
public static void main(String[] args) {
Cat mist = new Cat("Mr. Mistoffelees", 8);
System.out.println(mist.sayHello("magical"));
Cat garf = new Cat("Garfield", 12);
System.out.println(garf.sayHello("tired/hungry"));
mist.setFriend(garf);
System.out.println(mist.getName() + " and " + garf.getName()
+ " are friends? " + areFriends(mist, garf));
garf.setFriend(mist);
System.out.println(mist.getName() + " and " + garf.getName()
+ " are friends? " + areFriends(mist, garf));
}
public static boolean areFriends(Cat cat1, Cat cat2) {
if if (cat1.getFriend() != null && cat2.getFriend() != null) {
return cat1.getFriend() == cat2 && cat2.getFriend() == cat1;
}
return false;
}
}
|
|
|
Lets make sure we understand how this works. First, lets talk about the contents of the files Cat.java
.
...