0% found this document useful (0 votes)
35 views5 pages

19 08. Basic Interfaces Polymorphism Lab 202110910

The document describes creating class hierarchies for shapes, animals, and math operations. For shapes, it involves an abstract Shape class with CalculatePerimeter, CalculateArea, and Draw methods, extended by Rectangle and Circle classes. For animals, it involves an Animal class with name and favorite food fields and an ExplainSelf method, extended by Cat and Dog classes. For math operations, it involves a MathOperations class with three Add methods for different data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

19 08. Basic Interfaces Polymorphism Lab 202110910

The document describes creating class hierarchies for shapes, animals, and math operations. For shapes, it involves an abstract Shape class with CalculatePerimeter, CalculateArea, and Draw methods, extended by Rectangle and Circle classes. For animals, it involves an Animal class with name and favorite food fields and an ExplainSelf method, extended by Cat and Dog classes. For math operations, it involves a MathOperations class with three Add methods for different data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab: Basic Interfaces and Encapsulation

Shapes
NOTE: You need a public StartUp class with the namespace Shapes.
Build hierarchy of interfaces and classes:

You should be able to use the class like this:


StartUp.cs

var radius = int.Parse(Console.ReadLine());


IDrawable circle = new Circle(radius);

var width = int.Parse(Console.ReadLine());


var height = int.Parse(Console.ReadLine());
IDrawable rect = new Rectangle(width, height);

circle.Draw();
rect.Draw();

Examples
Input Output
3 *******
4 ** **
5 ** **
* *
** **
** **
*******
****
* *
* *
* *
****

PROJECT WEB - WEBG301 1


Solution
The algorithm for drawing a circle is:

The algorithm for drawing a rectangle is:

MathOperation
NOTE: You need a public StartUp class with the namespace Operations.

PROJECT WEB - WEBG301 2


Create a class MathOperations, which should have 3 times method Add(). Method Add() has
to be invoked with:
 Add(int, int): int
 Add(double, double, double): double
 Add(decimal, decimal, decimal): decimal
You should be able to use the class like this:
StartUp.cs

public static void Main()


{
MathOperations mo = new MathOperations();
Console.WriteLine(mo.Add(2, 3));
Console.WriteLine(mo.Add(2.2, 3.3, 5.5));
Console.WriteLine(mo.Add(2.2m, 3.3m, 4.4m));
}

Examples
Output
5
11
9.9

Solution
Created MathOperation class should look like this:

Animals
NOTE: You need a public StartUp class with the namespace Animals.
Create a class Animal, which holds two fields:

PROJECT WEB - WEBG301 3


 name: string
 favouriteFood: string
Animal has one virtual method ExplainSelf(): string.
You should add two new classes - Cat and Dog. Override the ExplainSelf() method by
adding concrete animal sound on a new line. (Look at examples below)
You should be able to use the class like this:
StartUp.cs

Animal cat = new Cat("Pesho", "Whiskas");


Animal dog = new Dog("Gosho", "Meat");

Console.WriteLine(cat.ExplainSelf());
Console.WriteLine(dog.ExplainSelf());

Examples
Output
I am Pesho and my fovourite food is
Whiskas
MEEOW
I am Gosho and my fovourite food is Meat
DJAAF
Solution

PROJECT WEB - WEBG301 4


Shapes
NOTE: You need a public StartUp class with the namespace Shapes.
Create a class hierarchy, starting with abstract class Shape:

 Abstract methods:
o CalculatePerimeter(): doulbe
o CalculateArea(): double
 Virtual methods:
o Draw(): string
Extend the Shape class with two children:

 Rectangle
 Circle
Each of them need to have:
 Fields:
o height and width for Rectangle
o radius for Circle
 Encapsulation for these fields
 A public constructor
 Concrete methods for calculations (perimeter and area)
 Override methods for drawing

PROJECT WEB - WEBG301 5

You might also like