On this page:
Problem 1
8.6

Assignment 2: Delegation

Goals: Practice designing methods for interfaces, and employing delegation.

You should submit one .java file containing the solution to this problem.

Be sure to properly test your code and write purpose statements for your methods. A lack of tests and documentation will result in a lower grade! Remember that testing requires you to make some examples of data in an examples class.

If a method is declared in an interface and the implementing class’s method does not require extrapolation outside of the purpose statement in the interface, you do not need to provide a purpose statement for the method inside the class.

Remember to delegate to the class/interface that is primarily responsible for a given computation! Classes should talk to each other through methods, not through fields.

Problem 1

Here are some classes that represent pets and pet owners:

// a pet owner class Person {
String name;
IPet pet;
int age; // in years  
Person(String name, IPet pet, int age) {
this.name = name;
this.pet = pet;
this.age = age;
}
}
 
// a pet interface IPet {
}
 
// a pet cat class Cat implements IPet {
String name;
int age; // in years  
Cat(String name, int age) {
this.name = name;
this.age = age;
}
}
 
// a pet dog class Dog implements IPet {
String name;
int age; // in years  
Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
 
// no pet class NoPet implements IPet {
}