Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added JennStaci.md
Empty file.
2 changes: 2 additions & 0 deletions src/main/java/com/zipcodewilmington/assessment1/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
* Created by leon on 2/16/18.
*/
public interface Animal {

// Returns the name of this animal
String speak();
}
16 changes: 15 additions & 1 deletion src/main/java/com/zipcodewilmington/assessment1/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ 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);
}

/**
* @param name name of this Cat
*/
public Cat(String name) {

// Call the superclass constructor
super(name);

}

/**
Expand All @@ -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";
}
}
17 changes: 15 additions & 2 deletions src/main/java/com/zipcodewilmington/assessment1/Dog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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";
}
}
43 changes: 40 additions & 3 deletions src/main/java/com/zipcodewilmington/assessment1/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,91 @@ 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);
}


/**
* @param age age of this pet
*/
public Pet(int age) {

//default name of ""
//specified age
this("", age);
}

/**
* @param name name of this pet
* @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;
}

/**
* @param newPetOwner the new owner of this pet
* 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;
}
}
89 changes: 82 additions & 7 deletions src/main/java/com/zipcodewilmington/assessment1/PetOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,52 @@
* Created by leon on 2/16/18.
*/
public class PetOwner {

private String name;
private List<Pet> 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);
}

}

Expand All @@ -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;
}


Expand All @@ -46,35 +90,66 @@ 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;
}


/**
* @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]);
}
}