Download Advanced Programming - Assignment 1 and more Papers Computer Applications in PDF only on Docsity!
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 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
P1 P2 M1 M2 D1 D
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Lecturer Signature:
- I. INTRODUCTION
- II. OOP GENERAL CONCEPTS
- Object-oriented Programming (Bodnar, 2022)
- General concepts of OOP
- III. CHARACTERISTICS OF OOP
- Encapsulation
- i. Encapsulation
- ii. Access Modifier
- iii. Properties
- iv. OOP Scenario
- v. Class diagram.................................................................................................................................................
- vi. Snippet Code
- Inheritance
- i. Inheritance
- ii. OOP Scenario
- iii. Class Diagram
- iv. Snippet Code
- Polymorphism
- i. Polymorphism
- ii. Overriding method
- iii. Overloading method
- iv. OOP Scenario
- v. Class diagram...............................................................................................................................................
- vi. Snippet Code
- Abstraction
- i. Abstraction
- ii. Abstract method..........................................................................................................................................
- iii. Abstract class
- iv. Interface
- v. OOP Scenario
- vi. Class diagram...............................................................................................................................................
- vii. Snippet Code
- IV. RELATIONSHIPS BETWEEN CLASSES
- Introduction.....................................................................................................................................................
- Association
- i. Class Diagram
- ii. Snippet Code
- Aggregation
- i. Class Diagram
- ii. Snippet Code
- Composition
- i. Class Diagram
- ii. Snippet Code
- V. CONCLUSION
- VI. References
- Figure 1: CLASS DIAGRAM FOR ENCAPSULATION FIGURES AND TABLES
- Figure 2: PRIVATE ACCESS MODIFIER
- Figure 3: PUBLIC ACCESS MODIFIER
- Figure 4: PROTECTED ACCESS MODIFIER
- Figure 5: INTERNAL ACCESS MODIFIER
- Figure 6: ENCAPSULATION EXAMPLE
- Figure 7: EXAMPLE’S RESULT.......................................................................................................................................
- Figure 8: CLASS DIAGRAM FOR INHERITANCE.............................................................................................................
- Figure 9: INHERITANCE EXAMPLE
- Figure 10: EXAMPLE’S RESULT.....................................................................................................................................
- Figure 11: CLASS DIAGRAM POLYMORPHISM
- Figure 12: POLYMORPHISM EXAMPLE
- Figure 13: EXAMPLE'S RESULT
- Figure 14: ABSTRARCTION CLASS DIAGRAM
- Figure 15: INTERFACE CLASS DIAGRAM
- Figure 16: ABSTRACTION EXAMPLE.............................................................................................................................
- Figure 17: EXAMPLE'S RESULT
- Figure 18: INTERFACE EXAMPLE
- Figure 19: EXAMPLE'S RESULT
- Figure 20: RELATIONSHIP BETWEEN CLASSES
- Figure 21: ASSOCIATION CLASS DIAGRAM
- Figure 22: ASSOCIATION EXAMPLE
- Figure 23: EXAMPLE'S RESULT
- Figure 24: AGGREGATION CLASS DIAGRAM
- Figure 25: AGGREGATION EXAMPLE
- Figure 26: EXAMPLE'S RESULT
- Figure 27: COMPOSITION CLASS DIAGRAM
- Figure 28: COMPOSITION EXAMPLE
- Figure 29: EXAMPLE'S RESULT
a tangible entity like a human being represented by characteristics like name and address to a simple
computer software like a widget.
2. General concepts of OOP - Class: A class is a model, blueprint, or prototype of an object that defines or specifies all of
the object's attributes.
In general, a class declaration has simply the term class, followed by the class's identifier.
However, depending on the application requirements, there are certain optional properties that
can be utilized with class definition. In general, class declarations can have these elements.
(Anon., 2022)
o Modifiers: A class can be public or private, for example. The class's default modifier is
internal.
o Class keyword: The class keyword is used to declare the type class.
o Class Identifier: The variable of type class is supplied. The identifier (or class name)
should begin with an initial letter that is capitalized by convention.
o Super class vs. base class: If there is one, the name of the class's parent (superclass),
preceded by the: (colon). This is entirely optional.
o Interfaces: A comma-separated list of any interfaces implemented by the class,
followed by the: (colon). A class can implement many interfaces. This is entirely
optional.
o Body: The body of the class is surrounded by (curly braces).
- Object: An object is a class entity or instance. Objects are generally physical entities, although
they can also be intellectual entities. Each object has a state and a set of behaviors.
Objects are the fundamental building elements of a C# OOP application. An object is made up of
data and methods. An object's data and methods are referred to as its members. Objects are
created in an OOP software. These objects communicate with one another via methods. Each
object has the ability to receive messages, send messages, and process data. (Bodnar, 2022)
(Anon., 2022)
o State is represented through an object's characteristics. It also reflects an object's
attributes.
o Behavior is expressed through an object's methods. It also represents an object's
interaction with other objects.
o Identity: It provides an object a unique name and allows one thing to communicate with
other objects.
- Method : Methods are functions that are specified within the body of a class. They are utilized
to conduct operations on our objects' characteristics. Our programs become more modular
as a result of the use of methods.
Methods are critical in the OOP paradigm's encapsulation idea. We don't need to know how the
Connect method connects to the database. All we need to know is that it is used to connect to a
database. This is critical when it comes to separating duties in programming, especially in large
projects
- Constructor: A constructor is a subtype of a method. When the object is created, it
automatically calls this method. Values are not returned by constructors.
The constructor's goal is to start an object's state. Constructors are named the same as the class.
Because constructors are methods, they may also be overloaded. Constructors cannot be passed
down. They are referred to according to the sequence of inheritance. If we don't provide a
constructor for a class, C# will supply an implicit default constructor. If we specify any kind of
constructor, then no default is provided. (Anon., 2022)
o A class's constructor must have the same name as the class in which it lives.
o A constructor can't be abstract, final, or synchronous.
- Private: It states that access is restricted to the type that contains it. iii. Properties
A property is a sort of class member that provides a flexible method for reading, writing, or
computing the value of a private field. Properties can be used as if they were public data members, but
they are actually accessors. This facilitates data access and promotes the flexibility and safety of
procedures. Properties can also be used to encapsulate and hide information. It makes use of pre-defined
methods such as "get" and "set" to access and modify properties.
(Anon., 2022)
- When a property has both get and set methods, it is said to be read and write.
- When a property only has the get method, it is said to be read-only.
- When a property only has the set method, it is referred to be a write only property.
- Auto Implemented Properties: Introduced in C# 3.0 when there is no extra logic in the
property accessors.
iv. OOP Scenario
So, we are having some properties set as private including name, ID number and age. The problem
is if we call for the properties in the Main program, the program won’t be executed because the properties
are private and can only be used inside that specific class that contains them. The solution to this is we will
be creating constructors and methods to get those data from the keyboard and set them into the new
variables to use instead of using the original properties.
v. Class diagram Figure 1 : CLASS DIAGRAM FOR ENCAPSULATION vi. Snippet Code
Private Access Modifier: We created an example above to demonstrate the private access modifier
feature. As you can see, the private property number is only accessible within the owner class.
public class NumberClass{ private int number = 10 ; } public static void Main (string[] args){ NumberClass num = new NumberClass(); Console.WriteLine(num.number); } Figure 2 : PRIVATE ACCESS MODIFIER
Public Access Modifier : We created an example above to demonstrate the public access modifier
feature. As you can see, the public property number may be accessed from anywhere in the solution.
public class NumberClass{ public int number = 10 ; } public static void Main (string[] args){ NumberClass num = new NumberClass(); Console.WriteLine(num.number); } Figure 3 : PUBLIC ACCESS MODIFIER
Protected Access Modifier : We created an example above to demonstrate the protected access
modifier feature. Protected is similar to a private access modifier, except the class that inherits the owner
class can also access it.
this.idNum = idNum; this.age = age; } public class RunEncap { public static void main(String args[]) { EncapTest encap = new EncapTest(); encap.name("James"); encap.age( 20 ); encap.idNum("12343ms"); Console.WriteLine("Name : " + encap.name()); Console.WriteLine(" Age : " + encap.age()); } } Figure 6 : ENCAPSULATION EXAMPLE
Name : James
Age : 20
Figure 7 : EXAMPLE’S RESULT
In this example, the properties were kept as private since they are properties, and they shouldn’t be
touched by any means as well as based on the encapsulation’s rules. So, we will create a constructor to
access them indirectly yet not damaging the main properties. This is the most common way to write a class
with properties and constructors.
2. Inheritance i. Inheritance
One of the fundamental notions of object-oriented programming (OOP) languages is inheritance. It
is a methodology for deriving one class from another in a hierarchy of classes that share a set of properties
and methods. (Janssen, 2022)
Inheritance allows programmers to construct classes that are built on top of existing classes, allowing
a class formed through inheritance to inherit the parent class's properties and functions. This indicates
that inheritance promotes code reuse. Methods or, more broadly, software inherited by a subclass are
regarded to be reused in the subclass. A directed graph is formed by the inheritance of connections
between items or classes. (Klein, 2022)
ii. OOP Scenario
In real life, there are tons of animal, and they are having some identical properties. So, the problem
is can we share the same properties or methods between them in order to cut the time coding and
implementing the program. In C#, inheritance will assist us in pass down properties and methods for the
children classes. Below is the demo code for that problem told above.
iii. Class Diagram Figure 8 : CLASS DIAGRAM FOR INHERITANCE iv. Snippet Code class Animal { String name; public void eat() { Console.WriteLine("I can eat"); } } class Dog : Animal { public void bark() { Console.WriteLine("Woof”); } }
ii. Overriding method
Method overriding indicates that the code has two or more methods with the same name, but each
of them performs a unique job that differs from the others. Taking the literal sense of the word itself, this
indicates that one approach must take precedence over another. This idea refers to redefining a base class
method in a derived class with the same method signature.
Overriding is a feature that allows a subclass or child class to offer a customized implementation of
a method that is already given by one of its parent or super-classes. When a method in a subclass has the
same name, parameters or signature, and return type (or sub-type) as a method in its superclass, the
method in the subclass is said to override the method in the superclass. (Mathur, 2022)
iii. Overloading method
Overloading methods is a prominent element of Object-Oriented Programming (OOP). It allows you
to declare the same method with various parameter lists numerous times.
In OOP, method overloading is a type of polymorphism. Polymorphism allows objects or methods to
behave differently depending on how they are utilized. Method overloading is one way in which methods
function differently depending on their parameter types and number of arguments.
Overloading occurs when two methods with the same name, but distinct signatures coexist (or
arguments). We can implement two or more methods with the same name in a class. Overloaded methods
are distinguished by the number and type of parameters given as method arguments. (Singhal, 2022)
iv. OOP Scenario
From the inheritance example earlier, animals have some identical properties and methods. But, for
instance, how the animals sound is different from each other and not identical at all because they are
unique. So, polymorphism was born to solve the problem of have the same methods but do the dissimilar
work.
v. Class diagram Figure 11 : CLASS DIAGRAM POLYMORPHISM vi. Snippet Code class Animal { public void animalSound() { Console.WriteLine("The animal makes a sound"); } } class Pig : Animal { public void animalSound() { Console.WriteLine("The pig says: wee wee"); } } class Dog : Animal { public void animalSound() { Console.WriteLine("The dog says: woof woof"); }
ii. Abstract method
An Abstract method is one that does not have a body. A derived class is responsible for implementing
an abstract method. When a derived class inherits an abstract method from the abstract class, the abstract
method must be overridden. This requirement, which is also known as dynamic polymorphism, is enforced
at compile time. (Sharma, 2022)
iii. Abstract class
An abstract class is a type of unfinished or special class that cannot be instantiated. An abstract class's
goal is to offer a blueprint for derived classes and to provide some rules that derived classes must follow
when they inherit an abstract class.
An abstract class can be used as a base class, and all derived classes must implement abstract
definitions. The override keyword must be used to implement an abstract method in all non-abstract
classes. The abstract method is now in the non-Abstract class after being overridden. We may inherit this
class from another class and use it to override the same abstract method. (Subbaiah, 2022)
iv. Interface
In C#, an interface is a class blueprint. It is similar to an abstract class in that all of the methods
specified within the interface are abstract methods. It is not capable of having a method body and cannot
be created.
It is used to accomplish multiple inheritance, which is not possible with classes. Because it cannot
have a method body, it is used to create complete abstraction.
Its implementation is required to be given by a class or struct. The class or struct that implements
the interface must implement all of the methods defined inside the interface. (Anon., 2022)
v. OOP Scenario
In polymorphism example, we can see that the actions will stay the same even though we are working
with a different class and method, which means that doesn’t meet our expectation. So, abstraction was
here to fix this problem by overriding the methods and the program will execute the way that we want to.
By adding new actions to the overriding method and call it in the main program.
vi. Class diagram Figure 14 : ABSTRARCTION CLASS DIAGRAM Figure 15 : INTERFACE CLASS DIAGRAM