// CS U213 Fall 2008 // Lab 3 // Problem 3.3 // September 25, 2008 /* +------+ | IPet | +------+ / \ --- | ----------------------------------- | | | +--------------+ +--------------+ +--------------+ | Cat | | Dog | | Gerbil | +--------------+ +--------------+ +--------------+ | String name | | String name | | String name | | String owner | | String owner | | String owner | | boolean hair | | String breed | | boolean male | +--------------+ +--------------+ +--------------+ */ // to represent a pet interface IPet{ // does the owner of this animal have the given name? boolean isOwner(String owner); // does this pet have the same name as that given pet? boolean sameName(IPet that); // is this pet's name the given name? boolean isName(String name); } // to represent a cat class Cat implements IPet{ String name; String owner; boolean hair; // short haired? Cat(String name, String owner, boolean hair){ this.name = name; this.owner = owner; this.hair = hair; } /* TEMPLATE: FIELDS: ... this.name ... -- String ... this.owner ... -- String ... this.hair ... -- boolean METHODS: ... this.isOwner(String) ... -- boolean ... this.sameName(IPet) ... -- boolean ... this.isName(String) ... -- boolean */ // does the owner of this animal have the given name? boolean isOwner(String owner){ return this.owner.equals(owner); } // does this cat have the same name as that given pet? boolean sameName(IPet that){ /* add to the template: ... that.isOwner(String) ... -- boolean ... that.sameName(IPet) ... -- boolean ... that.isName(String) ... -- boolean */ return that.isName(this.name); } // is this pet's name the given name? boolean isName(String name){ return this.name.equals(name); } } // to represent a dog class Dog implements IPet{ String name; String owner; String breed; Dog(String name, String owner, String breed){ this.name = name; this.owner = owner; this.breed = breed; } /* TEMPLATE: FIELDS: ... this.name ... -- String ... this.owner ... -- String ... this.breed ... -- String METHODS: ... this.isOwner(String) ... -- boolean ... this.sameName(String) ... -- boolean ... this.isName(String) ... -- boolean */ // does the owner of this animal have the given name? boolean isOwner(String owner){ return this.owner.equals(owner); } // does this cat have the same name as that given pet? boolean sameName(IPet that){ /* add to the template: ... that.isOwner(String) ... -- boolean ... that.sameName(IPet) ... -- boolean ... that.isName(String) ... -- boolean */ return that.isName(this.name); } // is this pet's name the given name? boolean isName(String name){ return this.name.equals(name); } } // to represent a gerbil class Gerbil implements IPet{ String name; String owner; boolean male; Gerbil(String name, String owner, boolean male){ this.name = name; this.owner = owner; this.male = male; } /* TEMPLATE: FIELDS: ... this.name ... -- String ... this.owner ... -- String ... this.male ... -- boolean METHODS: ... this.isOwner(String) ... -- boolean ... this.sameName(String) ... -- boolean ... this.isName(String) ... -- boolean */ // does the owner of this animal have the given name? boolean isOwner(String owner){ return this.owner.equals(owner); } // does this cat have the same name as that given pet? boolean sameName(IPet that){ /* add to the template: ... that.isOwner(String) ... -- boolean ... that.sameName(IPet) ... -- boolean ... that.isName(String) ... -- boolean */ return that.isName(this.name); } // is this pet's name the given name? boolean isName(String name){ return this.name.equals(name); } } class Examples{ Examples(){} // Examples of IPet data IPet doggie = new Dog("Toto", "Pete", "Mutt"); IPet bigdog = new Dog("Spot", "Jane", "Labrador"); IPet kitty = new Cat("Sleepy", "Pete", true); IPet boots = new Cat("Boots", "Pat", false); IPet sleepy = new Gerbil("Sleepy", "Mike", true); IPet spot = new Gerbil("Spot", "Mary", false); // tests for the method isOwner for the IPet class hierarchy boolean testIsOwner = (check this.doggie.isOwner("Pete") expect true) && (check this.doggie.isOwner("Jane") expect false) && (check this.kitty.isOwner("Pete") expect true) && (check this.kitty.isOwner("Jane") expect false) && (check this.sleepy.isOwner("Mike") expect true) && (check this.sleepy.isOwner("Pete") expect false); // tests for the method sameName for the IPet class hierarchy boolean testSameName = (check this.bigdog.sameName(this.spot) expect true) && (check this.doggie.sameName(this.bigdog) expect false) && (check this.kitty.sameName(this.sleepy) expect true) && (check this.kitty.sameName(this.doggie) expect false) && (check this.sleepy.sameName(this.kitty) expect true) && (check this.spot.sameName(this.bigdog) expect true)&& (check this.sleepy.sameName(this.spot) expect false); }