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..8c41a5b 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Animal.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Animal.java @@ -4,5 +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 1cf2894..40a9c1c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Cat.java @@ -10,12 +10,18 @@ public class Cat extends Pet { */ public Cat(String name, Integer age) { + // Call the superclass constructor + super(name, age); + } /** * @param age age of this Cat */ public Cat(Integer age) { + + // Call the superclass constructor + super(age); } /** @@ -23,6 +29,9 @@ public Cat(Integer age) { */ public Cat(String name) { + // Call the superclass constructor + super(name); + } /** @@ -32,12 +41,17 @@ public Cat(String name) { * age is 0 */ public Cat() { + + // Call the superclass constructor + super(); } /** * @return meow as a string */ public String speak() { - return null; + + // 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 bca1c07..5a610b1 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/Dog.java @@ -10,19 +10,27 @@ public class Dog extends Pet { */ public Dog(String name, Integer age) { + // Call the superclass constructor + super(name, age); + } /** * @param age age of this dog */ public Dog(Integer age) { + + // Call the superclass constructor + super(age); } /** * @param name name of this dog */ public Dog(String name) { - + + // Call the superclass constructor + super(name); } /** @@ -32,12 +40,17 @@ public Dog(String name) { * age is 0 */ public Dog() { + + // Call the superclass constructor + super(); } /** * @return bark as a string */ public String speak() { - return null; + + // 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 afc3e99..081ffbb 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,28 @@ 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 the name of this pet + return name; } /** * @return age of this pet */ public Integer getAge() { - return null; + + //return the age of this pet + return age; } /** @@ -50,12 +77,22 @@ public Integer getAge() { * ensure this instance of `Pet` is added to the owner's composite `pets` list */ public void setOwner(PetOwner newPetOwner) { + + //set the owner of this pet + 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 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 326ada5..2ad3753 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/PetOwner.java @@ -4,23 +4,52 @@ * 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) { + + // 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); + } + } } /** * @param pet pet to be added to the composite collection of 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); + } } /** * @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); + } } @@ -29,14 +58,29 @@ public void removePet(Pet pet) { * @return true if I own this pet */ public Boolean isOwnerOf(Pet pet) { - return null; + + // Check if the pet is in the list of owned pets + 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 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; } @@ -46,7 +90,22 @@ 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 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; } @@ -54,27 +113,43 @@ 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 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(); + } + return totalAge / pets.size(); } /** * @return the number of Pet objects stored in this class */ public Integer getNumberOfPets() { - return null; + + // Return the size of the pets list + return pets.size(); } /** * @return the name property of the Pet */ public String getName() { - return null; + + // Return the name of the PetOwner + return name; } /** * @return array representation of animals owned by this PetOwner */ public Pet[] getPets() { - return null; + + // Convert the list of pets to an array and return it + return pets.toArray(new Pet[0]); } }