Question

**IN JAVAA**

Write a program where a user populates a collection of various cats and dogs. The user should be able to specify which type they wish to enter, and then are prompted with the pertinent information for every type of pet.   

Requirements:

The structure of the program should follow this UML class diagram.

AnimalCollection AnimalFrontEnd animals: Animall + addAnimal(Animal): void + removeAnimal(String): void + printAnimals():void + static main(Stringll args):void Animal -name: String -weight: double Cat Dog -mood: String -energyLevel: int HouseCat Leopard numberOfSpots: int DomesticDog Wolf -type: String -type: String -packLeaderName: String

You may also include helper methods and attributes that are not noted in the class.

Additional Notes:

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

Example Output:

Welcome to the Cat and Dog Collection!

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

Enter a selection

1

Would you like to add a 1. House Cat, 2. A Leopard, 3. A Domestic Dog, or 4. A wolf

1

Enter the house cat's name, weight, mood, and type

Roscoe

20

Hungry

Bombay

Current Collection

Name: Roscoe Weight: 20.0 Mood: Hungry Type: Bombay

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

Enter a selection

1

Would you like to add a 1. House Cat, 2. A Leopard, 3. A Domestic Dog, or 4. A wolf

3

Enter the domestic dog's name, weight, energy level, and type

Maggie

50

75

Retriever

Current Collection

Name: Roscoe Weight: 20.0 Mood: Hungry Type: Bombay

Name: Maggie Weight: 50.0 Energy Level:75 Type: Retriever

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

Enter a selection

1

Would you like to add a 1. House Cat, 2. A Leopard, 3. A Domestic Dog, or 4. A wolf

2

Enter the leopard's name, weight, mood, and number of spots

Chester

200

Playful

100

Current Collection

Name: Roscoe Weight: 20.0 Mood: Hungry Type: Bombay

Name: Maggie Weight: 50.0 Energy Level:75 Type: Retriever

Name: Chester Weight: 200.0 Mood: Playful Number of Spots: 100

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

Enter a selection

1

Would you like to add a 1. House Cat, 2. A Leopard, 3. A Domestic Dog, or 4. A wolf

4

Enter the wolf's name, weight, energy level, and type

Ghost

200

90

Jon Snow

Current Collection

Name: Roscoe Weight: 20.0 Mood: Hungry Type: Bombay

Name: Maggie Weight: 50.0 Energy Level:75 Type: Retriever

Name: Chester Weight: 200.0 Mood: Playful Number of Spots: 100

Name: Ghost Weight: 200.0 Energy Level:90 Pack Leader Name: Jon Snow

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

Enter a selection

2

Enter the animal's name that is to be removed

Chester

Current Collection

Name: Roscoe Weight: 20.0 Mood: Hungry Type: Bombay

Name: Maggie Weight: 50.0 Energy Level:75 Type: Retriever

Name: Ghost Weight: 200.0 Energy Level:90 Pack Leader Name: Jon Snow

Would you like to

1. Add a cat or dog

2. Remove a cat or dog

3. Quit

3

Goodbye!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

- Code for all the classes that are mentioned in the UML are given sequentially below.
- Here Animal is the superclass for all the classes.
- Cat is the super class of the classes HouseCat and Leopard class.
- Dog is the superclass of the classes DomesticDog and Wolf class.
- There are 2 more classes called AnimalCollection, which stores/removes created Animal classes and AnimalFrontEnd that deals with the UI for the user.
- Comments are added in the required sections of the code.

Animal Class

public class Animal {
  
   String name;
   double weight;
  
   Animal(String name,double weight){ // Parameterized Constructor
       this.name=name;
       if(weight > 0){
           this.weight=weight;
       }
       else{
           System.out.println("Weight should be > 0");
           return;
       }
          
   }
  
   //Following are the getter and setters for the 2 properties of animal class
  
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public double getWeight() {
       return weight;
   }
   public void setWeight(double weight) {
       this.weight = weight;
   }
  
   public String toString(){ //Displaying the name and weight of the animal
       StringBuilder str=new StringBuilder();
       str.append("Name: ");
       str.append(this.getName()+" ");
       str.append("Weight: ");
       str.append(this.getWeight()+" ");
       return str.toString();
   }
  
   public boolean equals(String name1,double weight2){   //To check between 2 animals
       if(this.getName()==name1&&this.getWeight()==weight2){
           return true;
       }
       else
           return false;
   }
  
  
}

Cat class

public class Cat extends Animal {
  
   //enum mood { sleepy, playful, hungry} ;
   String mood;
   Cat(String name,double weight,String mood){ //Parameterized Constructor
       super(name,weight);
       switch(mood){ //Code for the required definitions for mood variable
       case "Sleepy":
       case "sleepy":
           this.mood=mood;
           break;
       case "playful":
       case "Playful":
           this.mood=mood;
           break;
       case "Hungry":
       case "hungry":
           this.mood=mood;
           break;
       default:
           System.out.println("Mood should be:\"sleepy, playful, hungry\"");
           return;
       }
   }
   // getters and setters for variable mood
   public String getMood() {
       return mood;
   }
   public void setMood(String mood) {
       this.mood = mood;
   }
  
   //Below definition of toString adds Mood variable to the String produced by toString method of Animal class
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Mood: ");
       str.append(this.getMood());
       return str.toString();
   }
}

HouseCat class

public class HouseCat extends Cat {
  
   String type;

   HouseCat(String name,double weight,String mood,String type){ //Parameterized Constructor
       super( name, weight, mood); //Calling super class Constructor with required values
       switch(type){ //Code for the required definitions for type variable
       case "Bombay":
           this.type=type;
           break;
       case "Ragdoll":
           this.type=type;
           break;
       case "Sphinx":
           this.type=type;
           break;
       case "Scottish Fold":
           this.type=type;
           break;
       default:
           System.out.println("Type should be:\"Bombay, Ragdoll,Sphinx, Scottish Fold\"");
           return;
       }
   }
  
   // getters and setters for variable type
   public String getType() {
       return type;
   }
   public void setType(String type) {
       this.type = type;
   }
  
   //Below definition of toString adds type variable to the String produced by toString method of Cat class
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Type: ");
       str.append(this.getType());
       return str.toString();
   }

}

Leopard class

public class Leopard extends Cat{
  
   int numberOfSpots;
  
   Leopard(String name,double weight,String mood,int nos){ //Parameterized Constructor
       super( name, weight, mood);
       this.numberOfSpots=nos;
   }

   //Getters and Setters for the variable numberOfSpots
   public int getNumberOfSpots() {
       return numberOfSpots;
   }

   public void setNumberOfSpots(int numberOfSpots) {
       this.numberOfSpots = numberOfSpots;
   }
  
   //Below definition of toString adds numberOfSpots variable to the String produced by toString method of Animal class
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Number of Spots: ");
       str.append(this.getNumberOfSpots());
       return str.toString();
   }

}

Dog class

public class Dog extends Animal{
  
   int energyLevel;
  
   Dog(String name,double weight,int el){
       super(name,weight);
      
       if(el >=0 && el <=100){ //Energy level should be >=0 && <=100
           energyLevel=el;
       }
       else{
           System.out.println("Energy level should be >=0 && <=100");
           return;
       }
   }
  
  
  
   public int getEnergyLevel() {
       return energyLevel;
   }

   public void setEnergyLevel(int energyLevel) {
       this.energyLevel = energyLevel;
   }


   //Below definition of toString adds energyLevel variable to the String produced by toString method of Animal class
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Energy Level: ");
       str.append(this.getEnergyLevel());
       return str.toString();
    }   

}

DomesticDog class

public class DomesticDog extends Dog{
  
   String type;

   DomesticDog(String name,double weight,int el,String type){
       super(name,weight,el);
       switch(type){
       case "Retriever":
           this.type=type;
           break;
       case "Terrier":
           this.type=type;
           break;
       case "Husky":
           this.type=type;
           break;
       case "Yappy":
           this.type=type;
           break;
       case "Mutt":
           this.type=type;
           break;
       default:
           System.out.println("Type should be:\"Retriever, Terrier,Husky,Yappy, Mutt\"");
           return;
       }
   }

   public String getType() {
       return type;
   }

   public void setType(String type) {
       this.type = type;
   }
  
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Type: ");
       str.append(this.getType());
       return str.toString();
   }

}

Wolf class

public class Wolf extends Dog {
  
   String packLeaderName;
  
   Wolf(String name,double weight,int el,String packLeaderName){
       super(name,weight,el);
      
       this.packLeaderName=packLeaderName;
   }

   public String getPackLeaderName() {
       return packLeaderName;
   }

   public void setPackLeaderName(String packLeaderName) {
       this.packLeaderName = packLeaderName;
   }
  
   public String toString(){
       StringBuilder str=new StringBuilder();
       str.append(super.toString()+" ");
       str.append("Pack Leader Name: ");
       str.append(this.getPackLeaderName());
       return str.toString();
   }

}

AnimalCollection class (Updated)

public class AnimalCollection {
  
   Animal[] animals;
   int cnt=0; //For keeping track of no of Animals Object Stored in the Animal[]
   AnimalCollection(){
       animals=new Animal[15]; //Giving default length of array to 15
   }

   public void addAnimal(Animal an){
               animals[cnt++]=an; //Storing the animal obj and incrementing the cnt variable
      
   }
  
   public void removeAnimal(String name){
       //System.out.println("Inputted String: "+name);
       for(int i=0;i<cnt;i++){
           //System.out.println("animals[i].getName()==name: "+animals[i].getName()==name);
           //System.out.println("Cnt: "+cnt);
           if(animals[i].getName()==name){
               //Reassigning the elements in the Array
               for(int j=i;j<cnt-1;j++){
                   animals[j]=animals[j+1];
               }
           }
           cnt--; // Reducing the element count by 1
      
       }
   }
  
   public void printAnimal(){
      
       for(int i=0;i<cnt;i++){
          
           System.out.println(animals[i].toString());
          
       }
   }
  

}

AnimalFrontEnd class

public class AnimalFrontEnd {
  
   public static void main(String[] args){
       Scanner scan=new Scanner(System.in);
       AnimalCollection ac=new AnimalCollection();
       int ch;
       char op='y';
       do{
           System.out.println("\nWelcome to the Cat and Dog Collection!");

           System.out.println("Would you like to");

           System.out.println("1. Add a cat or dog");

           System.out.println("2. Remove a cat or dog");

           System.out.println("3. Quit");

           System.out.println("Enter a selection");
           ch=scan.nextInt();
           switch(ch){ // Outer Loop of options
           case 1:
               System.out.println("Would you like to add a 1. House Cat, 2. A Leopard, 3. A Domestic Dog, or 4. A wolf");
               ch=scan.nextInt();
                   if(ch==1){ //Inner Loop of options
                       System.out.println("Enter the house cat's name, weight, mood, and type");
                       HouseCat hc=new HouseCat(scan.next(),scan.nextDouble(),scan.next(),scan.next());
                       ac.addAnimal(hc);
                       ac.printAnimal();
                       break;
                   }
                  
                  
                   if(ch==2){ //Adding Leopard object
                       System.out.println("Enter the leopard's name, weight, mood, and number of spots");
                       Leopard lp=new Leopard(scan.next(),scan.nextDouble(),scan.next(),scan.nextInt());
                       ac.addAnimal(lp);
                       ac.printAnimal();
                       break;
                   }
                  
                   if(ch==3){ //Adding DomesticDog object
                   System.out.println("Enter the domestic dog's name, weight, energy level, and type");
                   ac.addAnimal(new DomesticDog(scan.next(),scan.nextDouble(),scan.nextInt(),scan.next()));
                   ac.printAnimal();
                   break;
                   }
                  
                   if(ch==4){ //Adding Wolf object
                       System.out.println("Enter the wolf's name, weight, energy level, and type");
                       ac.addAnimal(new Wolf(scan.next(),scan.nextDouble(),scan.nextInt(),scan.next()));
                       ac.printAnimal();
                       break;
                   }
           case 2: //For removal of the Animal by name
               System.out.println("Enter the animal's name that is to be removed");
               ac.removeAnimal(scan.next());
               ac.printAnimal();
               break;
          
           case 3:
               System.out.println("Goodbye!");
               op='n';
               break;
          
           }
       } while(op!='n');

}
}

The ouput generated after running the code.

Problems Javadoc Declaration ConsoleProgress Error Log <terminated> AnimalFrontEnd [Java Application] C:\Program FilesJavajdk

Add a comment
Know the answer?
Add Answer to:
**IN JAVAA** Write a program where a user populates a collection of various cats and dogs....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated...

    10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated sludge operation that adversely affect effluent quality with origins in the engineering, hydraulic and microbiological components of the process. The real "heart" of the activated sludge system is the development and maintenance of a mixed microbial culture (activated sludge) that treats wastewater and which can be managed. One definition of a wastewater treatment plant operator is a "bug farmer", one who controls the aeration...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT