Question

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.

STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class

Animal williamWolf = new Animal(William the Wolf, Meat, Dog Village, 50.6, 9, Grey); Animal tonyTiger = new Animal(T

Animal -name: String -diet: String -location: String -weight: double -age: int -colour: String +eat (): void + sleep(): void

STEP 2: Lets navigate back to the ZooPark class where you will see the three Animal objects created. Comment out the tonyTig

Animal williamwolf = new Animal(William the Wolf, Meat, Dog Village, 50.6, 9, Grey); 1/Animal tonyTiger = new Animal(

STEP 5: Create classes for a Wolf and an Eagle that all inherit from the base Animal class as detailed by the above UML. Alte

public void layEgg) 1/ code to allow eagles to lay eggs public void fly // code to allow eagles to fly If you try to access t

Compile and run the program. Check whether the produced output is as expected. STEP 3: Add an override method for makeNoise()

Now, create an eat() method in the Tiger class and include the override keyword in the method declaration. public override

4. So the Zoo Park is starting to take shape, but could we make it better? At this moment, Tiger inherits from the base class

Add the new Feline class that inherits from the base Animal class by writing the following code: class Feline : Animal privat

5. In this last exercise, test the knowledge you have acquired. Consider the following code snippets. class A class CB public

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Have have all the common properties and methods in the base class
  • Make sure to have virtual methods in base class to override the methods in sub classes.
  • Sub class constructor should call its base constructor to assign values of its base class attributes.
  • Provided all the base and super classes requested for.
  • And main method in ZooPark class tests all the methods.

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:

An animal nakes an noise ROARRRRRRRRRRRRRRRRRR HOWLSSSSSSS WHISTLESSS An animal eats I can eat 201bs of neat I can eat 10lbs

******************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

******************************************************************************

Add a comment
Know the answer?
Add Answer to:
C# - Inheritance exercise I could not firgure out why my code was not working. I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    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...

    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;...

  • So I am working on an assignment where we make a rudimentary browser history with c#....

    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...

    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...

    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;...

    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...

    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"...

    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;    }   ...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Hello, I ran into some issues with my code and wanted to ask if anyone can...

    Hello, I ran into some issues with my code and wanted to ask if anyone can help me fix them. Thank you in advance! Question: You and your team of software developers are experiencing problems saving the state of a program between different times that the program is run. An object oriented program state can be maintained by saving the state of the objects in the program. Also, you can transfer the state of an object between different computer systems...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT