Fixed .gitignore for the last time.
This commit is contained in:
@@ -1,209 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,223 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
|
||||
public int totalHumanAge() { return 0; }
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
|
||||
public int totalHumanAge() {
|
||||
return this.first.totalAge() + this.rest.totalHumanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
|
||||
// Sum of age and pet's age (in human years).
|
||||
int totalAge() {
|
||||
return this.age + this.pet.humanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,227 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
|
||||
boolean testTotalAge(Tester t) {
|
||||
return t.checkExpect(horb.totalAge(), 408 + 9);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
|
||||
public int totalHumanAge() { return 0; }
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
|
||||
public int totalHumanAge() {
|
||||
return this.first.totalAge() + this.rest.totalHumanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
|
||||
// Sum of age and pet's age (in human years).
|
||||
int totalAge() {
|
||||
return this.age + this.pet.humanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,228 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
|
||||
boolean testTotalHumanAge(Tester t) {
|
||||
return t.checkExpect(unpopulated.totalHumanAge(), 0)
|
||||
&& t.checkExpect(onlyHorb.totalHumanAge(), hekOlder.humanAge() + horb.age);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
|
||||
public int totalHumanAge() { return 0; }
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
|
||||
public int totalHumanAge() {
|
||||
return this.first.totalAge() + this.rest.totalHumanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
|
||||
// Sum of age and pet's age (in human years).
|
||||
int totalAge() {
|
||||
return this.age + this.pet.humanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,248 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,208 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
public class Main {
|
||||
|
||||
}
|
@@ -1,212 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
|
||||
// Calculate the age in human years.
|
||||
int humanAge();
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,218 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
|
||||
public int totalHumanAge() { return 0; }
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
|
||||
public int totalHumanAge() {
|
||||
return this.first.totalAge() + this.rest.totalHumanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,239 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,212 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,209 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,245 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,247 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.hasPet() && this.name.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
@@ -1,243 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.hasPet() && this.name.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
@@ -1,248 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.hasPet() && this.name.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
package mastermind;
|
||||
|
||||
public class Main {
|
||||
|
||||
}
|
@@ -1,236 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.hasPet() && this.name.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
@@ -1,228 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
|
||||
boolean testTotalHumanAge(Tester t) {
|
||||
return t.checkExpect(unpopulated.totalHumanAge(), 0)
|
||||
&& t.checkExpect(onlyHorb.totalHumanAge(), 2457);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
|
||||
// Calculate the total age in human years of all humans and pets.
|
||||
int totalHumanAge();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
|
||||
public int totalHumanAge() { return 0; }
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
|
||||
public int totalHumanAge() {
|
||||
return this.first.totalAge() + this.rest.totalHumanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
|
||||
// Sum of age and pet's age (in human years).
|
||||
int totalAge() {
|
||||
return this.age + this.pet.humanAge();
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name? This would be better implemented as an
|
||||
// abstract class, but in keeping with the spirit of the assignment, it
|
||||
// shall reside in the interface.
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,215 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,206 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.pet.hasName(s);
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.pet.hasName(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return name.equals(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public int humanAge() { return 0; }
|
||||
public boolean hasName(String name) { return false; }
|
||||
}
|
@@ -1,239 +0,0 @@
|
||||
package delegation;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
IPet foot = new Dog("Foot", 44);
|
||||
IPet footOlder = new Dog("Foot", 45);
|
||||
IPet hek = new Cat("Hek", 408);
|
||||
IPet hekOlder = new Cat("Hek", 409);
|
||||
IPet unpet = new NoPet();
|
||||
IPet stephenDog = new Dog("Stephen", 93);
|
||||
|
||||
Person horb = new Person("Horb", hek, 9);
|
||||
Person horbOlder = new Person("Horb", hekOlder, 10);
|
||||
Person minge = new Person("Minge", unpet, 3);
|
||||
Person mingeOlder = new Person("Minge", unpet, 4);
|
||||
Person wrist = new Person("Wrist", foot, 60);
|
||||
Person wristOlder = new Person("Wrist", footOlder, 61);
|
||||
Person stephenPerson = new Person("Stephen", stephenDog, 88);
|
||||
|
||||
ILoPerson unpopulated = new MtLoPerson();
|
||||
ILoPerson onlyHorb = new ConsLoPerson(horb, unpopulated);
|
||||
ILoPerson mingeAndHorb = new ConsLoPerson(minge, onlyHorb);
|
||||
ILoPerson mingeAndHorbOneYearLater =
|
||||
new ConsLoPerson(mingeOlder,
|
||||
new ConsLoPerson(horbOlder, unpopulated));
|
||||
ILoPerson mingeAndStephen = new ConsLoPerson(minge, new ConsLoPerson(stephenPerson, unpopulated));
|
||||
|
||||
boolean testPetOlder(Tester t) {
|
||||
return t.checkExpect(foot.older(), footOlder)
|
||||
&& t.checkExpect(hek.older(), hekOlder)
|
||||
&& t.checkExpect(unpet, unpet);
|
||||
}
|
||||
|
||||
boolean testPersonOlder(Tester t) {
|
||||
return t.checkExpect(horb.older(), horbOlder)
|
||||
&& t.checkExpect(minge.older(), mingeOlder)
|
||||
&& t.checkExpect(wrist.older(), wristOlder);
|
||||
}
|
||||
|
||||
boolean testPetGetName(Tester t) {
|
||||
return t.checkExpect(hek.getName(), new YesString("Hek"))
|
||||
&& t.checkExpect(unpet.getName(), new NoString())
|
||||
&& t.checkExpect(foot.getName(), new YesString("Foot"));
|
||||
}
|
||||
|
||||
boolean testPersonSamePetName(Tester t) {
|
||||
return t.checkExpect(horb.samePetName("Hek"), true)
|
||||
&& t.checkExpect(horb.samePetName("Minkus"), false)
|
||||
&& t.checkExpect(minge.samePetName("8"), false);
|
||||
}
|
||||
|
||||
boolean testILoPersonOlder(Tester t) {
|
||||
return t.checkExpect(unpopulated.older(), unpopulated)
|
||||
&& t.checkExpect(mingeAndHorb.older(), mingeAndHorbOneYearLater);
|
||||
}
|
||||
|
||||
boolean testPetHumanAge(Tester t) {
|
||||
return t.checkExpect(hek.humanAge(), 2448)
|
||||
&& t.checkExpect(stephenDog.humanAge(), 651)
|
||||
&& t.checkExpect(unpet.humanAge(), 0);
|
||||
}
|
||||
|
||||
boolean testPersonPetHasSameName(Tester t) {
|
||||
return t.checkExpect(wrist.petHasSameName(), false) &&
|
||||
t.checkExpect(minge.petHasSameName(), false) &&
|
||||
t.checkExpect(stephenPerson.petHasSameName(), true);
|
||||
|
||||
}
|
||||
|
||||
boolean testAnyNarcissists(Tester t) {
|
||||
return t.checkExpect(mingeAndHorbOneYearLater.anyNarcissists(), false) &&
|
||||
t.checkExpect(mingeAndStephen.anyNarcissists(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of people.
|
||||
interface ILoPerson {
|
||||
// Increase each member person's and their pet's ages by one.
|
||||
ILoPerson older();
|
||||
|
||||
// Does anyone share a name with their pet?
|
||||
boolean anyNarcissists();
|
||||
}
|
||||
|
||||
class MtLoPerson implements ILoPerson {
|
||||
public ILoPerson older() { return this; };
|
||||
|
||||
public boolean anyNarcissists() { return false; };
|
||||
}
|
||||
|
||||
class ConsLoPerson implements ILoPerson {
|
||||
Person first;
|
||||
ILoPerson rest;
|
||||
|
||||
ConsLoPerson(Person first, ILoPerson rest) {
|
||||
this.first = first;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILoPerson older() {
|
||||
return new ConsLoPerson(this.first.older(), this.rest.older());
|
||||
}
|
||||
|
||||
public boolean anyNarcissists() {
|
||||
return this.first.petHasSameName() || this.rest.anyNarcissists();
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Increase the age of a person and their pet by one year.
|
||||
Person older() {
|
||||
return new Person(this.name, this.pet.older(), this.age + 1);
|
||||
}
|
||||
|
||||
// Do they have a pet (and not an unpet)?
|
||||
boolean hasPet() {
|
||||
return this.pet.isPet();
|
||||
}
|
||||
|
||||
// Whether a person's pet's name's the same as the given string.
|
||||
boolean samePetName(String s) {
|
||||
return this.hasPet() && s.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
|
||||
// Return whether they've the same name their pet.
|
||||
boolean petHasSameName() {
|
||||
return this.hasPet() && this.name.equals(this.pet.getName().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
//a pet
|
||||
interface IPet {
|
||||
// Increase the age of a pet by one year.
|
||||
IPet older();
|
||||
boolean isPet();
|
||||
// Return the name of the pet (maybe).
|
||||
IMaybeString getName();
|
||||
// Age of pet in human years.
|
||||
int humanAge();
|
||||
|
||||
// Does the pet have the given name?
|
||||
boolean hasName(String name);
|
||||
}
|
||||
|
||||
//a pet cat
|
||||
class Cat implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Cat(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Cat older() {
|
||||
return new Cat(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 6;
|
||||
}
|
||||
}
|
||||
|
||||
//a pet dog
|
||||
class Dog implements IPet {
|
||||
String name;
|
||||
int age; // in years
|
||||
|
||||
Dog(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
public Dog older() {
|
||||
return new Dog(this.name, this.age + 1);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPet() { return true; }
|
||||
|
||||
public IMaybeString getName() {
|
||||
return new YesString(this.name);
|
||||
}
|
||||
|
||||
public int humanAge() {
|
||||
return this.age * 7;
|
||||
}
|
||||
}
|
||||
|
||||
//no pet
|
||||
class NoPet implements IPet {
|
||||
public NoPet older() { return this; }
|
||||
public boolean isPet() { return false; }
|
||||
public IMaybeString getName() { return new NoString(); }
|
||||
public int humanAge() { return 0; }
|
||||
}
|
||||
|
||||
// Maybe a string.
|
||||
interface IMaybeString {
|
||||
boolean isString();
|
||||
String unwrap();
|
||||
}
|
||||
|
||||
// No string.
|
||||
class NoString implements IMaybeString {
|
||||
public boolean isString() { return false; }
|
||||
|
||||
// This is an exception and should cause the program to crash, but I don't know how to do that cleanly.
|
||||
public String unwrap() { int i = 0/0; return "what have you done 😔"; };
|
||||
}
|
||||
|
||||
// Yes string.
|
||||
class YesString implements IMaybeString {
|
||||
String s;
|
||||
|
||||
YesString(String s) { this.s = s; }
|
||||
|
||||
public boolean isString() { return true; }
|
||||
public String unwrap() { return this.s; }
|
||||
}
|
Reference in New Issue
Block a user