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

This homework will be based on Polymorphism. For this assignment there will be 1

ID: 3726661 • Letter: T

Question

This homework will be based on Polymorphism.

For this assignment there will be 1 main program file, 1 Car class, 1 Animal class, 1 Human class, 1 Dog class and 1 Cat class.

The Animal class is the base class. To make things easy, it will only have one private variable "int Age" and one called "string Name". It has a basic constructor and an overloaded constructor, one accessor for each of the private variables and finally a "display" function which will display the Name and age (If the Animal has not been initialized, meaning the basic constructor was used and it set up the name to be "none" and age to be 0, then it will simply display "EMPTY")

The Human class is derived from the Animal class. this class has one private variable called "bool license". This is to check if the person has a license or not, so its either true or false. There is a basic constructor and an overloaded constructor (Remember to initialize the base class as well!!). one accessor for the private variable and finally a "display" function which will display Name, Age, and License (it displays either "has license" if it is true or "no license" if false).

The Dog class is derived from the Animal class. This class has one private variable called "bool loud". This is to see how loud the dog is and it is either true or false. There is a basic constructor and an overloaded constructor (Remember to initialize the base class as well!). one accessor for the private variable and finally a "display" function which will display Name, Age, Loud (it will display either "bark" if not loud or "barkbarkbark" if loud).

The Cat class is derived from the Animal class. This class has one private variable called "bool needy". This is to see if the cat needs someone to play with. There is a basic constructor and an overloaded constructor (Remember to initialize the base class as well!). one accessor for the private variable and finally a "display" function which will display Name, Age, Needy (it will display either "purr" if it is needy or "meow" if it is not needy).

The car class is not derived from anything. This class has a private array of 5 Animals called "Animal [] seat;" This means that the car has seats, but each seat holds an animal. There is a basic constructor and an overloaded constructor (Remember to initialize the seats array to be of size 5 for both. The basic constructor only needs to initialize the 5 default constructor Animals *EMPTY*, while the overloaded needs to receive an array of 5 animals from the main to initialize the seats with those animals). one accessor for the private variable, a display which shows the contents of the car, so it will display the animals in all 5 seats... it will display "EMPTY" if the seat is empty because an empty seat has the Animal default constructor used which based on the display function for Animal, it displays empty in that case. Display it nicely, such as "Seat 1 has Tim Johnson which is 23 and has license" etc.

In the main, you will read from all the files first. The following are the files and their contents. You are responsible for creating them and coming up with the data in the files (I will use my own files when grading).

"humans.txt" - Has the Name, Age, and License of 10 people (License means it will have "true" or "false").  

"dogs.txt" - has the Name, Age, and Loudness of 10 dogs (Loudness is "true" or "false").

"cats.txt" - has the Name, Age, and Needy of 10 cats (Needy is "true" or "false").

place each one of these humans, dogs, cats into 3 different arrays of size 10. These three arrays are simply to hold all the options for the user in the next section of code.

Create a default car (meaning all 5 seats are "EMPTY" because we have 5 default animals in the 5 seats and a default animal is "EMPTY" because of what is stated above for the Animal class).  

Display to the user the contents of the car (should show all seats empty) *array of 0 is the driver*

Create an array of size 5 of Animals

Give the user Options. 1. to add Human, 2 to add Dog, 3 to add Cat, 4 to remove, 5 to check. *There is a loop so the person can do this as many times until the user enters 5 to check AND it is a valid configuration. Make it look nice with clearing the screen and re-displaying the contents of the car each time a change is made so there is no clutter on the screen.

Selecting 1-4 will lead to the question "Which seat would you like to perform this action on". Change the contents of that seat with the choice the user picked in the Options.

**new** show the list of the type of animal selected before, so you can choose which of the 10 animals you want to select to go on the seat. This option is only available if they selected Human, Dog, or Cat. This option is not available if they select remove or check.

Selecting 5 will lead to the program checking if the current configuration is valid, it will display "Congrats, you are on your way on the road happily" and the program will end. If the configuration is not valid, it will display "Sorry this setup is not good because of the following reasons *lists reasons*, please try again"

Reasons for it to be invalid are 1. driver does not have a license. 2. you have two or more loud dogs. 3. You have a cat which is needy.. but there is no one for it to play with (driver not included and a needy cat does not like a loud dog.. so having a driver, a needy cat and a loud dog would not make the cat happy, so this setup is invalid even if another human is present in the car).

C# only Console application only no utf-8

Explanation / Answer

//Since the program is very big, please post the rest of the program as new question

//Code

using System;
using System.IO;
namespace Ex12
{
    //The Animal class is the base class.
    public class Animal
    {
        //one private variable "int Age" and one called "string Name"
        int Age;
        string Name;
        //It has a basic constructor
        //basic constructor was used and it set up the name to be "none" and age to be 0
        public Animal(){
            Age=0;
            Name="none";
        }
        //overloaded constructor
        public Animal(int age,string name)
        {
            Age=age;
            Name=name;
        }
        //one accessor for each of the private variables
        public int getAge()
        {
            return Age;
        }
        public string getName()
        {
            return Name;
        }
        //"display" function which will display the Name and age
        //If the Animal has not been initialized, meaning the
        //basic constructor was used and it set up the name
        //to be "none" and age to be 0, then it will simply display "EMPTY")
        public void display()
        {
            if(Age==0 && Name.Equals("none"))
                Console.WriteLine("EMPTY");
            else
            {
                Console.WriteLine("NAME: "+Name);
                Console.WriteLine("AGE: "+Age);
            }
                
        }
    }
    /// <summary>
    /// /////////////////////////////////////////////////////////////
    /// </summary>
    //The Human class is derived from the Animal class.
    public class Human :Animal{
    //this class has one private variable called "bool license".
    //This is to check if the person has a license or not,
    //so its either true or false.
    bool license;
    //There is a basic constructor
    public Human()
    {
        license=false;
    }
    //overloaded constructor
    public Human(int age,string name,bool stat):base(age,name)
    {
        license=stat;
    }
    //one accessor for the private variable
    public bool getLicenseStatus()
    {
        return license;
    }
    //"display" function which will display Name, Age, and License
    //(it displays either "has license" if it is true or "no license" if false).
     void display()
    {
         if(getAge()==0 && getName().Equals("none"))
                Console.WriteLine("EMPTY");
            else
            {
                Console.WriteLine("NAME: "+getName());
                Console.WriteLine("AGE: "+getAge());
                if(license==true)
                    Console.WriteLine("LICENSE: has license");
                else
                    Console.WriteLine("LICENSE: no license");
            }
    }
                     
    }
    /// <summary>
    /// /////////////////////////////////////////////////////////
    /// </summary>
    //The Dog class is derived from the Animal class.  
    public class Dog: Animal
    {
    //This class has one private variable called "bool loud".
    //to see how loud the dog is and it is either true or false.
    bool loud;
    //There is a basic constructor
    public Dog()
    {
        loud=false;
    }
    //overloaded constructor
    public Dog(int age,string name,bool stat):base(age,name)
    {
        loud=stat;
    }
    //one accessor for the private variable
    public bool getBarkingStatus()
    {
        return loud;
    }
    //"display" function which will display Name, Age, Loud
    //(it will display either "bark" if not loud or "barkbarkbark" if loud).
     void display()
    {
         if(getAge()==0 && getName().Equals("none"))
                Console.WriteLine("EMPTY");
            else
            {
                Console.WriteLine("NAME: "+getName());
                Console.WriteLine("AGE: "+getAge());
                if(loud==true)
                    Console.WriteLine("Loud Status: barkbarkbark");
                else
                    Console.WriteLine("Loud Status: bark");
            }
    }
    }
    /// <summary>
    /// //////////////////////////////////////////////////////
    /// </summary>
  
    //The Cat class is derived from the Animal class.  
    public class Cat: Animal
    {
    //This class has one private variable called "bool needy".
    //This is to see if the cat needs someone to play with
    //and it is either true or false.
    bool needy;
    //There is a basic constructor
    public Cat()
    {
        needy=false;
    }
    //overloaded constructor
    public Cat(int age,string name,bool stat):base(age,name)
    {
        needy=stat;
    }
    //one accessor for the private variable
    public bool getNeedyStatus()
    {
        return needy;
    }
    //"display" function which will display Name, Age, Needy
    //(it will display either "purr" if it is needy or "meow"
    //if it is not needy).
     void display()
    {
        if(getAge()==0 && getName().Equals("none"))
                Console.WriteLine("EMPTY");
            else
            {
                Console.WriteLine("NAME: "+getName());
                Console.WriteLine("AGE: "+getAge());
                if(needy==true)
                    Console.WriteLine("Needy Status: Purr");
                else
                    Console.WriteLine("Needy Status: meow");
            }
    }
    }
    //////////////////////////////////////////////////////////////
    ///
    //The car class is not derived from anything.  
    class Car
    {
        //This class has a private array of 5 Animals called "Animal [] seat;"
        Animal [] seat;        
        //This means that the car has seats, but each seat holds an animal.
        //There is a basic constructor
        //initialize the seats array to be of size 5
        public Car()
        {
            seat=new Animal[5];
            //The basic constructor only needs to initialize
            //the 5 default constructor Animals *EMPTY*
            for(int i=0;i<5;i++)
            {
                seat[i]=new Animal();
            }
        }
        //overloaded constructor
        public Car(Animal [] animals)
        {
            //initialize the seats array to be of size 5
            seat=new Animal[5];
            //the overloaded needs to receive an array of 5 animals
        //from the main to initialize the seats with those animals).
            for(int i=0;i<5;i++)
            {
                seat[i]=new Animal(animals[i].getAge(),animals[i].getName());
            }
        }
        //one accessor for the private variable,
        public Animal[] getSeatDetails()
        {
            return seat;
        }
        //a display which shows the contents of the car,
        //so it will display the animals in all 5 seats...
        void display()
        {
        //it will display "EMPTY" if the seat is empty
        //because an empty seat has the Animal default constructor
        //used which based on the display function for Animal,
        //it displays empty in that case.  Display it nicely,
        //such as  "Seat 1 has Tim Johnson which is 23 and has license"  etc.
        }
    }
    ///////////////////////////////////////////////////////////////////////
    class Program
    {
        public static void Main(string[] args)
        {
            //Since program itself very big , I created 3 samples only under
            //each file in my format
            int n=3; // you have to use "n=10";
            //You have add upto 10 samples as per assignment
            
            /* "humans.txt" - Has the Name, Age, and License of 10 people  (License means it will have "true" or "false").  
             * "dogs.txt" - has the Name, Age, and Loudness of 10 dogs (Loudness is "true" or "false").
             * "cats.txt" - has the Name, Age, and Needy of 10 cats (Needy is "true" or "false").
             */
            Human[]humans=new Human[n];
            string[] lines = File.ReadAllLines("humans.txt");
            for(int i=0;i<lines.Length;i++)
            {
                string [] details=lines[i].Split();
                humans[i]=new Human(Convert.ToInt32(details[2]),details[0]+" "+details[1],Convert.ToBoolean(details[3]));
            }
            
            Dog[]dogs=new Dog[n];
            string[] ll = File.ReadAllLines(@"dogs.txt");
            for(int i=0;i<ll.Length;i++)
            {
                string [] details=ll[i].Split();
                dogs[i]=new Dog(Convert.ToInt32(details[1]),details[0],Convert.ToBoolean(details[2]));
            }
            
            Cat[]cats=new Cat[n];
            string[] Lines = File.ReadAllLines(@"cats.txt");
            for(int i=0;i<Lines.Length;i++)
            {
                string [] details=Lines[i].Split();
                cats[i]=new Cat(Convert.ToInt32(details[1]),details[0],Convert.ToBoolean(details[2]));
            }

// Post the processing part of the program as new question
            Console.ReadKey(true);
        }
    }
}

/////////////////////////////////////////////////////////////////////////

//Input files should be placed in bindebug

//cats.txt

kitty 2 true
sweety 3 false
white 1 true

//dogs.txt

blacky 4 true
browny 1 false
Tiger 3 true

//humans.txt

Tim Johnson 23 true
Jhon Stephen 45 true
David Mathew 17 false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote