Question: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InheritanceActivity { public class Animal { public string Name { get; set; } public

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InheritanceActivity
{
public class Animal
{
public string Name { get; set; }
public int Legs { get; set; }
public Animal()
{
Name = "Animal";
Legs =0;
}
public virtual string GetName()
{
return Name;
}
public virtual string GetLegs()
{
return Legs.ToString();
}
public virtual string GetSpecialAbility()
{
return "
Eating...";
}
public string Display()
{
return GetName()+""+GetLegs()+GetSpecialAbility();
}
public override string ToString()
{
return Display()+"
";
}
}
public class Dog : Animal
{
//Complete the constructor for Dog and GetSpecialAbility() function
public Dog()
{
//ToDo
//Set the dog name "Dog" and Legs to be 4
}
public override string GetSpecialAbility()
{
//ToDo
//replace return null and return dogs special ability "Woof";
return null;
}
}
public class Fish : Animal
{
//TODO
//Complete the constructor for Fish and GetSpecialAbility() function
}
public class Shark : Fish
{
//TODO
//Complete the constructor for Shark and GetSpecialAbility() function
//Shark Takes the last animal as parameter to the constructor method.
//Shark ate the last animal as the SpecialAbility.
public Shark(Animal animal)
{
}
}
class Program
{
static void Main(string[] args)
{
/* The main method is complete.
* You do not need to modify the main method code.
*/
Animal lastAnimal = null;
Animal animal = null;
string input;
while ((input = Console.ReadLine())!= "exit")
{
if (input == "animal")
{
animal = new Animal();
lastAnimal = animal;
}
else if (input == "dog")
{
animal = new Dog();
lastAnimal = animal;
}
else if (input == "fish")
{
animal = new Fish();
lastAnimal = animal;
}
else if (input == "shark")
{
animal = new Shark(lastAnimal);
lastAnimal = animal;
}
else if(input == "exit")
{
return;
}
Console.WriteLine(animal);
}
}
}
}
// Sample Outputs
/*Command Words
animal
dog
fish
shark
exit
Sample Input/Output 1:
animal
Animal 0
Eating...
dog
Dog 4
Woof
fish
Fish 0
Just keep swimming
shark
Shark 0
Shark ate Fish
exit
---------------------------
*/
/*Command words
animal
shark
fish
shark
dog
shark
shark
exit
Sample Input/Output 2:
animal
Animal 0
Eating...
shark
Shark 0
Shark ate Animal
fish
Fish 0
Just keep swimming
shark
Shark 0
Shark ate Fish
dog
Dog 4
Woof
shark
Shark 0
Shark ate Dog
shark
Shark 0
Shark ate Shark
exit
*/ Create a simple set of classes representing types of animals.
1. Create a base class representing all animals. The base class should contain a field for the animals name. The name should be set to Animal by default but should also be set to a more specific value by any derived classes. The base class should also have a field for storing the number of legs the animal has. By default, in the Animal class, legs are set to 0. The animal class should have three virtual functions namely GetName(), GetLegs(), and GetSpecialAbility(), that return the Animals Name, the total number of legs, and the special ability of the Animal. Moreover, this base class contains an overridden ToString() method, to return all of its state variables in a given format. [Please investigate the starter code, or the provided input/ output to understand the format.]
2. Create a Dog class that is a type of Animal, and the class also contains a method called GetSpecialAbility() that returns Woof.
3. Create a Fish class that is a type of Animal, and the class also contains a method called GetSpecialAbility() that returns Just keep swimming.
4. Create a Shark class that is a type of Fish, and also contains a method called GetSpecialAbility() that uses a reference animal object that was initially passed as a parameter to a constructor method and returns Shark ate

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!