Advanced Programming - Assignment 2, Papers of Computer Applications

1651 - Advanced Programming - Assignment 2

Typology: Papers

2021/2022

Uploaded on 04/05/2022

unkaeciique
unkaeciique 🇻🇳

4.7

(174)

29 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 | H o N g o c K h a n h
ASSIGNMENT 2 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 20: Advanced Programming
Submission date
Date Received 1st submission
Re-submission Date
Date Received 2nd submission
Student Name
Hồ Ngọc Khánh
Student ID
GCS200074
Class
GCD0901
Assessor name
Nguyễn Văn Lợi
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Khanh
Grading grid
P3
P4
M3
M4
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Advanced Programming - Assignment 2 and more Papers Computer Applications in PDF only on Docsity!

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Hồ Ngọc Khánh Student ID GCS

Class GCD0901 Assessor name Nguyễn Văn Lợi

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that

making a false declaration is a form of malpractice.

Student’s signature Khanh

Grading grid

P3 P4 M3 M4 D3 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Lecturer Signature:

  • I. Introduction...........................................................................................................................................................
  • II. Scenario analysis
      1. Scenario
      1. Class Diagram of given scenario
      1. Break down Class diagram
  • III. Implementation
      1. Code...................................................................................................................................................................
      1. Program screenshots
  • IV. Design Patterns
      1. Creational patterns..........................................................................................................................................
      1. Behavioral patterns
      1. Structural patterns
  • V. References
  • Figure 1: CLASS DIAGRAM FIGURES AND TABLES
  • Figure 2: CLASS TAXIORG...............................................................................................................................................
  • Figure 3: CLASS TAXI
  • Figure 4: CLASS CILENT
  • Figure 5: CLASS INFORMATION
  • Figure 6: CLASS CLIENTINFORMATION
  • Figure 7: CLASS DRIVERINFORMATION
  • Figure 8: TAXIORG CLASS.............................................................................................................................................
  • Figure 9: TAXI CLASS
  • Figure 10: CLIENT CLASS
  • Figure 11: INFORMATION CLASS
  • Figure 12: CLIENTINFORMATION CLASS
  • Figure 13: DRIVERINFORMATION CLASS
  • Figure 14: UNAVAILABLETAXIEXCEPTION
  • Figure 15: FINAL RESULT - COLLECTING DATA
  • Figure 16: FINAL RESULT - PARING CUSTOMERS AND DRIVERS
  • Figure 17: SINGLETON EXAMPLE (Anon., 2018)
  • Figure 18: FACTORY EXAMPLE (Anon., 2018)
  • Figure 19: OBJECT POOL EXAMPLE (Anon., 2018).......................................................................................................
  • Figure 20: PROTOTYPE DESIGN PATTERN (Anon., 2018).............................................................................................
  • Figure 21: ADAPTER EXAMPLE (Anon., 2018)
  • Figure 22: DECORATOR EXAMPLE (Anon., 2018)
  • Figure 23: COMPOSITE EXAMPLE (Anon., 2018)
  • Figure 24: TEMPLATE METHOD EXAMPLE (Anon., 2018)............................................................................................
  • Figure 25: COMMAND EXAMPLE (Anon., 2018)
  • Figure 26: STRATEGY EXAMPLE (Anon., 2018)
  • Table 1: CREATIONAL PATTERNS
  • Table 2: BEHAVIORAL PATTERNS
  • Table 3: STRUCTURAL PATTERNS

I. Introduction

In the previous report, we have already discussed about everything goes around OOP which includes

OOP concepts, characteristics of OOP and so much more. Furthermore, we also learned briefly about UML

and the connection between classes.

In this report, I will choose a specific situation to implement a program so I could take all of the

knowledge from the first report in action. Also, the report will include the UML class diagram for easier

comprehending. Lastly, a small part of discussing the design pattern to use further in the next projects.

II. Scenario analysis

1. Scenario

A taxi service A has a restricted number of N taxis, and the taxi business is responsible for controlling

the condition of the vehicles (whether they are free or carrying passengers), allocating free cars to pick up

customers, taking care of, and extending waiting periods. If all cars are full (it is essential to pick up the

client while waiting for one of them to become available), cancel when the customer wait is too lengthy.

Create an app to imitate the above-mentioned company's job.

• The taxi firm has a certain number of vehicles. As a result, you cannot start a new taxi every

time you receive a call; instead, you must re-use a taxi that is currently available.

• Customers can be served by the free cab that is currently available.

• The taxi that is now serving consumers will notify the firm that it is transporting customers.

• Each passenger has a set waiting time; if consumers wait too long without taxi service, the

trip is canceled.

Below will be all of the needed classes and methods to create this program

• Taxi: represents a taxi, is a class that defines the properties and methods of a taxi.

• TaxiOrg: A representative for a taxi company, has:

2. Class Diagram of given scenario

Figure 1 : CLASS DIAGRAM

3. Break down Class diagram

Figure 2 : CLASS TAXIORG

  • Properties

o EXPIRED_TIME: a private constraint, defines the maximum time to waiting until the

taxi available.

o numberOfTaxi: public and defines how many taxies that company manages

o availableTaxi: a private list and contains all the available taxies

o inUseTaxi: a private list and contains all the in use taxies

o taxiCount: private and counts all the taxies from data

o waitingCheck: a boolean property and check on the availability of taxies

  • Methods

o TaxiOrg(): private constructors for class TaxiOrg

Figure 4 : CLASS CILENT

  • Properties:

o ClientInfo: a public list, used to contain clients’ information

o taxiOrg: a private object to work inside Client class

  • Method:

o CallATaxi(): is the public method to simulate client to take taxi and free that taxi after

using

Figure 5 : CLASS INFORMATION

  • Properties

o Id: protected, used to store ID

o Name: protected, used to store name

o PhoneNumber: protected, used to store phone number

  • Methods

o Information(): sets constructors for programmer to use instead of using fields

o ShowInfo(): public abstract method to force class inherit class Information to define

the action inside

Figure 6 : CLASS CLIENTINFORMATION

• Methods

o ClientInformation(): sets constructors for programmers to use instead of using private

fields

o ShowInfo(): inherits from Information class

Figure 7 : CLASS DRIVERINFORMATION

• Methods

o DriverInformation(): sets constructors for programmers to use instead of using private

fields

o ShowInfo(): inherits from Information class

III. Implementation

1. Code

• TaxiOrg class

waitingCheck = false; throw new UnavailableTaxiException("Not enough taxi for clients"); } waitingCheck = true; Waiting(EXPIRED_TIME); } private void Waiting(int seconds) { try { Thread.Sleep(seconds); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } Figure 8 : TAXIORG CLASS

  • Taxi class public class Taxi { public static List DriverInfo = new List(); private string _name; public Taxi(string name) { this._name = name; } public string Name { get { return _name; } set { this._name = value; } } public override string ToString() { return "Taxi - " + _name + "]"; } } Figure 9 : TAXI CLASS
  • Client class public class Client { public static List clientInfo = new List(); private TaxiOrg taxiOrg; public Client(TaxiOrg TaxiOrg) {

this.taxiOrg = TaxiOrg; } public void CallATaxi() { try { Console.WriteLine("New Client: " + Thread.CurrentThread.Name); Taxi taxi = taxiOrg.CallTaxi(); taxiOrg.FreeTaxi(taxi); Console.WriteLine("This client is being served: " + Thread.CurrentThread.Name); } catch (UnavailableTaxiException ex) { Console.WriteLine("This client has been rejected: " + Thread.CurrentThread.Name); } } } Figure 10 : CLIENT CLASS

  • Information class public abstract class Information { protected int Id; protected string Name; protected string PhoneNumber; public Information(int id, string name, string phoneNumber) { this.Id = id; this.Name = name; this.PhoneNumber = phoneNumber; } public abstract string ShowInfo(); } Figure 11 : INFORMATION CLASS
  • ClientInformation class public class ClientInformation: Information { public ClientInformation(int id, string name, string phoneNumber): base(id, name, phoneNumber) { } public override string ShowInfo() { return $"{Name}, has the ID of {Id} and his/her phone number is {PhoneNumber}";

2. Program screenshots

Figure 15 : FINAL RESULT - COLLECTING DATA Figure 16 : FINAL RESULT - PARING CUSTOMERS AND DRIVERS

IV. Design Patterns

1. Creational patterns

PATTERN

NAME

CLASS DIAGRAM SHORT EXPLANATION

Singleton

Figure 17 : SINGLETON EXAMPLE (Anon., 2018)

The singleton design

pattern ensures that a

class has only one

instance and provides a

global access point to this

instance.

Factory

Figure 18 : FACTORY EXAMPLE (Anon., 2018)

The Factory Method is a

creational design pattern

that defines an interface

for producing an object

while allowing subclasses

to choose which class to

instantiate.

The Factory Method

allows a class to delegate

instantiation to

subclasses.

2. Behavioral patterns

PATTERN

NAME

CLASS DIAGRAM SHORT EXPLANATION

Adapter

Figure 21 : ADAPTER EXAMPLE (Anon., 2018)

The Adapter Pattern is a

structural design pattern

that converts a class's

interface into another

interface that customers

anticipate.

Adapters allow classes

that would otherwise be

unable to collaborate

due to conflicting

interfaces to do so.

Decorator

Figure 22 : DECORATOR EXAMPLE (Anon., 2018)

Dynamically assign extra

responsibilities to an

item.

Decorators offer a more

versatile alternative to

subclassing for adding

functionality.

Composite

Figure 23 : COMPOSITE EXAMPLE (Anon., 2018)

To depict part-whole

hierarchies, compose

things into tree

structures.

Clients can use

Composite to handle

individual objects and

object combinations

consistently.

Table 2 : BEHAVIORAL PATTERNS

3. Structural patterns

PATTERN

NAME

CLASS DIAGRAM SHORT EXPLANATION

Template

method

Figure 24 : TEMPLATE METHOD EXAMPLE (Anon., 2018)

In an operation, define

the skeleton of an

algorithm while

delegating some steps

to client subclasses.

Subclasses can use the

Template Method to

rewrite certain stages

of an algorithm

without affecting the

algorithm's structure.

Command

Figure 25 : COMMAND EXAMPLE (Anon., 2018)

Encapsulate a request

as an object, allowing

you to parameterize

clients with various

requests, queue or log

requests, and offer

undoable activities.

Strategy

Figure 26 : STRATEGY EXAMPLE (Anon., 2018)

Define an algorithm

family, encapsulate

each one, and make

them interchangeable.

The strategy allows the

algorithm to change

independently of the

clients that utilize it.

Table 3 : STRUCTURAL PATTERNS