Question

In: Computer Science

C# The Zookeepers need a system to keep track of all their animals. They need to...

C#

The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species.

  • There are three cages and the user must input information about the animal in each one. After accepting input for all three cages, the program should output the contents of each cage in a way that exposes all the information about that animal.
  • The program should accept the following species: Lion, Bear, Wolf.
  • Define classes for the Lion, Bear, and Wolf species that all implement the Animal interface. All Animals should have an int field for age and a String field for species. Each species of animal should have its own unique field defined: String maneColour for Lions, int speed for Wolves, and bool isGrizzly for Bears. They should also define two methods:
    • RequestUniqueCharacteristic() which outputs a string asking for a value to store in the specific animal’s unique characteristic and stores it in the appropriate field (maneColour, speed, or isGrizzly)
    • GetDescription() which outputs a short sentence that includes all the animal’s info.
  • Store all the Animals in a list of animals (List<Animal>) and iterate through the list to output all the animals after input is received.

Sample Session

Cage 1
What is the animal’s species? Lion
How old is it? 6
What colour is its mane? Brown

Cage 2
What is the animal’s species? Wolf
How old is it? 9
How fast can it run (in km/h)? 20

Cage 3
What is the animal’s species? Bear
How old is it? 12
Is it a grizzly bear (true/false)? No
=====
Cage 1 contains a 6-year-old lion with a brown mane.
Cage 2 contains a 9-year-old wolf that runs 20 km/h.
Cage 3 contains a 12-year-old non-grizzly bear.

Solutions

Expert Solution

The program is given below. The comments are provided for the better understanding of the logic.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        //Declare the variables for holding temporary values..
        string species;
        string age;

        //Declare the animal interface
        Animal animal = null;

        //Declare the animals list.
        List<Animal> animalsList = new List<Animal>(3);

        //Start a loop 3 times to accept inputs of 3 cages.
        for(int i = 1; i <=3; i++)
        {
            //Get the inputs for species and age.
            string cageLabel = "Cage " + i;
            Console.WriteLine(cageLabel);
            Console.Write("What is the animal’s species? ");
            species = Console.ReadLine();
            Console.Write("How old is it? ");
            age = Console.ReadLine();

            //Depending on what species is entered, the below switch statement will instantiate a corrresponding class (Lion, Bear or Wolf)
            //It also calls the RequestUniqueCharacteristic method which will get the input of the unique characteristic
            //In case of wrong species is entered, the control will enter the default block and display an error message.
            switch (species.ToLower().Trim())
            {
                case "lion":
                    animal = new Lion(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                case "wolf":
                    animal = new Wolf(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                case "bear":
                    animal = new Bear(cageLabel, age, species);
                    animal.RequestUniqueCharacteristic();
                    animalsList.Add(animal);
                    break;
                default:
                    Console.WriteLine("Invalid Species");
                    break;
            }
        }

        //After all the inputs are received, display all the 3 cage and animal information.
        Console.WriteLine("=====");
        foreach (Animal a in animalsList)
        {
            a.GetDescription(); 
        }

        Console.ReadLine();
    }
}

//Create the animal interface as below.
//It has 2 abstract methods.
interface Animal
{
    void RequestUniqueCharacteristic();
    void GetDescription();
}

//The Lion Class is created as below.
class Lion : Animal
{
    private string cage;
    private int age;
    private string species;
    private string maneColour;

    //Default Constructor
    public Lion(string cage, string age, string species)
    {
        this.cage = cage;
        //The age is received as string from the main program.  Convert to int and store it in age.
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("What colour is its mane? ");
        maneColour = Console.ReadLine().ToLower();
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        Console.WriteLine(cage +  " contains a " + age + "-year-old " + species + " with a " + maneColour + " mane.");
    }
}

//The Bear Class is created as below.
class Bear : Animal
{
    private string cage;
    private int age;
    private string species;
    private bool isGrizzly;

    //Default Constructor
    public Bear(string cage, string age, string species)
    {
        this.cage = cage;
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("Is it a grizzly bear (true/false)? ");
        string characteristic = Console.ReadLine().ToLower().Trim();
        //The user can enter yes/no or true/false
        //Check it accordingly and store true or false in the field.
        if (characteristic == "yes" || characteristic == "true")
            isGrizzly = true;
        else
            isGrizzly = false;
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        //Depending on whether the bear is grizzly or not, display the information accordingly.
        if (isGrizzly)
            Console.WriteLine(cage + " contains a " + age + "-year-old grizzly " + species + ".");
        else
            Console.WriteLine(cage + " contains a " + age + "-year-old non-grizzly " + species + ".");
    }
}

//The Wolf Class is created as below.
class Wolf : Animal
{
    private string cage;
    private int age;
    private string species;
    private int speed;

    //Default Constructor
    public Wolf(string cage, string age, string species)
    {
        this.cage = cage;
        this.age = Convert.ToInt32(age);
        this.species = species.ToLower();
    }
    public void RequestUniqueCharacteristic()
    {
        //Get the unique characteristic of the animal.
        Console.Write("How fast can it run (in km/h)? ");
        //Convert it into int before storing
        speed = Convert.ToInt32(Console.ReadLine());
    }
    public void GetDescription()
    {
        //Display the short description about the animal.
        Console.WriteLine(cage + " contains a " + age + "-year-old " + species + " that runs " + speed + " km/h.");
    }
}

The screenshots of the code and output are provided below.


Related Solutions

Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
C++ Modify this to use a separate Boolean member to keep track of whether the queue...
C++ Modify this to use a separate Boolean member to keep track of whether the queue is empty rather than require that one array position remain empty. #include <stdio.h> #include <stdlib.h> #include <limits.h> // A structure to represent a queue struct Queue { int front, rear, size; unsigned capacity; int* array; }; // function to create a queue of given capacity. // It initializes size of queue as 0 struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*)...
How can websites keep track of users? Do they always need to use cookies?
How can websites keep track of users? Do they always need to use cookies?
A retailer, Continental Palms Retail (CPR), plans to create a database system to keep track of...
A retailer, Continental Palms Retail (CPR), plans to create a database system to keep track of the information about its inventory. CPR has several warehouses across the country. Each warehouse is uniquely named. CPR also wants to record the location, city, state, zip, and space (in cubic meters) of each warehouse. There are several warehouses in any single city. CPR stores its products in the warehouses. A product may be stored in multiple warehouses. A warehouse may store multiple products....
Given the following mission statement: . A group of system administrators must keep track of which...
Given the following mission statement: . A group of system administrators must keep track of which computers are assigned to computer users in the community they support. Currently this is done by hand.. but this is tedious, error prone, and inconvenient. System administrators want to automate this task to ease their workload. Product Vision. The Computer Assignment System (CAS) will keep track of computers, computer users, and assignments of computers to users; answer queries; and produce reports about users, computers,...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
Task 2 introduction to software engineering A nursery wants to keep track of all its products,...
Task 2 introduction to software engineering A nursery wants to keep track of all its products, including plants, fountains, garden hardware (wheelbarrow, shovels etc) and also soil and sand which they sell. They buy all stock from the wholesalers. The management wants to know which staff members have been selling what, and from which wholesaler the products were purchased. There are also times when a customer returns a product for a refund, and such information should be available in the...
Tony Gaddis C++ Monkey Business A local zoo wants to keep track of how many pounds...
Tony Gaddis C++ Monkey Business A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day during a typical week. Write a program that stores this information in a two-dimensional 3 × 7 array, where each row represents a different monkey and each column represents a different day of the week. The monkeys are represented by integers 1, 2, and 3; the weekdays are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s)...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list. 5. Clear...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT