From 39a4291ddcf72f82ed492241692c6998dad3eda7 Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 5 Nov 2024 19:44:36 -0500 Subject: [PATCH] Added delegation. Also added .project files. --- .gitignore | 1 - delegation/.project | 17 +++ delegation/src/delegation/Main.java | 229 ++++++++++++++++++++++++++++ mastermind/.project | 17 +++ 4 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 delegation/.project create mode 100644 delegation/src/delegation/Main.java create mode 100644 mastermind/.project diff --git a/.gitignore b/.gitignore index 64c4b68..8e84886 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,5 @@ replay_pid* **/.metadata/ **/.settings/ **/.classpath -**/.project **/bin/ .old/ diff --git a/delegation/.project b/delegation/.project new file mode 100644 index 0000000..25e0c6f --- /dev/null +++ b/delegation/.project @@ -0,0 +1,17 @@ + + + delegation + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/delegation/src/delegation/Main.java b/delegation/src/delegation/Main.java new file mode 100644 index 0000000..9543429 --- /dev/null +++ b/delegation/src/delegation/Main.java @@ -0,0 +1,229 @@ +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) + && t.checkExpect(mingeAndHorb.totalHumanAge(), 2460); + } +} + +// 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; } +} diff --git a/mastermind/.project b/mastermind/.project new file mode 100644 index 0000000..7ba2a68 --- /dev/null +++ b/mastermind/.project @@ -0,0 +1,17 @@ + + + mastermind + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + +