SlideShare a Scribd company logo
1 of 4
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Author: Kasun Ranga Wijeweera
Email: krw19870829@gmail.com
Date: 2020 April 11
1) Design a system to handle the shapes: circles, ellipses, rectangles and squares. Use the
Factory Method design pattern according to the instructions given below. Read all the steps
carefully before you begin answering the questions.
a) The interface should have two method prototypes for computing the area and
perimeter of the shapes.
interface IShape
{
double GetArea ();
double GetPerimeter ();
}
b) Create classes for each of the shapes. You should use inheritance appropriately.
class Circle : IShape
{
protected double a;
public Circle (double a)
{
this.a = a;
}
public virtual double GetArea ()
{
return Math.PI * Math.Pow(a, 2);
}
public virtual double GetPerimeter ()
{
return 2 * Math.PI * a;
}
}
2 of 4
class Ellipse : Circle
{
double b;
public Ellipse (double a, double b) : base(a)
{
this.b = b;
}
public override double GetArea ()
{
return Math.PI * a * b;
}
public override double GetPerimeter ()
{
return 2 * Math.PI * Math.Sqrt ((Math.Pow (a, 2) + Math.Pow (b, 2)) / 2);
}
}
class Square : IShape
{
protected double a;
public Square (double a)
{
this.a = a;
}
public virtual double GetArea ()
{
return Math.Pow (a, 2);
}
public virtual double GetPerimeter ()
{
return 4 * a;
}
}
3 of 4
class Rectangle : Square
{
double b;
public Rectangle(double a, double b) : base(a)
{
this.b = b;
}
public override double GetArea ()
{
return a * b;
}
public override double GetPerimeter ()
{
return 2 * a + 2 * b;
}
}
c) The object should be created based on the user inputs as shown in table below.
User input Object type
c Circle object
r Rectangle object
s Square object
e Ellipse object
class Creator
{
public IShape FactoryMethod (char ch, double a, double b)
{
if (ch == 'c')
{
return new Circle (a);
}
else if (ch == 's')
{
return new Square (a);
}
else if (ch == 'e')
{
return new Ellipse (a, b);
4 of 4
}
else if (ch == 'r')
{
return new Rectangle (a, b);
}
}
}
d) Create a circle object with the radius = 7.
Creator c = new Creator ();
IShape shape;
shape = c.FactoryMethod (‘c’, 7, 0);
e) Compute the area and perimeter of the circle object created in (1) (d) above.
Console.WriteLine ("Circle area: {0}", shape.GetArea ());
Console.WriteLine ("Cirlce perimeter: {0}", shape.GetPerimeter ());
Note:
1. Circle of radius = r.
a. Area = πr2
b. Perimeter = 2πr
2. Ellipse of radii a and b.
a. Area = πab
b. Perimeter = 2π√{(a2
+b2
)/2}

More Related Content

PPTX
Array in c#
Prem Kumar Badri
 
PDF
The Power of Composition (NDC Oslo 2020)
Scott Wlaschin
 
PPTX
React hooks
Assaf Gannon
 
PDF
React new features and intro to Hooks
Soluto
 
PPT
HTML Introduction
c525600
 
PPTX
What is component in reactjs
manojbkalla
 
PPTX
Conditions In C# C-Sharp
Abid Kohistani
 
DOCX
pega training with project level Trainingwhatsup@8142976573
Santhoo Vardan
 
Array in c#
Prem Kumar Badri
 
The Power of Composition (NDC Oslo 2020)
Scott Wlaschin
 
React hooks
Assaf Gannon
 
React new features and intro to Hooks
Soluto
 
HTML Introduction
c525600
 
What is component in reactjs
manojbkalla
 
Conditions In C# C-Sharp
Abid Kohistani
 
pega training with project level Trainingwhatsup@8142976573
Santhoo Vardan
 

What's hot (20)

PDF
Ad hoc Polymorphism using Type Classes and Cats
Philip Schwarz
 
PDF
Asp.net mvc basic introduction
Bhagath Gopinath
 
PDF
Pipeline oriented programming
Scott Wlaschin
 
PDF
Workshop React.js
Commit University
 
PPTX
Composite design pattern
MUHAMMAD FARHAN ASLAM
 
PPT
Javascript built in String Functions
Avanitrambadiya
 
PPTX
Mongo db 최범균
beom kyun choi
 
PDF
The Functional Programmer's Toolkit (NDC London 2019)
Scott Wlaschin
 
PPT
C# Basics
Sunil OS
 
PPT
MySQL Cursors
Reggie Niccolo Santos
 
PDF
Rails MVC Architecture
Masato TADA
 
PDF
Functor, Apply, Applicative And Monad
Oliver Daff
 
PPTX
java Servlet technology
Tanmoy Barman
 
PDF
Html / CSS Presentation
Shawn Calvert
 
PDF
Case Study Research in Software Engineering
alessio_ferrari
 
PPTX
Optional in Java 8
Richard Walker
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PDF
Angular Schematics
Christoffer Noring
 
PDF
How to make APEX print through Node.js
Dimitri Gielis
 
PDF
React for Beginners
Derek Willian Stavis
 
Ad hoc Polymorphism using Type Classes and Cats
Philip Schwarz
 
Asp.net mvc basic introduction
Bhagath Gopinath
 
Pipeline oriented programming
Scott Wlaschin
 
Workshop React.js
Commit University
 
Composite design pattern
MUHAMMAD FARHAN ASLAM
 
Javascript built in String Functions
Avanitrambadiya
 
Mongo db 최범균
beom kyun choi
 
The Functional Programmer's Toolkit (NDC London 2019)
Scott Wlaschin
 
C# Basics
Sunil OS
 
MySQL Cursors
Reggie Niccolo Santos
 
Rails MVC Architecture
Masato TADA
 
Functor, Apply, Applicative And Monad
Oliver Daff
 
java Servlet technology
Tanmoy Barman
 
Html / CSS Presentation
Shawn Calvert
 
Case Study Research in Software Engineering
alessio_ferrari
 
Optional in Java 8
Richard Walker
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Angular Schematics
Christoffer Noring
 
How to make APEX print through Node.js
Dimitri Gielis
 
React for Beginners
Derek Willian Stavis
 
Ad

Similar to Assignment for Factory Method Design Pattern in C# [ANSWERS] (20)

PDF
An Exercise for Factory Method Design Pattern in C#
Kasun Ranga Wijeweera
 
PDF
Contoh Factory pattern
Fajar Baskoro
 
PPT
3433 Ch09 Ppt
martha leon
 
PPT
Csphtp1 09
HUST
 
PPT
3433 Ch10 Ppt
martha leon
 
PPT
Csphtp1 10
HUST
 
ODP
Expression problem
Attila Magyar
 
PDF
The Expression Problem - Part 1
Philip Schwarz
 
PPT
Oops concept in c#
ANURAG SINGH
 
DOC
C# example (Polymorphesim)
MaRwa Samih AL-Amri
 
PDF
Software Engineering for Indies #gcmuc18
Andreas Pohl
 
PDF
JRuby: Apples and Oranges
Engine Yard
 
PDF
Dot Net Design Patterns Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Software Development - Thinking Object's Way
Sanjay Gadge
 
PDF
Lab_910.pdf
linaalama
 
PDF
Factory method pattern (Virtual Constructor)
Sameer Rathoud
 
PDF
Class program and uml in c++
Osama Al-Mohaia
 
DOCX
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
faithxdunce63732
 
PDF
JRuby: Enhancing Java Developers Lives
Engine Yard
 
PDF
JRuby: Enhancing Java Developers' Lives
Hiro Asari
 
An Exercise for Factory Method Design Pattern in C#
Kasun Ranga Wijeweera
 
Contoh Factory pattern
Fajar Baskoro
 
3433 Ch09 Ppt
martha leon
 
Csphtp1 09
HUST
 
3433 Ch10 Ppt
martha leon
 
Csphtp1 10
HUST
 
Expression problem
Attila Magyar
 
The Expression Problem - Part 1
Philip Schwarz
 
Oops concept in c#
ANURAG SINGH
 
C# example (Polymorphesim)
MaRwa Samih AL-Amri
 
Software Engineering for Indies #gcmuc18
Andreas Pohl
 
JRuby: Apples and Oranges
Engine Yard
 
Dot Net Design Patterns Interview Questions PDF By ScholarHat
Scholarhat
 
Software Development - Thinking Object's Way
Sanjay Gadge
 
Lab_910.pdf
linaalama
 
Factory method pattern (Virtual Constructor)
Sameer Rathoud
 
Class program and uml in c++
Osama Al-Mohaia
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
faithxdunce63732
 
JRuby: Enhancing Java Developers Lives
Engine Yard
 
JRuby: Enhancing Java Developers' Lives
Hiro Asari
 
Ad

More from Kasun Ranga Wijeweera (20)

PDF
Decorator Design Pattern in C#
Kasun Ranga Wijeweera
 
PDF
Singleton Design Pattern in C#
Kasun Ranga Wijeweera
 
PDF
Introduction to Design Patterns
Kasun Ranga Wijeweera
 
PPTX
Algorithms for Convex Partitioning of a Polygon
Kasun Ranga Wijeweera
 
PDF
Geometric Transformations II
Kasun Ranga Wijeweera
 
PDF
Geometric Transformations I
Kasun Ranga Wijeweera
 
PDF
Introduction to Polygons
Kasun Ranga Wijeweera
 
PDF
Bresenham Line Drawing Algorithm
Kasun Ranga Wijeweera
 
PDF
Digital Differential Analyzer Line Drawing Algorithm
Kasun Ranga Wijeweera
 
PDF
Loops in Visual Basic: Exercises
Kasun Ranga Wijeweera
 
PDF
Conditional Logic: Exercises
Kasun Ranga Wijeweera
 
PDF
Getting Started with Visual Basic Programming
Kasun Ranga Wijeweera
 
PDF
CheckBoxes and RadioButtons
Kasun Ranga Wijeweera
 
PDF
Variables in Visual Basic Programming
Kasun Ranga Wijeweera
 
PDF
Loops in Visual Basic Programming
Kasun Ranga Wijeweera
 
PDF
Conditional Logic in Visual Basic Programming
Kasun Ranga Wijeweera
 
PDF
Assignment for Variables
Kasun Ranga Wijeweera
 
PDF
Assignment for Events
Kasun Ranga Wijeweera
 
PDF
Mastering Arrays Assignment
Kasun Ranga Wijeweera
 
PDF
Arrays in Visual Basic Programming
Kasun Ranga Wijeweera
 
Decorator Design Pattern in C#
Kasun Ranga Wijeweera
 
Singleton Design Pattern in C#
Kasun Ranga Wijeweera
 
Introduction to Design Patterns
Kasun Ranga Wijeweera
 
Algorithms for Convex Partitioning of a Polygon
Kasun Ranga Wijeweera
 
Geometric Transformations II
Kasun Ranga Wijeweera
 
Geometric Transformations I
Kasun Ranga Wijeweera
 
Introduction to Polygons
Kasun Ranga Wijeweera
 
Bresenham Line Drawing Algorithm
Kasun Ranga Wijeweera
 
Digital Differential Analyzer Line Drawing Algorithm
Kasun Ranga Wijeweera
 
Loops in Visual Basic: Exercises
Kasun Ranga Wijeweera
 
Conditional Logic: Exercises
Kasun Ranga Wijeweera
 
Getting Started with Visual Basic Programming
Kasun Ranga Wijeweera
 
CheckBoxes and RadioButtons
Kasun Ranga Wijeweera
 
Variables in Visual Basic Programming
Kasun Ranga Wijeweera
 
Loops in Visual Basic Programming
Kasun Ranga Wijeweera
 
Conditional Logic in Visual Basic Programming
Kasun Ranga Wijeweera
 
Assignment for Variables
Kasun Ranga Wijeweera
 
Assignment for Events
Kasun Ranga Wijeweera
 
Mastering Arrays Assignment
Kasun Ranga Wijeweera
 
Arrays in Visual Basic Programming
Kasun Ranga Wijeweera
 

Recently uploaded (20)

PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
oapresentation.pptx
mehatdhavalrajubhai
 
Presentation about variables and constant.pptx
kr2589474
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 

Assignment for Factory Method Design Pattern in C# [ANSWERS]

  • 1. 1 of 4 Assignment for Factory Method Design Pattern in C# [ANSWERS] Author: Kasun Ranga Wijeweera Email: [email protected] Date: 2020 April 11 1) Design a system to handle the shapes: circles, ellipses, rectangles and squares. Use the Factory Method design pattern according to the instructions given below. Read all the steps carefully before you begin answering the questions. a) The interface should have two method prototypes for computing the area and perimeter of the shapes. interface IShape { double GetArea (); double GetPerimeter (); } b) Create classes for each of the shapes. You should use inheritance appropriately. class Circle : IShape { protected double a; public Circle (double a) { this.a = a; } public virtual double GetArea () { return Math.PI * Math.Pow(a, 2); } public virtual double GetPerimeter () { return 2 * Math.PI * a; } }
  • 2. 2 of 4 class Ellipse : Circle { double b; public Ellipse (double a, double b) : base(a) { this.b = b; } public override double GetArea () { return Math.PI * a * b; } public override double GetPerimeter () { return 2 * Math.PI * Math.Sqrt ((Math.Pow (a, 2) + Math.Pow (b, 2)) / 2); } } class Square : IShape { protected double a; public Square (double a) { this.a = a; } public virtual double GetArea () { return Math.Pow (a, 2); } public virtual double GetPerimeter () { return 4 * a; } }
  • 3. 3 of 4 class Rectangle : Square { double b; public Rectangle(double a, double b) : base(a) { this.b = b; } public override double GetArea () { return a * b; } public override double GetPerimeter () { return 2 * a + 2 * b; } } c) The object should be created based on the user inputs as shown in table below. User input Object type c Circle object r Rectangle object s Square object e Ellipse object class Creator { public IShape FactoryMethod (char ch, double a, double b) { if (ch == 'c') { return new Circle (a); } else if (ch == 's') { return new Square (a); } else if (ch == 'e') { return new Ellipse (a, b);
  • 4. 4 of 4 } else if (ch == 'r') { return new Rectangle (a, b); } } } d) Create a circle object with the radius = 7. Creator c = new Creator (); IShape shape; shape = c.FactoryMethod (‘c’, 7, 0); e) Compute the area and perimeter of the circle object created in (1) (d) above. Console.WriteLine ("Circle area: {0}", shape.GetArea ()); Console.WriteLine ("Cirlce perimeter: {0}", shape.GetPerimeter ()); Note: 1. Circle of radius = r. a. Area = πr2 b. Perimeter = 2πr 2. Ellipse of radii a and b. a. Area = πab b. Perimeter = 2π√{(a2 +b2 )/2}