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

bjective: Write a program where a user populates a collection of various cats an

ID: 3738522 • Letter: B

Question

bjective:

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.

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!

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

Explanation / Answer

ANS:-

Given that,

  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.

PROGRAM:-

/**
* The java program that prompts user to select a choice
* and then enter values for selected animal and then details
* of the selection are printed to console.
* */
//AnimalFrontEnd.java
import java.util.Scanner;
public class AnimalFrontEnd
{
   private static Scanner scanner=new Scanner(System.in);
   public static void main(String[] args)
   {
       //Create an object of Animalcollection class of size=10
       AnimalCollection collection=new AnimalCollection(10);
       int choice;
       int animaltype;
       while(true)
       {
           //calling menu method
           choice=menu();
           switch(choice)
           {
           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");
               animaltype=Integer.parseInt(scanner.nextLine());
               switch (animaltype) {
               case 1:
                   HouseCat hcat=getHouseCat();
                   collection.addAnimal(hcat);
                   break;
               case 2:
                   Leopard leopard=getLeopard();
                   collection.addAnimal(leopard);
                   break;
               case 3:
                   DomesticDog ddog=getDomesticDog();
                   collection.addAnimal(ddog);
                   break;
               case 4:
                   Wolf wolf=getWolf();
                   collection.addAnimal(wolf);
                   break;
               default:
                   System.out.println("Invald choice");
                   break;
               }
               collection.printAnimals();
               break;
           case 2:
               System.out.println("Enter name to remove");
               String key=scanner.nextLine();
               collection.removeAnimal(key);
               collection.printAnimals();
               break;
           case 3:
               System.out.println("Thank you");
               //close the appliation
               System.exit(0);
           }
       }
   }
  
  
   //Methog to get wolf object data
   private static Wolf getWolf() {
      
       String name;
       double weight;
       int energyLevel;
       String packLeaderName;
      
       System.out.println("Ente the wolf' name,weight, energy level and leader name");
       name=scanner.nextLine();
       weight=Double.parseDouble(scanner.nextLine());
       energyLevel=Integer.parseInt(scanner.nextLine());
       packLeaderName=scanner.nextLine();
      
       return new Wolf(name, weight, energyLevel, packLeaderName);
   }
   //Methog to get Domestic object data
   private static DomesticDog getDomesticDog() {
       String name;
       double weight;
       int energyLevel;
       String type;
      
       System.out.println("Ente the Domestic dog' name,weight, energy level and type");
       name=scanner.nextLine();
       weight=Double.parseDouble(scanner.nextLine());
       energyLevel=Integer.parseInt(scanner.nextLine());
       type=scanner.nextLine();
      
       return new DomesticDog(name, weight, energyLevel, type);
   }
   //Methog to get Leopard object data
   private static Leopard getLeopard() {
       String name;
       double weight;      
       String mood;
       int numberOfPots;
      
       System.out.println("Ente the house cat's name,weight, "
               + "mood, and type");
       name=scanner.nextLine();
       weight=Double.parseDouble(scanner.nextLine());      
       mood=scanner.nextLine();
       numberOfPots=Integer.parseInt(scanner.nextLine());
      
       return new Leopard(name, weight, mood,numberOfPots);
   }
   //Methog to get House cat object data
   private static HouseCat getHouseCat() {
       String name;
       double weight;      
       String mood;
       String type;
      
       System.out.println("Ente the house cat's name,weight, "
               + "mood, and type");
       name=scanner.nextLine();
       weight=Double.parseDouble(scanner.nextLine());      
       mood=scanner.nextLine();
       type=scanner.nextLine();
      
       return new HouseCat(name, weight, mood,type);
   }
   //Methog to get user choice
   public static int menu()
   {
       System.out.println
       ("Welcome 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");
       int choice=Integer.parseInt(scanner.nextLine());
       return choice;
   }
  
}

-------------------------------------------------------------------------------------------


//Animal.java
//Abstract animal class
public abstract class Animal
{
   private String name;
   private double weight;
   //constructor to set name and weight
   public Animal(String name,double weight) {
       this.name=name;
       this.weight=weight;
   }
   //Return name
   public String getName()
   {
       return name;
   }
   //override the toString method
   public String toString() {      
       return "Name: "+name+" Weight:"+weight;
   }
}

-------------------------------------------------------------------------------------------

//Cat.java
//Extends the Animal class and overrides the toStirng method
public class Cat extends Animal
{
   private String mood;
   public Cat(String name,double weight,
           String mood) {
       super(name, weight);
       this.mood=mood;
   }
  
   public String getMood()
   {
       return mood;
   }
   public String toString() {  
       return super.toString()+" Mood: "+mood;
   }
}

-------------------------------------------------------------------------------------------

//Dog.java
//Extends the Animal class and overrides the toStirng method
public class Dog extends Animal
{
   private int energyLevel;
   public Dog(String name,double weight,
           int energyLevel) {
       super(name, weight);
       this.energyLevel=energyLevel;
   }
  
   public int getEnergyLevel()
   {
       return energyLevel;
   }
  
   public String toString() {      
       return super.toString()+" Energy Level"+energyLevel;
   }   
}

-------------------------------------------------------------------------------------------

//HouseCat.java
//Extends the Animal class and overrides the toStirng method
public class HouseCat extends Cat
{
   private String type;
   public HouseCat(String name,double weight,
           String mood,String type)
   {
       super(name, weight, mood);
       this.type=type;
   }
  
  
   public String getType()
   {
       return type;
   }
  
   public String toString() {      
       return super.toString()+" Type :"+type;
   }
}

-------------------------------------------------------------------------------------------

//Leopard.java
//Extends the Animal class and overrides the toStirng method
public class Leopard extends Cat
{
   private int numberOfPots;
   public Leopard(String name,double weight,
           String mood,int numberOfPots)
   {
       super(name, weight, mood);
       this.numberOfPots=numberOfPots;
   }
      
   public String toString() {      
       return super.toString()+
       "Number of pots:"+numberOfPots;
   }
}

-------------------------------------------------------------------------------------------

//DomesticDog.java
//Extends the Animal class and overrides the toStirng method
public class DomesticDog extends Dog
{
   private String type;
   public DomesticDog(String name,double weight,
           int energyLevel,
           String type) {
       super(name, weight, energyLevel);
       this.type=type;
   }
  
  
   public String toString() {      
       return super.toString()+
       " Type :"+type;
   }
}

-------------------------------------------------------------------------------------------

//Wolf.java
//Extends the Animal class and overrides the toStirng method
public class Wolf extends Dog
{
   private String packLeaderName;
   public Wolf(String name,double weight,
           int energyLevel,
           String packLeaderName) {
       super(packLeaderName, weight, energyLevel);
       this.packLeaderName=
               packLeaderName;
   }
  
   public String toString() {      
       return super.toString()+
       "Leader Name :"+packLeaderName;
   }
}

-------------------------------------------------------------------------------------------

//AnimalCollection.java
//Extends the Animal class and overrides the toStirng method
public class AnimalCollection
{
   private Animal animals[];
   private int count;
  
  
   //Constructor to create an array of given size
   public AnimalCollection(int size)
   {
       animals=new Animal[size];
       count=0;
   }
   //Method to add Animal to the animals array
   public void addAnimal(Animal animal)
   {
       if(count<animals.length)
       {
           animals[count]=animal;
           count++;
       }
   }
  
   //Method to remove an animal from animals array
   public void removeAnimal(String name)
   {
       boolean found=false;
       int pos=-1;
       for (int i = 0; i < count
               && !found; i++)
       {
           if(animals[i].getName().equals(name))
           {
               found=true;
               pos=i;
           }
       }
      
       if(found)
       {
           for (int i = pos; i < count; i++)
           {
               animals[i]=animals[i+1];
           }
           count--;
       }      
   }
  
   //Method to print animals
   public void printAnimals()
   {
       for (int i = 0; i < count; i++)
       {
           System.out.println(animals[i].toString());
       }
   }
  
}

-------------------------------------------------------------------------------------------

Sample 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
Ente the house cat's name,weight, mood, and type
Roscoe
20
Hungry
Bombay
Name: Roscoe Weight:20.0 Mood: Hungry Type :Bombay
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
3
Thank you