C# - Inheritance exercise
I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong.












Short Summary:
Source Code:
Animal.cs:
using System;
namespace Task02
{
class Animal
{
private String name;
private String diet;
private String location;
private double weight;
private int age;
private String colour;
public Animal(String name,String diet, String location,double weight,int age, String colour)
{
this.name = name;
this.diet = diet;
this.location = location;
this.weight = weight;
this.age = age;
this.colour = colour;
}
public virtual void eat()
{
Console.WriteLine("An animal eats");
}
public virtual void sleep()
{
Console.WriteLine("An animal sleeps");
}
public virtual void makeNoise()
{
Console.WriteLine("An animal makes an noise");
}
}
}
Wolf.cs:
using System;
namespace Task02
{
class Wolf : Animal
{
public Wolf(String name, String diet, String location, double weight,
int age, String colour) : base(name, diet, location, weight, age, colour)
{
}
public override void makeNoise()
{
Console.WriteLine("HOWLSSSSSSS");
}
public override void eat()
{
Console.WriteLine("I can eat 10lbs of meat");
}
}
}
Feline.cs:
using System;
namespace Task02
{
class Feline : Animal
{
private String species;
public Feline(String name, String diet, String location, double weight, int age, String colour,
String species): base(name, diet, location, weight, age, colour)
{
this.species = species;
}
public override void sleep()
{
Console.WriteLine("Feline Sleeps for 10 hrs");
}
}
}
Lion.cs:
using System;
namespace Task02
{
class Lion : Feline
{
public Lion(String name, String diet, String location, double weight, int age, String colour,
String species) : base(name, diet, location, weight, age, colour,species)
{
}
public override void makeNoise()
{
Console.WriteLine("Lion RoarsssssS");
}
public override void eat()
{
Console.WriteLine("I can eat 25lb of meat");
}
}
}
Tiger.cs:
using System;
namespace Task02
{
class Tiger : Feline
{
private String colourStripes;
public Tiger(String name, String diet, String location, double weight, int age, String colour,
String species, String colourStripes) : base(name, diet, location, weight, age, colour,species)
{
this.colourStripes = colourStripes;
}
public override void makeNoise()
{
Console.WriteLine("ROARRRRRRRRRRRRRRRRRR");
}
public override void eat()
{
Console.WriteLine("I can eat 20lbs of meat");
}
}
}
Birds.cs:
using System;
namespace Task02
{
class Birds : Animal
{
private String species;
private double wingSpan;
public Birds(String name, String diet, String location, double weight, int age, String colour,
String species, double wingSpan) : base(name, diet, location, weight, age, colour)
{
this.species = species;
this.wingSpan = wingSpan;
}
public virtual void fly()
{
}
}
}
Eagle.cs:
using System;
namespace Task02
{
class Eagle : Birds
{
public Eagle(String name, String diet, String location, double weight, int age, String colour,
String species, double wingSpan) : base(name, diet, location, weight, age, colour,species,wingSpan)
{
}
public void layEgg()
{
}
public override void fly()
{
Console.WriteLine("Eagle Flys");
}
public override void makeNoise()
{
Console.WriteLine("WHISTLESSS");
}
public override void eat()
{
Console.WriteLine("I can eat 1lb of fish");
}
}
}
Penguin.cs:
using System;
namespace Task02
{
class Penguin : Birds
{
public Penguin(String name, String diet, String location, double weight, int age, String colour,
String species, double wingSpan) : base(name, diet, location, weight, age, colour, species, wingSpan)
{
}
public override void fly()
{
Console.WriteLine("Penguin cannot Fly");
}
public override void makeNoise()
{
Console.WriteLine("Pennnnnn");
}
public override void eat()
{
Console.WriteLine("I can eat 0.5lb of reptiles");
}
}
}
ZooPark.cs:
using System;
namespace Task02
{
class ZooPark
{
static void Main(string[] args)
{
//Animal williamWolf = new Animal("William the Wolf", "Meat", "Dog Village", 50.6, 9, "Grey");
//Animal tonyTiger = new Animal("Tony the Tiger", "Meat", "Cat Land", 110, 6, "Orange and White");
//Animal edgerEagle = new Animal("Edger the Eagle", "Fish", "Bird Mania", 20, 15, "Black");
Animal baseAnimal = new Animal("Animal Name", "Animal Diet", "Animal Location", 0.0, 0, "Animal Colour");
Tiger tonyTiger = new Tiger("Tony the Tiger", "Meat", "Cat Land", 110, 6, "Orange and White", "Siberian", "White");
Wolf williamWolf = new Wolf("William the Wolf", "Meat", "Dog Village", 50.6, 9, "Grey");
Eagle edgerEagle = new Eagle("Edger the Eagle", "Fish", "Bird Mania", 20, 15, "Black", "Speci", 5.5);
Penguin penguin = new Penguin("Heppy the Penguin", "Reptiles", "Aquaruim", 10, 5, "Black and White", "Pen species", 3.5);
baseAnimal.makeNoise();
tonyTiger.makeNoise();
williamWolf.makeNoise();
edgerEagle.makeNoise();
baseAnimal.eat();
tonyTiger.eat();
williamWolf.eat();
edgerEagle.eat();
baseAnimal.sleep();
tonyTiger.sleep();
williamWolf.sleep();
edgerEagle.sleep();
edgerEagle.fly();
penguin.fly();
Console.ReadLine();
}
}
}
SAMPLE RUN:

******************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
******************************************************************************
C# - Inheritance exercise I could not firgure out why my code was not working. I...
What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...
The following code has a problem with polymorphism. I keep getting a runtime error. Error: "An unhandled exception of type System.InvalidCastException occured in polymorphism.exe" Apparently, I need one line of code to fix it. C# Code: class Sensor { private string sensorName; public Sensor(string _name) { sensorName = _name; } public virtual void ActionType() { Console.WriteLine("Sensor Detect Nothing."); } } class SmokeSensor : Sensor { private string type;...
previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() { //create Animal object Animal animal("Dog","Mammal",4); cout<<"Animal object details"<<endl; cout<<"Name: "<<animal.getName()<<endl; cout<<"Type: "<<animal.getType()<<endl; cout<<"# of Legs: "<<animal.getLegs()<<endl; cout<<endl<<endl; //create Cat object Cat cat("byssinian cat","carnivorous mammal",4,"short...
So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...
This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...
1. What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } A. 90 B. 110 C. 210 D. This is an infinite loop 2. If a superclass has constructor(s): A. then its subclass must initialize the superclass fields (attributes). B. then its subclass must call one of the constructors that the superclass does have. C. then its subclass does not inherit...
I cannot figure out why my removeAll for var = 7 is not
working.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnorderedArrayListNamespace;
namespace Array
{
public class Program
{
public static void Main(string[] args)
{
UnorderedArrayList u = new UnorderedArrayList();
u.print();
int var = 5;
u.insert(ref var);
var = 12;
u.insert(ref var);
var = 2;
u.insert(ref var);
var = 29;
u.insert(ref var);
var = 7;
u.insert(ref var);
var = 33;
u.insert(ref var);
var = 49;
u.insert(ref var);
var...
I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...
I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student { private String firstName; private String lastName; private String id; private boolean tuitionPaid; public Student(String firstName, String lastName, String id, boolean tuitionPaid) { this.firstName=firstName; this.lastName=lastName; this.id=id; this.tuitionPaid=tuitionPaid; } ...
******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...