ASM 2 1651 Advanced Programming, Assignments of Programming Languages

Passed Assignment. Good luck to you. Hope you enjoy!

Typology: Assignments

2021/2022

Uploaded on 03/14/2022

dang-hong-nguyen
dang-hong-nguyen 🇻🇳

4.4

(9)

2 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
Student ID
GCD191061
Class
Assessor name
Nguyen Van Loi
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 Dang
Grading grid
P1
P2
M1
M2
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download ASM 2 1651 Advanced Programming and more Assignments Programming Languages 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 NGUYEN PHAN HONG DANG Student ID GCD191 061

Class GCD0 901 Assessor name Nguyen Van Loi

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 Dang

Grading grid P1 P2 M1 M2 D1 D

Summative Feedback:

Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

Signature & Date:

A. INTRODUCTION This assignment, helped me better understand object-oriented programming. In this assignment, I will present all the information about Object Oriented Programming such as properties, characteristics, methods. And will give specific examples of each section. B. OOP general concepts (P1) I. What is OOP? Object-oriented programming (OOP) is a software programming model constructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods). OOP features include the following:

  • Encapsulation: This makes the program structure easier to manage because each object’s implementation and state are hidden behind well-defined boundaries.
  • Polymorphism: This means abstract entities are implemented in multiple ways.
  • Inheritance: This refers to the hierarchical arrangement of implementation fragments.
  • Abstraction: is the process of hiding certain details and showing only essential information to the user. Object-oriented programming allows for simplified programming. Its benefits include reusability, refactoring, extensibility, maintenance, and efficiency. 1. Class The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

Figure 3 Syntax:

<Access_Modifier> <return_type> <method_name> ([<param_list>])

Figure 4: Method In C# a method declaration consists of the following components as follows:

  • Modifier: It defines access type of the method i.e. from where it can be accessed in your application. In C# there are Public, Protected, Private access modifiers.
  • Name of the Method: It describes the name of the user defined method by which the user calls it or refer it. Eg. GetName()
  • Return type: It defines the data type returned by the method. It depends upon user as it may also return void value i.e return nothing
  • Body of the Method: It refers to the line of code of tasks to be performed by the method during its execution. It is enclosed between braces.
  • Parameter list: Comma-separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, then empty parentheses () have to use out. C. Characteristics of OOP (P1+P2)

I. Encapsulation

1. Definition 1.1. Encapsulation Encapsulation is about hiding the implementation details of an object from the consumer of the object. If you are using Java or C#, we use access modifiers like private and protected to make variables and methods available only to the object itself or its library or package. The consumer of the object does not need the internal details of the object, only the publicly available methods, and properties. Another reason may that we may want a consumer to read a value, but not change a value of a property or value. Allowing this could potentially allow bugs to be introduced into an application. Figure 5: Encapsulation In the example above we have a class that has a private variable for the class that has holds the value of the interest for our Mortgage class. We have a method called getInterest that allows a caller to retrieve the interest value. This is how encapsulation works. We could create a method that increments the interest for this class while still preventing the caller direct access. The following are the benefits of encapsulation:

  • Protection of data from accidental corruption
  • Specification of the accessibility of each of the members of a class to the code outside the class
  • Flexibility and extensibility of the code and reduction in complexity
  • Lower coupling between objects and hence improvement in code maintainability 1.2. Access Modifier
  • A. INTRODUCTION...........................................................................................................
  • B. OOP general concepts (P1)
    • I. What is OOP?..........................................................................................................................
        1. Class
        1. Object
        1. Method.........................................................................................................................................................
  • C. Characteristics of OOP (P1+P2)........................................................................................
    • I. Encapsulation
        1. Definition......................................................................................................................................................
        1. OOP Scenario
    • II. Inheritance
        1. Definition....................................................................................................................................................
        1. OOP Scenario
    • III. Polymorphism
        1. Definition....................................................................................................................................................
        1. OOP Scenario
    • IV. Abstraction
        1. Definition....................................................................................................................................................
        1. OOP Sscenario
  • D. Relationships between classes (P1+P2)
    • I. Introduction
    • II. Association
        1. Definition....................................................................................................................................................
        1. Include Class Diagram
        1. Include snippet code and the results of executing the code
    • III. Aggregation
        1. Definition....................................................................................................................................................
        1. Include Class Diagram
        1. Include snippet code and the results of executing the code
    • IV. Composition......................................................................................................................
        1. Definition....................................................................................................................................................
        1. Include Class Diagram
        1. Include snippet code and the results of executing the code
  • E. CONCLUSION
  • References...........................................................................................................................
  • Figure Table of Figure
  • Figure
  • Figure
  • Figure 4: Method
  • Figure 5: Encapsulation
  • Figure
  • Figure
  • Figure
  • Figure 9: Code of Encapsulation
  • Figure
  • Figure 11: Inheritance
  • Figure 12: Diagram of Inheritance
  • Figure
  • Figure
  • Figure 15: Diagram Overriding method
  • Figure 16 Diagram Overloading method....................................................................................................
  • Figure 17: Code of Polymorphism
  • Figure 18: Code of Polymorphism
  • Figure
  • Figure 20: Result of Overriding method
  • Figure
  • Figure
  • Figure 23 Result Overloading method
  • Figure 24: Diagram of Abstract
  • Figure 25: Code of Abstract
  • Figure 26: Result of Abstract
  • Figure 27: Relationship between classes
  • Figure 28: Diagram of Association
  • Figure 29: Code of Association...................................................................................................................
  • Figure 30: Result of Association
  • Figure 31:Diagram of Aggregation
  • Figure 32: Code of Aggregation
  • Figure 33: Result of Aggregation................................................................................................................
  • Figure 34: Diagram of Composition
  • Figure 35: Code of Composition
  • Figure 36: Result of Composition
  • Figure
  • The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.
  • The get method returns the value of the variable name.
  • The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. Automatic Properties (Shorthand): C# also provides a way to use short-hand / automatic properties, where you do not have to define the field for the property, and you only have to write get; and set; inside the property: Figure 7

2. OOP Scenario

2.1. Descriptions of OOP scenario We have a class called Bank and a balance field to store the balance. We declare the balance variable as private in the Bank class and therefore it cannot be accessed directly outside the Bank class. 2.2. Include Class Diagram Figure 8

II. Inheritance

1. Definition....................................................................................................................................................

Inheritance in C# is the process of acquiring all the properties of one class into another class. There are two classes referred to as base class and derived class. The base class is also known as a parent class, and the properties or methods of this class we want to inherit to another class. The derived class is known as the child class that is used to inherit the properties and methods of the base class or parent class. It helps in reusing the same code again, and no need to define the same properties again and again. Inheritance is one of the powerful concepts or fundamental attributes of object-oriented programming language. It is widely used in all the OOPs based programming languages. Its main purpose is to use the same code again. Figure 11: Inheritance

2. OOP Scenario 2.1. Descriptions of OOP scenario This example is to show, how inheritance reacts when we create object of child class or in other words derived class and calling a method that exists in both parent and child classes.

2. Include Class Diagram

Figure 12: Diagram of Inheritance ➢ This diagram represents a super class Animal and two classes that inherit from the super class Cat and Dog. When 2 subclasses inherit from Super Class it will get all the properties of Super Class.

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

Figure 13

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 method to create the same name method with the same signature in a derived class. You can achieve method overriding using inheritance. Virtual and Override keywords are used to achieve method overriding. 1.3. Overloading Method Method Overloading is a type of polymorphism. It has several names like “Compile Time Polymorphism” or “Static Polymorphism” and sometimes it is called “Early Binding”. Method Overloading means creating multiple methods in a class with the same names but different signatures (Parameters). It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures. The compiler automatically calls a required method to check a number of parameters and their type which are passed into that method.

2. OOP Scenario

2.1. Descriptions of OOP scenario Create a simple program to implement polymorphism in OOP. Clarifying overriding methods and overloading methods include Class Diagram.

2. Include Class Diagram

Figure 15: Diagram Overriding method

Figure 16 Diagram Overloading method

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

Overriding method:

Figure 17: Code of Polymorphism 1 This will be the base class from which all other objects will be derived. That's the animalSound() method. A virtual modifier exists in the animalSound() method. The animalSound() method of the Animal class prints the statement "The animal makes a sound" to the terminal.

Figure 20: Result of Overriding method

Overloading method:

Figure 21 In the above code, the method area() is defined twice. First, it's defined with one argument to find the area of the square; and second, it's defined with two arguments length and breadth, to find the area of the rectangle.

Figure 22 To invoke the overloaded methods, call the method with the exact arguments. For example, if we want to invoke the area() method to find the area of a square and the rectangle, we will pass only one argument. Figure 23 Result Overloading method IV. Abstraction

1. Definition....................................................................................................................................................

Abstraction is one of the most important pillars of object-oriented C++ programming language. Data abstraction concept in C++ helps programmers to provide only essential information to the outside world while hiding background details. It’s the most widely used technique that relies on the separation of implementation and interface of the code. Data Abstraction helps the user to increase the flexibility of the code while minimizing the problems and issues. The abstract keyword is used for classes and methods: