CSharp-OOP-Inheritance-Exercise
CSharp-OOP-Inheritance-Exercise
Problem 1. Person
You are asked to model an application for storing data about people. You should be able to have a person and a
child. The child derives from the person. Your task is to model the application. The only constraints are:
- People should not be able to have a negative age
- Children should not be able to have an age more than 15.
Person – represents the base class by which all of the others are implemented
Child - represents a class, which derives from Person.
Note
Your class’s names MUST be the same as the names shown above!!!
Sample Main()
static void Main()
{
string name = Console.ReadLine();
int age = int.Parse(Console.ReadLine());
// 2. Add Constructor
// 3. Add Properties
// 4. Add Methods
}
Define a field for each property the class should have (e.g. Name, Age)
Define the Name and Age properties of a Person.
Page 1 of 7
Sample Code
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
Sample Code
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(String.Format("Name: {0}, Age: {1}",
this.Name,
this.Age));
return stringBuilder.ToString();
}
And voila! If everything is correct, we can now create Person objects and display information about them.
Sample Code
public Child(string name, int age)
: base(name, age)
{
}
There is no need to rewrite the Name and Age properties since Child inherits Person and by default has them.
Examples
Input Output
Problem 2. Zoo
NOTE: You need a public class StartUp.
Create a project Zoo. It needs to contain the following classes:
Page 2 of 7
Follow the diagram and create all of the classes. Each of them, except the Animal class, should inherit from
another class. Every class should have:
A constructor, which accepts one parameter: name.
Property Name - string.
Zip your solution without the bin and obj folders and upload it in Judge.
Page 3 of 7
Create a class Hero. It should contain the following members:
A constructor, which accepts:
o username – string
o level – int
The following properties:
o Username - string
o Level – int
ToString() method
Hint: Override ToString() of the base class in the following way:
Page 4 of 7
SportCar – DefaultFuelConsumption = 10
RaceMotorcycle – DefaultFuelConsumption = 8
Car – DefaultFuelConsumption = 3
Zip your solution without the bin and obj folders and upload it in Judge.
Problem 5. Restaurant
NOTE: You need a public class StartUp. Create a Restaurant project with the following classes and hierarchy:
There are Food and Beverages in the restaurant and they are all products.
The Product class must have the following members:
A constructor with the following parameters: string name, decimal price, double milliliters
o Reuse the constructor of the inherited class
Name – string
Price – double
Milliliters – double
HotBeverage and ColdBeverage are beverages and they accept the following parameters upon initialization:
string name, decimal price, double milliliters. Reuse the constructor of the inherited class.
Coffee and Tea are hot beverages. The Coffee class must have the following additional members:
double CoffeeMilliliters = 50
decimal CoffeePrice = 3.50
Caffeine – double
A constructor with the following parameters: string name, decimal price, double grams
Name – string
Price – decimal
Grams – double
MainDish, Dessert and Starter are food. They all accept the following parameters upon initialization: string
name, decimal price, double grams. Reuse the base class constructor.
Dessert must accept one more parameter in its constructor: double calories, and has a property:
Calories
Page 5 of 7
Grams = 250
Calories = 1000
CakePrice = 5
Problem 6. Animals
NOTE: You need a public class StartUp.
Create a hierarchy of Animals. Your program should have three different animals – Dog, Frog and Cat. Deeper in the
hierarchy you should have two additional classes – Kitten and Tomcat. Kittens are female and Tomcats are male.
All types of animals should be able to produce some kind of sound - ProduceSound(). For example, the dog should
be able to bark. Your task is to model the hierarchy and test its functionality. Create an animal of each kind and
make them all produce sound.
You will be given some lines of input. Each two lines will represent an animal. On the first line will be the type of
animal and on the second – the name, the age and the gender. When the command "Beast!" is given, stop the
input and print all the animals in the format shown below.
Output
Print the information for each animal on three lines. On the first line, print: "{AnimalType}"
On the second line print: "{Name} {Age} {Gender}"
On the third line print the sounds it produces: "{ProduceSound()}"
Constraints
Each Animal should have a name, an age and a gender
All input values should not be blank (e.g. name, age and so on…)
If you receive an input for the gender of a Tomcat or a Kitten, ignore it but create the animal
If the input is invalid for one of the properties, throw an exception with message: "Invalid input!"
Each animal should have the functionality to ProduceSound()
Here is the type of sound each animal should produce:
o Dog: "Woof!"
o Cat: "Meow meow"
o Frog: "Ribbit"
o Kittens: "Meow"
o Tomcat: "MEOW"
Examples
Input Output
Cat Cat
Tom 12 Male Tom 12 Male
Dog Meow meow
Sharo 132 Male Dog
Beast! Sharo 132 Male
Woof!
Page 6 of 7
Frog Frog
Kermit 12 Male Kermit 12 Male
Beast! Ribbit
Page 7 of 7