From e21bcc8b28b398b6532d8e8b1a5e83c28b3cde8c Mon Sep 17 00:00:00 2001 From: jenn-182 Date: Mon, 21 Jul 2025 12:10:33 -0400 Subject: [PATCH 1/2] FInished --- JennStaci.md | 0 .../zipcodewilmington/assessment1/Animal.java | 1 + .../zipcodewilmington/assessment1/Cat.java | 9 ++- .../zipcodewilmington/assessment1/Dog.java | 12 +++- .../zipcodewilmington/assessment1/Pet.java | 39 ++++++++++- .../assessment1/PetOwner.java | 64 +++++++++++++++++-- 6 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 JennStaci.md diff --git a/JennStaci.md b/JennStaci.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/com/zipcodewilmington/assessment1/Animal.java b/src/main/java/com/zipcodewilmington/assessment1/Animal.java index dcb22e4..8a2fd89 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Animal.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Animal.java @@ -4,5 +4,6 @@ * Created by leon on 2/16/18. */ public interface Animal { + String speak(); } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Cat.java b/src/main/java/com/zipcodewilmington/assessment1/Cat.java index 1cf2894..7a1dcca 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Cat.java @@ -10,12 +10,16 @@ public class Cat extends Pet { */ public Cat(String name, Integer age) { + super(name, age); + } /** * @param age age of this Cat */ public Cat(Integer age) { + + super(age); } /** @@ -23,6 +27,8 @@ public Cat(Integer age) { */ public Cat(String name) { + super(name); + } /** @@ -32,12 +38,13 @@ public Cat(String name) { * age is 0 */ public Cat() { + super(); } /** * @return meow as a string */ public String speak() { - return null; + return "meow"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Dog.java b/src/main/java/com/zipcodewilmington/assessment1/Dog.java index bca1c07..4b642cd 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Dog.java @@ -9,6 +9,8 @@ public class Dog extends Pet { * @param age age of this dog */ public Dog(String name, Integer age) { + + super(name, age); } @@ -16,13 +18,16 @@ public Dog(String name, Integer age) { * @param age age of this dog */ public Dog(Integer age) { + + super(age); } /** * @param name name of this dog */ public Dog(String name) { - + + super(name); } /** @@ -32,12 +37,15 @@ public Dog(String name) { * age is 0 */ public Dog() { + + super(); } /** * @return bark as a string */ public String speak() { - return null; + + return "bark"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/Pet.java index afc3e99..cb73e8c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Pet.java @@ -8,13 +8,28 @@ public abstract class Pet implements Animal { * nullary constructor * by default, pet has age of 0; name of ""; */ + + //define instance variables + //name, age, owner + private String name; + private Integer age; + private PetOwner owner; + + public Pet() { + + //default age of 0 and name of "" + this("",0); } /** * @param name name of this pet */ public Pet(String name) { + + //default age of 0 + //specified name + this(name, 0); } @@ -22,6 +37,10 @@ public Pet(String name) { * @param age age of this pet */ public Pet(int age) { + + //default name of "" + //specified age + this("", age); } /** @@ -29,20 +48,26 @@ public Pet(int age) { * @param age age of this pet */ public Pet(String name, int age) { + + //set name and age + this.name = name; + this.age = age; } /** * @return name of this pet */ public String getName() { - return null; + + return name; } /** * @return age of this pet */ public Integer getAge() { - return null; + + return age; } /** @@ -50,12 +75,20 @@ public Integer getAge() { * ensure this instance of `Pet` is added to the owner's composite `pets` list */ public void setOwner(PetOwner newPetOwner) { + + this.owner = newPetOwner; + + //add this pet to the owner's pets list + if (newPetOwner != null) { + newPetOwner.addPet(this); + } } /** * @return PetOwner object whose composite `pets` collection contains this Pet instance */ public PetOwner getOwner() { - return null; + + return owner; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java b/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java index 326ada5..af7c802 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java @@ -4,17 +4,34 @@ * Created by leon on 2/16/18. */ public class PetOwner { + + private String name; + private List pets; + /** * @param name name of the owner of the Pet * @param pets array of Pet object */ public PetOwner(String name, Pet... pets) { + + this.name = name; + this.pets = new ArrayList<>(); + if (pets != null) { + for (Pet pet : pets) { + addPet(pet); + } + } } /** * @param pet pet to be added to the composite collection of Pets */ public void addPet(Pet pet) { + + if (pet != null && !this.pets.contains(pet)) { + this.pets.add(pet); + pet.setOwner(this); + } } /** @@ -22,6 +39,10 @@ public void addPet(Pet pet) { */ public void removePet(Pet pet) { + if (pet != null && this.pets.remove(pet)) { + pet.setOwner(null); + } + } /** @@ -29,14 +50,25 @@ public void removePet(Pet pet) { * @return true if I own this pet */ public Boolean isOwnerOf(Pet pet) { - return null; + + return this.pets.contains(pet); } /** * @return the age of the Pet object whose age field is the lowest amongst all Pets in this class */ public Integer getYoungetPetAge() { - return null; + + if (pets.isEmpty()) { + return 0; + } + Integer youngestAge = Integer.MAX_VALUE; + for (Pet pet : pets) { + if (pet.getAge() < youngestAge) { + youngestAge = pet.getAge(); + } + } + return youngestAge; } @@ -46,7 +78,17 @@ public Integer getYoungetPetAge() { * @return the age of the Pet object whose age field is the highest amongst all Pets in this class */ public Integer getOldestPetAge() { - return null; + + if (pets.isEmpty()) { + return 0; + } + Integer oldestAge = Integer.MIN_VALUE; + for (Pet pet : pets) { + if (pet.getAge() > oldestAge) { + oldestAge = pet.getAge(); + } + } + return oldestAge; } @@ -54,27 +96,35 @@ public Integer getOldestPetAge() { * @return the sum of ages of Pet objects stored in this class divided by the number of Pet object */ public Float getAveragePetAge() { - return null; + + if (pets.isEmpty()) { + return 0.0; + } + double totalAge = 0; + for (Pet pet : pets) { + totalAge += pet.getAge(); + } + return totalAge / pets.size(); } /** * @return the number of Pet objects stored in this class */ public Integer getNumberOfPets() { - return null; + return pets.size(); } /** * @return the name property of the Pet */ public String getName() { - return null; + return name; } /** * @return array representation of animals owned by this PetOwner */ public Pet[] getPets() { - return null; + return pets.toArray(new Pet[0]); } } From df0232c776924131d2c0c6e0754263e45a93d079 Mon Sep 17 00:00:00 2001 From: jenn-182 Date: Mon, 21 Jul 2025 12:30:37 -0400 Subject: [PATCH 2/2] Comments --- .../zipcodewilmington/assessment1/Animal.java | 3 +- .../zipcodewilmington/assessment1/Cat.java | 7 +++++ .../zipcodewilmington/assessment1/Dog.java | 7 ++++- .../zipcodewilmington/assessment1/Pet.java | 6 +++- .../assessment1/PetOwner.java | 29 +++++++++++++++++-- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/Animal.java b/src/main/java/com/zipcodewilmington/assessment1/Animal.java index 8a2fd89..8c41a5b 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Animal.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Animal.java @@ -4,6 +4,7 @@ * Created by leon on 2/16/18. */ public interface Animal { - + + // Returns the name of this animal String speak(); } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Cat.java b/src/main/java/com/zipcodewilmington/assessment1/Cat.java index 7a1dcca..40a9c1c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Cat.java @@ -10,6 +10,7 @@ public class Cat extends Pet { */ public Cat(String name, Integer age) { + // Call the superclass constructor super(name, age); } @@ -19,6 +20,7 @@ public Cat(String name, Integer age) { */ public Cat(Integer age) { + // Call the superclass constructor super(age); } @@ -27,6 +29,7 @@ public Cat(Integer age) { */ public Cat(String name) { + // Call the superclass constructor super(name); } @@ -38,6 +41,8 @@ public Cat(String name) { * age is 0 */ public Cat() { + + // Call the superclass constructor super(); } @@ -45,6 +50,8 @@ public Cat() { * @return meow as a string */ public String speak() { + + // Return meow return "meow"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Dog.java b/src/main/java/com/zipcodewilmington/assessment1/Dog.java index 4b642cd..5a610b1 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Dog.java @@ -9,7 +9,8 @@ public class Dog extends Pet { * @param age age of this dog */ public Dog(String name, Integer age) { - + + // Call the superclass constructor super(name, age); } @@ -19,6 +20,7 @@ public Dog(String name, Integer age) { */ public Dog(Integer age) { + // Call the superclass constructor super(age); } @@ -27,6 +29,7 @@ public Dog(Integer age) { */ public Dog(String name) { + // Call the superclass constructor super(name); } @@ -38,6 +41,7 @@ public Dog(String name) { */ public Dog() { + // Call the superclass constructor super(); } @@ -46,6 +50,7 @@ public Dog() { */ public String speak() { + // Return bark return "bark"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/Pet.java index cb73e8c..081ffbb 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Pet.java @@ -59,6 +59,7 @@ public Pet(String name, int age) { */ public String getName() { + //return the name of this pet return name; } @@ -67,6 +68,7 @@ public String getName() { */ public Integer getAge() { + //return the age of this pet return age; } @@ -76,6 +78,7 @@ public Integer getAge() { */ public void setOwner(PetOwner newPetOwner) { + //set the owner of this pet this.owner = newPetOwner; //add this pet to the owner's pets list @@ -88,7 +91,8 @@ public void setOwner(PetOwner newPetOwner) { * @return PetOwner object whose composite `pets` collection contains this Pet instance */ public PetOwner getOwner() { - + + //return the owner of this pet return owner; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java b/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java index af7c802..2ad3753 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java @@ -14,9 +14,12 @@ public class PetOwner { */ public PetOwner(String name, Pet... pets) { + // Initialize the name and pets list this.name = name; + // Initialize the pets list this.pets = new ArrayList<>(); if (pets != null) { + // Add each pet to the list of owned pets for (Pet pet : pets) { addPet(pet); } @@ -28,8 +31,11 @@ public PetOwner(String name, Pet... pets) { */ public void addPet(Pet pet) { + // Check if the pet is not null and is not already in the list of owned pets if (pet != null && !this.pets.contains(pet)) { + // Add the pet to the list of owned pets this.pets.add(pet); + // Set the owner of the pet to this PetOwner pet.setOwner(this); } } @@ -38,8 +44,10 @@ public void addPet(Pet pet) { * @param pet pet to be removed from the composite collection Pets */ public void removePet(Pet pet) { - + + // Check if the pet is not null and is in the list of owned pets if (pet != null && this.pets.remove(pet)) { + // If the pet was removed, set its owner to null pet.setOwner(null); } @@ -51,6 +59,7 @@ public void removePet(Pet pet) { */ public Boolean isOwnerOf(Pet pet) { + // Check if the pet is in the list of owned pets return this.pets.contains(pet); } @@ -59,15 +68,18 @@ public Boolean isOwnerOf(Pet pet) { */ public Integer getYoungetPetAge() { + // If there are no pets, return 0 if (pets.isEmpty()) { return 0; } + // Find the youngest pet's age Integer youngestAge = Integer.MAX_VALUE; for (Pet pet : pets) { if (pet.getAge() < youngestAge) { youngestAge = pet.getAge(); } } + // Return the youngest age found return youngestAge; } @@ -79,15 +91,20 @@ public Integer getYoungetPetAge() { */ public Integer getOldestPetAge() { - if (pets.isEmpty()) { + // If there are no pets, return 0 + if (pets.isEmpty()) { return 0; } + // Find the oldest pet's age Integer oldestAge = Integer.MIN_VALUE; + + // Iterate through the pets list to find the oldest age for (Pet pet : pets) { if (pet.getAge() > oldestAge) { oldestAge = pet.getAge(); } } + // Return the oldest age found return oldestAge; } @@ -97,9 +114,11 @@ public Integer getOldestPetAge() { */ public Float getAveragePetAge() { + // If there are no pets, return 0 if (pets.isEmpty()) { return 0.0; } + // Calculate the total age of all pets and divide by the number of pets double totalAge = 0; for (Pet pet : pets) { totalAge += pet.getAge(); @@ -111,6 +130,8 @@ public Float getAveragePetAge() { * @return the number of Pet objects stored in this class */ public Integer getNumberOfPets() { + + // Return the size of the pets list return pets.size(); } @@ -118,6 +139,8 @@ public Integer getNumberOfPets() { * @return the name property of the Pet */ public String getName() { + + // Return the name of the PetOwner return name; } @@ -125,6 +148,8 @@ public String getName() { * @return array representation of animals owned by this PetOwner */ public Pet[] getPets() { + + // Convert the list of pets to an array and return it return pets.toArray(new Pet[0]); } }