Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The weight for animal should be strictly greater than 0 The mood for a cat shoul

ID: 3605825 • Letter: T

Question

The weight for animal should be strictly greater than 0

The mood for a cat should either be “sleepy”, “playful”, or “hungry”

The energy level for a dog should be between 0 and 100 inclusively

The type of house cat should one of the following

Short Hair

Bombay

Ragdoll

Sphinx

Scottish Fold

The type of domestic dog should be one of the following

Retriever

Terrier

Husky

Yappy

Mutt

In addition to what’s specified in the UML diagram, the classes, Animal, Cat, Dog, HouseCat, Leopard, Domestic Dog, and Wolf must have

Constructors (Both default and parameterized)

Accessors and Mutators

A toString method

An equals method

In addition to what’s specified in the UML diagram, AnimalCollection only needs a default constructor which will set the array of animals to some constant default size. Also no Accessors or Mutators.

Remember the block arrows are the “is a” relationship so it means inheritance

The line arrows are the “has a” relationship so it contains one or more instances of that class

Explanation / Answer

Hello, I have created everything as per the requirements.

//Animal.java

public class Animal {

      private String name;

      private double weight;

      public Animal() {

            /**

            * default constructor

            */

            name="unknown";

            weight=1;

      }

      public Animal(String name, double weight) {

            /**

            * parameterized constructor

            */

            this.name = name;

            /**

            * making sure weight is greater than 0

            */

            if(weight<=0){

                  this.weight=1;

            }else{

                  this.weight = weight;

            }

           

      }

      public String getName() {

            return name;

      }

      public void setName(String name) {

            this.name = name;

      }

      public double getWeight() {

            return weight;

      }

      public void setWeight(double weight) {         

            if(weight<=0){

                  this.weight=1;

            }else{

                  this.weight = weight;

            }

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str="Name: "+name;

            str+=" Weight: "+weight;

            return str;

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof Animal){

                  Animal a=(Animal) obj;

                  if(a.name.equals(this.name) && a.weight==this.weight){

                        return true;

                  }

            }

            return false;

      }

     

}

//Cat.java

public class Cat extends Animal{

      private String mood;

     

     

      public Cat(String name, double weight,Mood mood) {

            super(name, weight);

            this.mood = mood.name();

      }

      public Cat(){

            /**

            * default constructor

            */

            super(); /*invoking the super class constructor*/

      }

      public String getMood() {

            return mood;

      }

      public void setMood(Mood mood) {

            this.mood = mood.name();

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Mood: "+mood;

            return str;

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof Cat){

                  Cat a=(Cat) obj;

                  if( this.mood==a.mood){

                        return super.equals(obj);

                  }

            }

            return false;

      }

}

/**

* An enum object which can hold the different moods of Cat

*

*/

enum Mood{

      sleepy,

      playful,

      hungry

}

//Dog.java

public class Dog extends Animal{

      private int energyLevel;

     

      public Dog(String name, double weight,int energyLevel) {

            super(name, weight);

            /**

            * making sure energylevel is between 0 and 100

            */

            if(energyLevel<0){

                  this.energyLevel=0;

            }else if(energyLevel>100){

                  this.energyLevel=100;

            }else{

                  this.energyLevel = energyLevel;

            }

           

      }

      public Dog() {

            super();

            energyLevel=0;

      }

      public int getEnergyLevel() {

            return energyLevel;

      }

      public void setEnergyLevel(int energyLevel) {

            if(energyLevel<0){

                  this.energyLevel=0;

            }else if(energyLevel>100){

                  this.energyLevel=100;

            }else{

                  this.energyLevel = energyLevel;

            }

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Energy level: "+energyLevel;

            return str;

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof Dog){

                  Dog d=(Dog)obj;

                  if(this.energyLevel==d.energyLevel){

                        return super.equals(obj);

                  }

            }

            return false;

      }

     

}

//DomesticDog.java

public class DomesticDog extends Dog{

      private String type;

      public DomesticDog() {

            super();

            type=DomesticDogType.Retriever.name();

      }

      public DomesticDog(String name, double weight, int energyLevel,DomesticDogType type) {

            super(name, weight, energyLevel);

            this.type=type.name();

           

      }

     

     

      public String getType() {

            return type;

      }

      public void setType(DomesticDogType type) {

            this.type = type.name();

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Type: "+type;

            return str;

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof DomesticDog){

                  DomesticDog d=(DomesticDog)obj;

                  if(this.type.equals(d.type)){

                        return super.equals(obj);

                  }

            }

            return false;

      }

     

}

/**

* An enum object which can hold the different types of Domestic Dogs

*

*/

enum DomesticDogType{

      Retriever,

      Terrier,

      Husky,

      Yappy,

      Mutt

}

//HouseCat.java

public class HouseCat extends Cat {

      private String type;

      public HouseCat(String name, double weight, Mood mood, HouseCatType type) {

            super(name, weight, mood);

            this.type = type.name();

      }

      public HouseCat() {

            super();

      }

      public String getType() {

            return type;

      }

      public void setType(HouseCatType type) {

            this.type = type.name();

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof HouseCat){

                  HouseCat c=(HouseCat) obj;

                  if(this.type.equals(c.type)){

                        return super.equals(obj);

                  }

            }

            return false;

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Type: "+type.replace('_', ' '); /*removing the '_' */

            return str;

      }

}

/**

* An enum object which can hold the different types of House Cats

*

*/

enum HouseCatType{

      Short_hair,

      Bombay,

      Ragdoll,

      Sphinx,

      Scottish_Fold

     

}

//Leopard.java

public class Leopard extends Cat {

      private int numberOfSpots;

      public int getNumberOfSpots() {

            return numberOfSpots;

      }

      public void setNumberOfSpots(int numberOfSpots) {

            this.numberOfSpots = numberOfSpots;

      }

      public Leopard(String name, double weight, Mood mood, int numberOfSpots) {

            super(name, weight, mood);

            this.numberOfSpots = numberOfSpots;

      }

      public Leopard() {

            super();

      }

      /**

      * this method helps to compare two objects

      */

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof Leopard){

                  Leopard c=(Leopard) obj;

                  if(this.numberOfSpots==c.numberOfSpots){

                        return super.equals(obj);

                  }

            }

            return false;

      }

      /**

      * returns a string with all details

      */

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Number of Spots: "+numberOfSpots;

            return str;

      }

}

//Wolf.java

public class Wolf extends Dog {

      private String packLeaderName;

      public Wolf() {

            super();

           

      }

      public Wolf(String name, double weight, int energyLevel,String packLeaderName) {

            super(name, weight, energyLevel);

            this.packLeaderName=packLeaderName;

      }

      public String getPackLeaderName() {

            return packLeaderName;

      }

      public void setPackLeaderName(String packLeaderName) {

            this.packLeaderName = packLeaderName;

      }

      @Override

      public String toString() {

            String str=super.toString();

            str+=" Pack Leader Name: "+packLeaderName;

            return str;

      }

      @Override

      public boolean equals(Object obj) {

            if(obj instanceof Wolf){

                  Wolf d=(Wolf)obj;

                  if(this.packLeaderName.equals(d.packLeaderName)){

                        return super.equals(obj);

                  }

            }

            return false;

      }

     

}

//AnimalCollection.java

public class AnimalCollection {

      private Animal[] animals;

      public AnimalCollection() {

            animals=new Animal[10];

      }

      public void addAnimal(Animal animal){

            boolean overflowflag=true;

            for(int i=0;i<animals.length;i++){

                  if(animals[i]==null){

                        animals[i]=animal;

                        overflowflag=false;

                        System.out.println(animal.getName()+" is added");

                        break;

                  }

            }

            if(overflowflag){

                  System.out.println("Array is full,can't be added");

            }

      }

      public void removeAnimal(String name){

            boolean found=false;

            for(int i=0;i<animals.length;i++){

                  if(animals[i]!=null){

                        if(animals[i].getName().equalsIgnoreCase(name)){

                              animals[i]=null;

                              found=true;

                              System.out.println(name+" is deleted");

                              break;

                        }

                  }

            }

            if(!found){

                  System.out.println("The entered animal doesn't exist");

            }

      }

      public void printAnimals(){

            System.out.println("=========================================");

            System.out.println("Animals List...");

            System.out.println("=========================================");

            for(int i=0;i<animals.length;i++){

                  if(animals[i]!=null){

                        System.out.println(animals[i].toString());

                        System.out.println();

                  }

            }

            System.out.println("=========================================");

      }

}

//AnimalFrontEnd.java

public class AnimalFrontEnd {

      public static void main(String[] args) {

            /**

            * initialize an AnimalCollection object

            */

            AnimalCollection collection=new AnimalCollection();

            /**

            * Define some animals

            */

            Animal dora=new HouseCat("Dora", 2, Mood.playful, HouseCatType.Bombay);

            Animal tom=new HouseCat("Tom", 2, Mood.hungry, HouseCatType.Scottish_Fold);

            DomesticDog bob=new DomesticDog("Bob", 30, 98, DomesticDogType.Retriever);

            Wolf akela=new Wolf("Akela", 10, 100, "Mougli");

            /**

            * add each animals to the collection

            */

            collection.addAnimal(dora);

            collection.addAnimal(tom);

            collection.addAnimal(bob);

            collection.addAnimal(akela);

            /**

            * display the animal details

            */

            collection.printAnimals();

            /**

            * deleting an animal if exists

            */

            collection.removeAnimal("dora");

            /**

            * trying to delete an animal which doesnt exist, will display 'The entered animal doesn't exist'

            */

            collection.removeAnimal("dora");

      }

}

/*Output*/

Dora is added

Tom is added

Bob is added

Akela is added

=========================================

Animals List...

=========================================

Name: Dora

Weight: 2.0

Mood: playful

Type: Bombay

Name: Tom

Weight: 2.0

Mood: hungry

Type: Scottish Fold

Name: Bob

Weight: 30.0

Energy level: 98

Type: Retriever

Name: Akela

Weight: 10.0

Energy level: 100

Pack Leader Name: Mougli

=========================================

dora is deleted

The entered animal doesn't exist