0% found this document useful (0 votes)
14 views31 pages

Asm 1

The document discusses object-oriented programming concepts including characteristics like encapsulation, inheritance, and polymorphism. It provides multiple code examples demonstrating OOP principles like creating classes for a company, employee, and manager and showing their relationships and handling of data.

Uploaded by

irgjotaro
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)
14 views31 pages

Asm 1

The document discusses object-oriented programming concepts including characteristics like encapsulation, inheritance, and polymorphism. It provides multiple code examples demonstrating OOP principles like creating classes for a company, employee, and manager and showing their relationships and handling of data.

Uploaded by

irgjotaro
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/ 31

ASSIGNMENT 1 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 Le Vinh Hung Student ID GCD191237

Class GCD1001 Assessor name Pham Thanh Son

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 Hung

Grading grid

P1 P2 M1 M2 D1 D2
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Lecturer Signature:
Contents
CHAPTER 1: GENERAL CONCEPTS AND CHARACTERISTICS OF OBJECT-ORIENTED PROGRAMMING (OOP)............................. 7
II. Characteristics of OOP .................................................................................................................................................. 8
❖ Example: Descriptions of OOP scenario: You need to design a piece of code to display the name, start year, and
address of the company on the interface screen.......................................................................................................... 9
❖ Include snippet code and the results of executing the code. ....................................................................................... 9
❖ Explanations ................................................................................................................................................................ 11
❖ For example: Descriptions of OOP scenario: You need to design the program so thatthe employee's name, age,
position, office location, and job can be displayed on thescreen ............................................................................... 12
❖ Include snippet code and the results of executing the code ...................................................................................... 12
❖ Explanation ................................................................................................................................................................. 13
❖ Example: Descriptions of OOP scenario: You need to design the program so that it shows the salary of the
manager, and the manager is holding the employee's salary. ................................................................................... 14
❖ Include snippet code and the results of executing the code. ..................................................................................... 14
❖ Explanation ................................................................................................................................................................. 16
❖ Example: Descriptions of OOP scenario: You need to design the program so thatit shows the salary of the
manager, and the manager is holding the employee's salary .................................................................................... 17
❖ Include snippet code and the results of executing the code. ..................................................................................... 17
❖ Explanation ................................................................................................................................................................. 19
CHAPTER 2: RELATIONSHIPS BETWEEN CLASSES .................................................................................................................... 23
IV. Unary association ................................................................................................................................... 28
CHAPTER 3 : DESIGN AND BUILD CLASS DIAGRAMS USING A UML TOOL .............................................................................. 30
I. UML Class Diagram ..................................................................................................................................................... 30
II. Explain UML Class Diagram ......................................................................................................................................... 30

Figure 1 ............................................................................................................................................................................................ 7
Figure 2 ............................................................................................................................................................................................ 8
Figure 3 ............................................................................................................................................................................................ 9
Figure 4 .......................................................................................................................................................................................... 10
Figure 5 .......................................................................................................................................................................................... 11
Figure 6 .......................................................................................................................................................................................... 11
Figure 7 .......................................................................................................................................................................................... 12
Figure 8 .......................................................................................................................................................................................... 12
Figure 9 .......................................................................................................................................................................................... 13
Figure 10 ........................................................................................................................................................................................ 13
Figure 11 ........................................................................................................................................................................................ 13
Figure 12 ........................................................................................................................................................................................ 14
Figure 13 ........................................................................................................................................................................................ 14
Figure 14 ........................................................................................................................................................................................ 15
Figure 15 ........................................................................................................................................................................................ 16
Figure 16 ........................................................................................................................................................................................ 16
Figure 17 ........................................................................................................................................................................................ 17
Figure 18 ........................................................................................................................................................................................ 17
Figure 19 ........................................................................................................................................................................................ 18
Figure 20 ........................................................................................................................................................................................ 18
Figure 21 ........................................................................................................................................................................................ 19
Figure 22 ........................................................................................................................................................................................ 19
Figure 23 ........................................................................................................................................................................................ 21
Figure 24 ........................................................................................................................................................................................ 21
Figure 25 ........................................................................................................................................................................................ 21
Figure 26 ........................................................................................................................................................................................ 22
Figure 27 ........................................................................................................................................................................................ 23
Figure 28 ........................................................................................................................................................................................ 23
Figure 29 ........................................................................................................................................................................................ 24
Figure 30 ........................................................................................................................................................................................ 24
Figure 31 ........................................................................................................................................................................................ 24
Figure 32 ........................................................................................................................................................................................ 25
Figure 33 ........................................................................................................................................................................................ 25
Figure 34 ........................................................................................................................................................................................ 26
Figure 35 ........................................................................................................................................................................................ 26
Figure 36 ........................................................................................................................................................................................ 27
Figure 37 ........................................................................................................................................................................................ 27
Figure 38 ........................................................................................................................................................................................ 27
Figure 39 ........................................................................................................................................................................................ 28
Figure 40 ........................................................................................................................................................................................ 28
Figure 41 ........................................................................................................................................................................................ 28
Figure 42 ........................................................................................................................................................................................ 29
Figure 43 ........................................................................................................................................................................................ 29
Figure 44 ........................................................................................................................................................................................ 29
Figure 45 ........................................................................................................................................................................................ 30
ASSIGNMENT
CHAPTER 1: GENERAL CONCEPTS AND CHARACTERISTICS OF OBJECT-
ORIENTED PROGRAMMING (OOP).
I. OOP general concepts.
1. Introduce OOP.
Object-oriented programming (OOP) is a programming paradigm based on the concept of
"objects", which may contain data, in the form of fields, often known as attributes; and
code, in the form of procedures, often known as methods. For example, a person is an
object which has certain properties such as height, gender, age, etc. It also has certain
methods such as move, talk, and so on.
2. Object.
An object consists of two pieces of information: properties and methods.
• Properties are the information and characteristics of the object. For example,
humans have characteristics such as eyes, nose, hands, feet, etc.
• Methods are operations and actions that an object can perform. For example, a
person would be able to do the act of speaking, walking, eating, drinking, . . .

Figure 1

3. Class.
A class is a data type consisting of predefined properties and methods. This is the
abstraction of the object. Unlike a normal data type, a class is a (abstract) unit consisting
of a combination of methods and properties. More roughly, objects with similar
properties are grouped into a class of objects.
Figure 2

4. Difference between object and class.


Class you can understand it as template, object is an instance based on that template.
For example: We talk about dogs, you can understand it as a dog class with: Information
and characteristics: 4 legs, 2 eyes, tail, height, weight, coat color... Actions such as barking,
walking, eating, sleeping...The object is the Phu Quoc dog we are raising in the house,
which also has the characteristics of the dog class.
II. Characteristics of OOP.
1. Encapsulation.
• Encapsulation is the ability to hide an object's information from the outside
environment. Encapsulation is done using access directives: private, public, internal,
protected, protected internal.
• The public access directive allows access to the properties and member methods of a
class.
• The private access directive allows the class to hide fields and member methods from
other methods and objects.
• The protected access directive allows the class to hide member fields and methods
from other methods and objects. It only allows a subclass to access these fields and
member methods of the superclass (or base class).
• The internal access directive allows a class to expose its member properties and
methods to other methods and objects in the same assembly.
• The protected internal access directive allows the class to hide member fields and
methods from other methods and objects. It only allows a subclass of the same
assembly as the superclass to access these fields and member methods of the
superclass (or base class).
❖ Example: Descriptions of OOP scenario: You need to design a piece of code to
display the name, start year, and address of the company on the interface screen.
❖ Include class diagram.

Figure 3

❖ Include snippet code and the results of executing the code.


Figure 4
Figure 5

Figure 6

❖ Explanations:
o The class Company is encapsulated in the above program since the variables
are declared as private.
o The Name and Address and Years Begin accessors, which contain the get and
set methods for retrieving and setting the values of private fields, are used to
access these private
o variables.
o Accessors are declared as public so that they can access in other classes.
2. Inheritance.
• Inheritance allows us to define a class in terms of another class, which makes it
easier to create and maintain an application. This also provides an opportunity for
code reuse and faster execution times.
• Inheritance in C# is used with ampersands:
• When creating a class, instead of writing all new data members and member
functions, the programmer might be better off inheriting the members of an
existing class. This existing class is called the Base Class - the base class, and the
new class is referred to as the Derived Class - the derived class.
❖ For example: Descriptions of OOP scenario: You need to design the program so that
the employee's name, age, position, office location, and job can be displayed on the
screen.
❖ Include Class Diagram

Figure 7

❖ Include snippet code and the results of executing the code.

Figure 8
Figure 9

Figure 10

Figure 11

❖ Explanation
o I created the class MyMemployee.
o The class MyGeneralM is an extension of the parent class MyMemployee. It
inherits all its fields and methods, plus defines two additional fields that only
belong to MyGeneralM.
o To test the MyGeneralM class I instantiate a new MyGeneralM object and
printed out all the information
3. Abstraction.
• Abstraction is a process of hiding the implementation details and exposing the
feature only to the user. Abstraction allows you to eliminate the complexity of an
object by exposing only the properties and methods that are needed by the object
in programming.
• Abstraction and encapsulation are two related characteristics in object-oriented
programming. Abstraction allows making related information visible, and
encapsulation gives the programmer the ability to implement inherited
abstraction.
❖ Example: Descriptions of OOP scenario: You need to design the program so that it
shows the salary of the manager, and the manager is holding the employee's salary.
❖ Include Class Diagram.
o Override

Figure 12

o Overloading

Figure 13

❖ Include snippet code and the results of executing the code.


Figure 14
Figure 15

Figure 16

❖ Explanation.
o I create the OverrideEmployee class with the iWork() method.
o The LManager class extends the OverrideEmployee class. Both have an iWork ()
method. By default, LManager inherits its parent’s iWork () method. However,
since it also defines its own iWork() method.
o The OverloadManager class has three work () methods. The first parameter
does not have any parameters, the second has one parameter (salary) and the
third has two parameters (name and salary).
4. Polymorphism.
• The word polymorphism means having many forms. In object-oriented
programming, polymorphism is often expressed as "one interface, many
functions". In c#, polymorphism is divided into two types: static polymorphism and
dynamic polymorphism (2 keywords overloading and overriding).
• Static polymorphism: The technique of binding a function to an object at compile
time is called Early Binding. It is also called Static Binding. C# provides two
techniques for implementing static polymorphism. They are:
o Function overloading
o Operator overloading
❖ Dynamic polymorphism: Dynamic polymorphism in C# is implemented by abstract
classes and virtual functions.
❖ Example: Descriptions of OOP scenario: You need to design the program so that
it shows the salary of the manager, and the manager is holding the employee's
salary.
❖ Include Class Diagram.
o Override.

Figure 17

o Overloading.

Figure 18

❖ Include snippet code and the results of executing the code.


Figure 19

Figure 20
Figure 21

Figure 22

❖ Explanation.

o I create the OverrideEmployee class with the iWork() method.


o The LManager class extends the OverrideEmployee class. Both have an
iWork () method. By default, LManager inherits its parent’s iWork ()
method. However, since it also defines its own iWork() method.
o The OverloadManager class has three work () methods. The first parameter
does not have any parameters, the second has one parameter (salary) and
the third has two parameters (name and salary).
5. Abstraction.
✓ Abstraction is commonly defined as the extraction of relevant information from a
larger data set, where utilizing abstraction allows engineers and others to simplify
a codebase.
✓ Abstract Method: A method, which is declared abstract, has no “body” and
declared inside the abstract class only. An abstract method must be implemented
in all non-abstract classes using the override keyword. After overriding the
abstract method is in the non-Abstract class. We can derive this class in another
class, and again we can override the same abstract method with it.
✓ Abstract Class: This is the way to achieve the abstraction in C#. An Abstract class is
never intended to be instantiated directly. This class must contain at least one
abstract method, which is marked by the keyword or modifier abstract in the class
definition. The Abstract classes are typically used to define a base class in the class
hierarchy. Or in other words, an abstract class is an incomplete class or special
class we cannot be instantiated.
✓ The purpose of an abstract class is to provide a blueprint for derived classes and
set some rules what the derived classes must implement when they inherit an
abstract class. We can use an abstract class as a base class and all derived classes
must implement abstract definitions.
✓ Important Points:
▪ Generally, we use abstract class at the time of inheritance.
▪ A user must use the override keyword before the method, which is
declared abstract in child class, the abstract class is used to inherit in the
child class.
▪ An abstract class cannot be inherited by structures.
▪ It can contain constructors or destructors.
▪ It can implement functions with non-Abstract methods.
▪ It cannot support multiple inheritance.
▪ It cannot be static.
✓ An interface is basically a contract—it does not have any implementation. An
interface can contain only method declarations; it cannot contain method
definitions. Nor can you have any member data in an interface. Whereas an
abstract class may contain method definitions, fields, and constructors, an
interface may only have declarations of events, methods, and properties. Methods
declared in an interface must be implemented by the classes that implement the
interface. Note that a class can implement more than one interface but extend
only one class. The class that implements the interface should implement all its
members. Like an abstract class, an interface cannot be instantiated.
✓ Example of Interface:
Figure 23

Figure 24

Figure 25
Figure 26
CHAPTER 2: RELATIONSHIPS BETWEEN CLASSES.
I. Association.
❖ Association is a “has-a” type relationship. Association establishes the relationship b/w two
classes using through their objects. Association relationship can be one to one, One to
many, many to one and many to many.
❖ For example, suppose we have two classes such as Student and Teacher then these two
classes are said to be “has-a” relationship if both entities share each other’s object for
some work and at the same time they can exists without each other’s dependency, or
both have their own lifetime.

Figure 27

Figure 28
Figure 29

Figure 30

Figure 31

II. Composition.
❖ Composition is a "part-of" relationship. Simply composition means mean use of
instance variables that are references to other objects. In composition relationship
both entities are interdependent of each other for example “engine is part of car”,
“heart is part of body”.
❖ For example, Between House and rooms. House can contain multiple rooms. There is
no independent life of room, and any room cannot belong to two different houses if
we delete the house room will automatically delete. Each class referenced is part-of
the aggregate class.
Figure 32

Figure 33
Figure 34

Figure 35

III. Aggregation.
❖ Aggregation is based is on "has-a" relationship. Aggregation is a special form of
association. In association there is not any classes (entity) work as owner but in
aggregation one entity work as owner. In aggregation both entities meet for some
work and then get separated. Aggregation is a one-way association.
❖ For example, let us take an example of “Student” and “Teacher”. Each student must
have an address so relationship between Student class and Teacher class will be “has-
a” type relationship. So, Teacher work as owner entity. This will be an aggregation
relationship.
Figure 36

Figure 37

Figure 38
Figure 39

Figure 40

IV. Unary association


Unary Association: A knows about B, but B knows nothing about A

Example:

Figure 41
Figure 42

Figure 43

Figure 44
CHAPTER 3 : DESIGN AND BUILD CLASS DIAGRAMS USING A UML
TOOL.
I. UML Class Diagram:

Figure 45

II. Explain UML Class Diagram:


• Above is my entire Uml Class diagram, with a total of 5 classes, 2 interfaces
are created
• First create an abstract class named Product, in Product create fields like id
(int), name (string), dateofmanufacture (date time), expirydate (date
time), price (double) and quantity () all These fields are private. In addition,
in Product also has a constructor Product () and this is the default
constructor. Then create a method called DisplayInfor () and it has the
data type string and it is the function of displaying program input.
• Then I create a class named ProductDetail and this class will inherit the
Product class, but in this class there will be a variable called _type with a
data type of string, it means the product type. In addition we also create a
default constructor named ProductDetail and the DisplayInfor () method
inherits from the Product class, however it overrides the Product class.
• Create an interfaces named IIterator, in this interface there will be
methods such as CurrentIterm () with the return type of Product class, the
First () method with the return type void, Isdone () with the return type of
bool and the end. with Next () the return type is Product class. IIterator is
an interface that defines the operations for accessing collection elements
in a sequence.
• An interface named Ilist has a method createIterator () whose return type
is class IIterator. This interface has the function of defining an operation to
create an iterator, and the operation here is createIterator ().
• Class ListProduct will inherit the Ilist interface and it will implement the
createIterator () function of Ilist. In addition, in this class there is a field
called lisproduct with the return type of Product class. Create a
constructor named ListProduct () and this is a default constructor. We
create a method called addProduct () with the return type void. The
ListProduct class also has an aggregation relationship with the Product
class. It is the responsibility of each ListProduct to instantiate a
ProductIterator that iterates over its set of objects. The iterator interface
provides a set of methods for traversing or modifying sets
• Class ProductIterator will be responsible for implementing all the functions
of class IIterator. In addition, it also has a field named List and position
with data type private, besides creating a constructor ProductIterator.
• Finally, create a class called FunctionApp, it has an association with the
ListProduct class. In this class, we will create 2 filed instance and
listproductdetail both with private access modifier. In addition, there are 3
methods in this class: AddProductDetal () whose return type is void,
ShowProductDetal () has the return type void and finally Open () has the
return type void. These two AddProductDetal (), ShowProductDetal ()
methods will implement the system functions of adding products and
showing all of the store's products. In addition, the above two methods
will be implemented in the open () method.

You might also like