Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Section
borderfalse
Column

Column
width500px
Code Block
1 linenumbers
titleCat.java
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 Cat(String name, int age, Cat friendgetName(){
    this.name = name;
    this.age = age;
    this.friend = friend;
  }

  public String getName(){
    return name;
  }

  public String getFriend(){
    if(friend == null){
      return "I am have no friends :(";
    }
    else{
      return "My friend is " + friend.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
width10px

Column
width500px
Code Block
titleCatsTheMusical.java
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(cat1.getFriend() != null && cat2.getFriend() != null){
			return cat1.getFriend() == cat2 && cat2.getFriend() == cat1;
		}
		return false;
	}
}
Column

Lets make sure we understand how this works. First, lets talk about the contents of the files Cat.java.

...