0% found this document useful (0 votes)
18 views

Object Oriented Design and Patterns

The document contains two problems related to design patterns. Problem 1 asks to implement different shapes (circle, triangle, rectangle) using the factory pattern. It provides the interface and classes to calculate the area of each shape. Problem 2 asks to implement cash withdrawal authorization rules using the chain of responsibility pattern. It defines handlers for cashier, senior officer, and manager and chains them together to process withdrawal requests of different amounts.

Uploaded by

Nïloy Ft Aminul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Object Oriented Design and Patterns

The document contains two problems related to design patterns. Problem 1 asks to implement different shapes (circle, triangle, rectangle) using the factory pattern. It provides the interface and classes to calculate the area of each shape. Problem 2 asks to implement cash withdrawal authorization rules using the chain of responsibility pattern. It defines handlers for cashier, senior officer, and manager and chains them together to process withdrawal requests of different amounts.

Uploaded by

Nïloy Ft Aminul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IMPERIAL COLLEGE OF ENGINEERING

Affiliated by Rajshahi University college code: 385

Department of CSE

Assignment on:
CSE-4122 (Object Oriented Design and Design Pattern Lab)

Question solves 2021

SUBMITTED To:
Shams Mahmud
Lecturer,
IMPERIAL COLLEGE OF ENGINEERING

SUBMITTED By:

Md: Aminul Haque


Id:1838520119
B.Sc. Engineering part 4 Odd Semester
IMPERIAL COLLEGE OF ENGINEERING
University of Rajshahi
Department of Computer Science and Engineering B.Sc. (Engg.)
Part-4 Odd Semester Practical Examination-2021
Course: CSE-4122 (Object Oriented Design and Design Pattern Lab)

Problem: 1
Write a program to create various shapes like (Circle, Triangle, Rectangle)and calculate the
area of the shape. You should use Factory Pattern for shape creation so that all shapes can
be created using a single factory object.
Each shape should have their defining properties (For example circle should have radius,
triangle should have length of its three side etc.) and a method named getArea() to calculate
the area of the shape.
When creating a particular shape, you should initialize the shapes property with any default
values (ie. for circle, radius = 1; for rectangle width = 2 and height = 1.5 etc.)

Problem: 2
Suppose we want to withdraw cash from our bank account using check. Bank has the
following business rule for cash withdrawal based on the amount written on check.
d) For Tk. 10,000 only cashier's authorization is sufficient to withdraw money from account.
e) For Tk. 10,000 to 10,00,000 authorization from both cashier and senior officer is needed
f) For Tk 10,00,000 authorization from Senior officer and Manager is mandatory.
Implement this business rule using Chain of Responsibility design pattern.

Special instructions for both problems


All your class/method/variable name should follow proper naming convention.
(ie. Class names should be TitleCased, method and variable name should be camelCased,
and constants (if any) should be UPPERCASED, etc.)
Use meaningful name for all your classes, methods and variables that reflect their purpose.
Your code MUST be indented properly before submit.
Problem 1

// Shape.java interface
public interface Shape {
double getArea();
}

// Circle.java class
class Circle implements Shape
{
private double radius;

public Circle() {
this.radius = 1.0; // Default radius
}

public Circle(double radius)


{
this.radius = radius;
}

@Override
public double getArea()
{
return Math.PI * radius * radius;
}
}

//Rectangle.java class
public class Rectangle implements Shape
{
private double width;
private double height;

public Rectangle()
{
this.width = 2.0; // Default width
this.height = 1.5; // Default height
}

public Rectangle(double width, double height)


{
this.width = width;
this.height = height;
}

@Override
public double getArea()
{
return width * height;
}
}

//Triangle.java class
public class Triangle implements Shape {
private double sideA;
private double sideB;
private double sideC;

public Triangle() {
this.sideA = 1.0; // Default side lengths
this.sideB = 1.0;
this.sideC = 1.0;
}

public Triangle(double sideA, double sideB, double sideC) {


this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}

@Override
public double getArea() {
double s = (sideA + sideB + sideC) / 2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
}

//ShapeFactory.java
class ShapeFactory {
public static Shape createShape(String shapeType, double... parameters) {
switch (shapeType.toLowerCase())
{
case "circle":
if (parameters.length == 1) {
return new Circle(parameters[0]);
} else {
return new Circle();
}
case "triangle":
if (parameters.length == 3)
{
return new Triangle(parameters[0], parameters[1], parameters[2]);
} else {
return new Triangle();
}
case "rectangle":
if (parameters.length == 2) {
return new Rectangle(parameters[0], parameters[1]);
} else {
return new Rectangle();
}
default:
return null;
}
}
}

//main.java

public class Main


{
public static void main(String[] args)
{
Shape circle = ShapeFactory.createShape("circle", 3.0);
Shape triangle = ShapeFactory.createShape("triangle", 4.0, 5.0, 6.0);
Shape rectangle = ShapeFactory.createShape("rectangle", 2.0, 3.0);

System.out.println("Area of Circle: " + circle.getArea());


System.out.println("Area of Triangle: " + triangle.getArea());
System.out.println("Area of Rectangle: " + rectangle.getArea());
}
}
Problem 2:

// Define the Handler Interface

interface AuthorizationHandler
{
void authorize(int amount);
void setNextHandler(AuthorizationHandler nextHandler);
}

// Create Concrete Handlers

class Cashier implements AuthorizationHandler


{
private AuthorizationHandler nextHandler;

@Override
public void authorize(int amount) {
if (amount <= 10000) {
System.out.println("Cashier authorizes the withdrawal.");
} else if (nextHandler != null) {
nextHandler.authorize(amount);
} else
{
System.out.println("Authorization denied.");
}
}

@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
this.nextHandler = nextHandler;
}
}
class SeniorOfficer implements AuthorizationHandler
{
private AuthorizationHandler nextHandler;

@Override
public void authorize(int amount) {
if (amount > 10000 && amount <= 1000000)
{
System.out.println("cashier and senior officer authorizes the withdrawal.");
} else if (nextHandler != null)
{
nextHandler.authorize(amount);
} else {
System.out.println("Authorization denied.");
}
}

@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
this.nextHandler = nextHandler;
}
}

class Manager implements AuthorizationHandler {


@Override
public void authorize(int amount) {
if (amount > 1000000) {
System.out.println("Senior officer and Manager authorizes the withdrawal.");
} else {
System.out.println("Authorization denied.");
}
}

@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
// Manager is the last handler in the chain .
throw new UnsupportedOperationException("Manager cannot have a next handler.");
}
}
//Base class
public class CashWithdrawal
{
public static void main(String[] args)
{
AuthorizationHandler cashier = new Cashier();
AuthorizationHandler seniorOfficer = new SeniorOfficer();
AuthorizationHandler manager = new Manager();

// Create the chain of responsibility


cashier.setNextHandler(seniorOfficer);
seniorOfficer.setNextHandler(manager);

// Request to withdraw money with different amounts


cashier.authorize(10000);
cashier.authorize(25000);
cashier.authorize(150000);
cashier.authorize(5000000);
}
}

You might also like