0% found this document useful (0 votes)
202 views44 pages

Asm1 - 1651

This document is the front sheet for Assignment 1 of the BTEC Level 5 HND Diploma in Computing unit on Advanced Programming. It provides information such as the student name and ID, class, assessor name, and grading criteria. The student declares that the assignment submission is entirely their own work and they understand the consequences of plagiarism. Space is provided for summative and resubmission feedback along with the grade and signatures of the assessor and lecturer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views44 pages

Asm1 - 1651

This document is the front sheet for Assignment 1 of the BTEC Level 5 HND Diploma in Computing unit on Advanced Programming. It provides information such as the student name and ID, class, assessor name, and grading criteria. The student declares that the assignment submission is entirely their own work and they understand the consequences of plagiarism. Space is provided for summative and resubmission feedback along with the grade and signatures of the assessor and lecturer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Submission date 14/10/2022 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Nguyen Huu Thang Student ID GCD2014443

Class GCD1001 Assessor name

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 Thang

Grading grid

P1 P2 M1 M2 D1 D2

Page | 1
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Lecturer Signature:

Page | 2
Table of Contents
I. Introduction ........................................................................................................................................................................................ 6
II. OOP general concepts......................................................................................................................................................................... 6
1. What is Object Oriented Programming (OOP)? ............................................................................................................................... 6
1.1. Classes....................................................................................................................................................................................... 8
1.2. Object ....................................................................................................................................................................................... 9
1.3. Attributes ................................................................................................................................................................................ 11
1.4. Methods.................................................................................................................................................................................. 11
2. Four Principles of OOP ...................................................................................................................................................................... 13
2.1. Inheritance.............................................................................................................................................................................. 14
2.2. Encapsulation ......................................................................................................................................................................... 17
2.3. Abstraction ............................................................................................................................................................................. 20
2.4. Polymorphism ......................................................................................................................................................................... 26
III. Relationship between classes ........................................................................................................................................................... 29
1. Association ..................................................................................................................................................................................... 29
1.1. Unary association ................................................................................................................................................................... 30
1.2. Binary Association .................................................................................................................................................................. 33
2. Aggregation .................................................................................................................................................................................... 38
3. Composition ................................................................................................................................................................................... 41
References ................................................................................................................................................................................................ 44

Page | 3
Table of Figures
Figure 1 Class blueprint being used to create two Car type objects, myCar and helensCar ...................................................................... 7
Figure 2 Example of Classes ........................................................................................................................................................................ 9
Figure 3 Example of Object ....................................................................................................................................................................... 10
Figure 4 Example of Methods ................................................................................................................................................................... 12
Figure 5 Principles OOP ............................................................................................................................................................................. 13
Figure 6 Example of Inheritance ............................................................................................................................................................... 14
Figure 7 Employee diagram ...................................................................................................................................................................... 15
Figure 8 Inheritance example code .......................................................................................................................................................... 16
Figure 9 Inheritance example result ......................................................................................................................................................... 16
Figure 10 Encapsulation ............................................................................................................................................................................ 17
Figure 11 Example diagram ...................................................................................................................................................................... 18
Figure 12 Encapsulation example code .................................................................................................................................................... 19
Figure 13 Encapsulation example result ................................................................................................................................................... 20
Figure 14 Abstraction ................................................................................................................................................................................ 21
Figure 15 Interface .................................................................................................................................................................................... 22
Figure 16 Difference between Abstract Class and Interface .................................................................................................................... 24
Figure 17 Abstract diagram ....................................................................................................................................................................... 24
Figure 18 Abstract Class example code .................................................................................................................................................... 25
Figure 19 Abstract Class example result ................................................................................................................................................... 26
Figure 20 Polymorphism diagram ............................................................................................................................................................. 27
Figure 21 Polymorphism example code ................................................................................................................................................... 28
Figure 22 Polymorphism example code ................................................................................................................................................... 29
Figure 23 Polymorphism example result .................................................................................................................................................. 29
Figure 24 Association ................................................................................................................................................................................ 30
Figure 25 Unary association class diagram ............................................................................................................................................... 30
Figure 26 Unary association example code .............................................................................................................................................. 32

Page | 4
Figure 27 Unary association example result ............................................................................................................................................. 33
Figure 28 Binary association class diagram .............................................................................................................................................. 33
Figure 29 Binary association example code.............................................................................................................................................. 35
Figure 30 Binary association example code.............................................................................................................................................. 36
Figure 31 Binary association example code.............................................................................................................................................. 37
Figure 32 Binary association example result ............................................................................................................................................ 37
Figure 33 Aggregation symbol .................................................................................................................................................................. 38
Figure 34 Aggregation class diagram ........................................................................................................................................................ 39
Figure 35 Example of aggregation 1 ......................................................................................................................................................... 40
Figure 36 Example of aggregation 2 ......................................................................................................................................................... 41
Figure 37 Result of aggregation example ................................................................................................................................................. 41
Figure 38 composition symbol .................................................................................................................................................................. 42
Figure 39 Composition class diagram ....................................................................................................................................................... 42
Figure 40 Example of composition 1......................................................................................................................................................... 43
Figure 42 Result of composition example ................................................................................................................................................ 44

Page | 5
I. Introduction
Software is created and developed more quickly when using programming languages that offer extensive functionalities. The
application runs more quickly, the code is easier to comprehend and scale, and expenses may be reduced as a result. One or more
design patterns will be used when creating a large number of the commercial or entertainment applications that are already
available, whether they are for business or play. A design pattern is a description of a problem-solving approach that may be applied
to numerous different contexts. A common and situation-specific nomenclature is provided by design patterns. As a result, it may
aid in your quest to learn more about object-oriented programming and enhance the usability and design of your product.

• The properties of the object-oriented paradigm and the different class relationships.
• With the use of a UML tool, create class diagrams.
• Choose a design pattern from each of the structural, behavioral, and creative pattern categories.
• Use a UML tool to specify class diagrams for particular design patterns.
• Examine how design patterns and the object-oriented paradigm interact.
• Utilizing a UML tool, define and improve class diagrams drawn from a particular code scenario.

II. OOP general concepts


1. What is Object Oriented Programming (OOP)?
A class is a generalized template that can be used to build more specialized, concrete things. Broad categories with shared
properties, like Car or Dog, are frequently represented using classes. These classes specify the characteristics of a type instance, such
as the color, but not the value of those characteristics for a particular object (Doherty, 202).

Classes can also include methods, which are special functions only accessible by objects of that kind. These functions, which are
defined within the class, carry out some helpful actions for that particular sort of object.

Page | 6
o For example, the Car class might contain a method called repaint that modifies the car's color attribute. This function is only
useful for objects of type Car, thus we declare it as a method within the Car class.

Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like
myCar or goldenRetriever. Each object can have unique values to the properties defined in the class.

o For example, say we created a class, Car, to contain all the properties a car must have, color, brand, and model. We then
create an instance of a Car type object, myCar to represent my specific car. We could then set the value of the properties
defined in the class to describe my car, without affecting other objects or the class template. We can then reuse this class to
represent any number of cars.

Figure 1 Class blueprint being used to create two Car type objects, myCar and helensCar
Page | 7
Building blocks of OOP

Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:

o Classes
o Objects
o Methods
o Attributes

1.1. Classes
In a nutshell, classes are essentially user defined data types. Classes are where we create a blueprint for the structure of methods
and attributes. Individual objects are instantiated, or created from this blueprint (Doherty, 202).

Classes contain fields for attributes, and methods for behaviors. In our Dog class example, attributes include name & birthday, while
methods include bark() and updateAttendance().

Here’s a code snippet demonstrating how to program a Dog class using the JavaScript language.

Page | 8
Figure 2 Example of Classes

1.2. Object
An object is a concrete instance of a class. It is the things that appear around us in the real world that exhibit their own unique
properties. Anything from laptops, phones, people to the car we're driving are all objects. Basically, an object can consist of one
or more other objects.

Of course OOP includes objects! Objects are instances of classes created with specific data, for example in the code snippet
below Rufus is an instance of the Dog class.
Page | 9
Figure 3 Example of Object

Page | 10
1.3. Attributes
Attributes are the information that is stored. Attributes are defined in the Class template. When objects are instantiated individual
objects contain data stored in the Attributes field (Doherty, 202).

An object's state is determined by the information in its attribute fields. For instance, at pet camp, a puppy and a dog would receive
different treatment. The birthday could specify an object's status and enable the software to treat dogs of various ages differently.

1.4. Methods
Methods represent behaviors. Methods perform actions; methods might return information about an object, or update an object’s
data. The method’s code is defined in the class definition (Doherty, 202).

When individual objects are instantiated, these objects can call the methods defined in the class. In the code snippet below, the bark
method is defined in Dog class, and the bark() method is called on the Rufus object.

Page | 11
Figure 4 Example of Methods

Methods often modify, update or delete data. Methods don’t have to update data though. For example the bark() method doesn’t
update any data because barking doesn’t modify any of the attributes of the Dog class: name or birthday. The updateAttendance()
method adds a day the Dog attended the pet sitting camp. The attendance attribute is important to keep track of for billing Owners
at the end of the month. Methods are how programmers promote reusability, and keep functionality encapsulated inside an object.
This reusability is a great benefit when debugging. If there’s an error, there’s only one place to find it and fix it instead of many. The
underscore in _attendance denotes that the variable is protected, and shouldn’t be modified directly. The updateAttendance()
method is used to change _attendance.
Page | 12
2. Four Principles of OOP
The four pillars of object oriented programming are:

o Inheritance: child classes inherit data and behaviors from parent class.
o Encapsulation: containing information in an object, exposing only selected information.
o Abstraction: only exposing high level public methods for accessing an object.
o Polymorphism: many methods can do the same task.

Figure 5 Principles OOP

Page | 13
2.1. Inheritance
Inheritance is a in the base of essence of the Object Oriented Programming (Object Oriented Programming - OOP for short). The
remaining properties include: Encapsulation, Objectivity (Abstract) and Polymorphism (Polymorphism).

Inheritance can be defined as the process by which one class (class) has the properties of another class. The property can be a
method (method) or a field (field). The derived class will be referred to as the parent class, and the derived class will be referred to
as the child class.

Figure 6 Example of Inheritance

The idea that comes from Inheritance is the relationships in life. Restrictions like "dalmatian" and "mast dog" are two classes of the
parent class "dog". Therefore, both these classes inherit the special features of the parent class.

Types of Inheritance in OOP

In OOP, there are several types of inheritance as follows:


Page | 14
o Single Inheritance: this is the case where a class can only inherit from a single class, i.e. a subclass has only one superclass.
o Multiple Inheritance: this is a case where a class can inherit from more than one other class, which means that a subclass can
have more than one superclass.
o Multi-Level Inheritance: in this case there will be a derived class created from another derived class.
o Hierarchical Inheritance: This is the case where many subclasses will inherit from a single parent class.
o Hybrid Inheritance: also known as Virtual Inheritance. This is the case where inheritance is combined by more than one type
of inheritance, such as combining Single Inheritance with Multiple Inheritance.

Example: This is an example of single-level inheritance in C# programming, talking about inheritance of employees and classifiers
in the system.
Code and result :

Figure 7 Employee diagram

Page | 15
Figure 8 Inheritance example code

Figure 9 Inheritance example result

Page | 16
2.2. Encapsulation
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of bundling data and
methods that work on that data within one unit, e.g., a class in C#.

This concept is also often used to hide the internal representation, or state, of an object from the outside. This is called information
hiding. The general idea of this mechanism is simple. If you have an attribute that is not visible from the outside of an object, and
bundle it with methods that provide read or write access to it, then you can hide specific information and control access to the
internal state of the object.

Figure 10 Encapsulation

If you’re familiar with any object-oriented programming language, you probably know that these methods as getter and setter
methods. As the names indicate, a getter method retrieves an attribute, and a setter method changes it. Depending on the methods
that you implement, you can decide if an attribute can be read and changed, or if it’s read-only, or if it is not visible at all. As I will

Page | 17
show you later, you can also use the setter method to implement additional validation rules to ensure that your object always has a
valid state.

Access Modifier: are keywords in object-oriented languages that set the accessibility of classes, methods, and other members...
The class will be accessible to other classes in the same package but will be inaccessible to classes outside the package.
Property: A property, in some object-oriented programming languages, is a special sort of class member, intermediate in

functionality between a field (or data member) and a method.

Example: An example of applying encapsulation in OOP, calculates the area of a rectangle after the length and width
are obtained.

Figure 11 Example diagram

Code and result :

Page | 18
Figure 12 Encapsulation example code

Page | 19
Figure 13 Encapsulation example result

2.3. Abstraction
a. Abstraction Class
Abstraction is the ability to hide implementation details, providing only feature information to the user.

Abstraction is expressed through abstract classes and interfaces.

Page | 20
Figure 14 Abstraction

Implement abstraction with Abstract class

Use abstract keyword to declare abstract class in C#. Abstract classes have the following characteristics:

o Cannot create instance for abstract class.


o An abstract class can contain regular methods, virtual methods, and abstract methods.
o The sealed keyword cannot be used for abstract class because it prevents a derived class from inheriting abstract class, while
abstract class needs to be inherited because it is not possible to create an instance from abstract class.

Abstract class: If a class is defined as abstract then we can't create an instance of that class. By the creation of the derived class
object where an abstract class is inherit from, we can call the method of the abstract class.

Page | 21
Abstract methods: An Abstract method is a method without a body. The implementation of an abstract method is done by a derived

class. When the derived class inherits the abstract method from the abstract class, it must override the abstract method. This

requirment is enforced at compile time and is also called dynamic polymorphism.

b. Interface
In the human world, a contract between the two or more humans binds them to act as per the contract. In the same way, an
interface includes the declarations of related functionalities. The entities that implement the interface must provide the
implementation of declared functionalities.

In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods, properties,
indexers, and events. However, it cannot contain fields, auto-implemented properties. The following interface declares some basic
functionalities for the file operations.

Figure 15 Interface
Page | 22
You cannot apply access modifiers to interface members. All the members are public by default. If you use an access modifier in an
interface, then the C# compiler will give a compile-time error "The modifier 'public/private/protected' is not valid for this item.".
(Visual Studio will show an error immediately without compilation.)

c. Difference between Abstract Class and Interface

Abstract Class Interface


An abstract class can have both abstract and non-abstract The interface can have only abstract methods.
methods.
It does not support multiple inheritances. It supports multiple inheritances.

It can provide the implementation of the interface. It can not provide the implementation of the abstract class.

An abstract class can have protected and abstract public methods An interface can have only have public abstract methods.

An abstract class can have final, static, or static final variable with
any access specifier.

Page | 23
Figure 16 Difference between Abstract Class and Interface

Example: example of implementing abstraction in object-oriented programming with classification for file types

Figure 17 Abstract diagram

Code and result:

Page | 24
Figure 18 Abstract Class example code
Page | 25
Figure 19 Abstract Class example result

2.4. Polymorphism
C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation
iscompleted when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived
class. The derived classes have more specialized functionality.

Here are the rules about abstract classes :

o You cannot create an instance of an abstract class


o You cannot declare an abstract method outside an abstract class
o When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.

Overriding: Method Overriding means having two methods with the same name and same signatures [parameters], one should be in
the base class and another method should be in a derived class [child class]. You can override the functionality of a base class

Page | 26
method to create a same name method with same signature in a derived class. You can achieve method overriding using
inheritance. Virtual and Override keywords are used to achieve method overriding.

Overloading: Method Overloading means creating multiple methods in a class with same names but different signatures
(Parameters). It permits a class, strut, or interface to declare multiple methods with the same name with unique signatures. The
compiler automatically calls required method to check number of parameters and their type which are passed into that method. ❖
Example: example calculate area of rectangle and triangle from given parameters based on inheritance of C#

Class diagram:

Figure 20 Polymorphism diagram

Example code and result:

Page | 27
Figure 21 Polymorphism example code

Page | 28
Figure 22 Polymorphism example code

Figure 23 Polymorphism example result

III. Relationship between classes


1. Association
Any logical connection or interaction between classes is referred to as association, which is a general term. As seen above, a
relationship between a passenger and an airline is an example.
Page | 29
Figure 24 Association

1.1. Unary association


Scenario:

I'll use the classes Person and Address to illustrate the concept of unary association. Since Person may display their Address using
the ShowInfor() method, this implies that Person has Address but Address does not have Person.

Class diagram:

Figure 25 Unary association class diagram

Page | 30
This is the class diagram of my scenario, a person has two fields name and address, and the function ShowInfor(), and in the class
Address has two fields which are street and city. The image below show class Address with two properties, Street and City,
constructor. Class Person with two properties includes: Name and Address, and constructor. The Address is the properties, or it is an
object of class Address, this proves that each person will have corresponding address, but the address cannot have a person. This is a
result after creating person object, assign parameters to it, calling the ShowInfor() function and running the program.

Page | 31
Figure 26 Unary association example code
Page | 32
Result:

Figure 27 Unary association example result

1.2. Binary Association


Scenario:

To demonstrate about unary association, I will use two class Father and Son.

Class Diagram:

Figure 28 Binary association class diagram


Page | 33
This is the class diagram of two class Saler and Customer, they know each other because each class contains the object of other
class.

Class Customer which contains properties Name, Address, and Saler (object) from class Saler. Class Saler which contains properties
Name, Address, and Customer (object) from class Customer.

Page | 34
Figure 29 Binary association example code

Page | 35
Figure 30 Binary association example code

Page | 36
Figure 31 Binary association example code

Result: Below is the result, it proves that the binary aggregation relationship is true, because the customer have the name of
their saler, and the saler have the name of the customer.

Figure 32 Binary association example result

Page | 37
2. Aggregation
Aggregation is usually represented in UML using a line with a hollow diamond. Like association, aggregation can involve a one-to-
one, one-to-many, or many-to-many relationship between the participating objects. In the case of a one-to-many or many-to-many
relationship, we may say that it is a redundant relationship.

Aggregation is usually represented in UML using a line with a hollow diamond. Like association, aggregation can involve a one-to-
one, one-to-many, or many-to-many relationship between the participating objects. In the case of a one-to-many or many-to-many
relationship, we may say that it is a redundant relationship.

Figure 33 Aggregation symbol

Class Diagram:

Page | 38
Figure 34 Aggregation class diagram

Page | 39
Example:

Figure 35 Example of aggregation 1

Page | 40
Figure 36 Example of aggregation 2

The above code is an example of aggregation where Person class has the reference of Address class as data member. In such way, it
can reuse the members of Address class.

Result:

Figure 37 Result of aggregation example

3. Composition
Another sort of relationship between classes is composition, which allows one class to contain another.

The Has-A connection is commonly used to describe composition. A car, for example, has an engine.

Composition, like inheritance, allows for code reuse. Composition allows for additional flexibility in the design of loosely linked
applications.

Page | 41
It's possible that we'll have a logging class that solely logs to a text file. This class can then be used to record anything by any other
class in our application; it isn't tied to a single class and will only log for that class.

Figure 38 composition symbol

Class Diagram:

Figure 39 Composition class diagram

Example:

Page | 42
Figure 40 Example of composition 1

Result:

Page | 43
Figure 41 Result of composition example

References
Doherty, E., 202. Educative. [Online]
Available at: https://www.educative.io/blog/object-oriented-programming
[Accessed 15 April 2020].

Page | 44

You might also like