Docsity

Object-Oriented Programming (OOP) Concepts: A Comprehensive Guide, Assignments of Advanced Computer Programming

Toggle sidebar
bg1
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 20: Advanced Programming
Submission date 6/23/2022 Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Mai Tran Tuan Anh Student ID GCD201452
Class GCD0904 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 Tuan Anh
Grading grid
P1 P2 M1 M2 D1 D2
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Object-Oriented Programming (OOP) Concepts: A Comprehensive Guide and more Assignments Advanced Computer Programming 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 6/23/2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Mai Tran Tuan Anh Student ID GCD Class GCD0904 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 Tuan Anh Grading grid P1 P2 M1 M2 D1 D

❒ Summative Feedback: ❒ Resubmission Feedback:

Grade: Assessor Signature: Date: Lecturer Signature:

Figure 16: Example of directed association................................................................................................................... Error! Bookmark not defined. Figure 17: Example of reflexive association................................................................................................................... Error! Bookmark not defined. Figure 21: Example of inheritance / generalization....................................................................................................... Error! Bookmark not defined.

  • I. EXAMINE THE CHARACTERISTICS OF THE OBJECT-ORIENTATED PARADIGM AS WELL AS THE VARIOUS CLASS RELATIONSHIPS (P1)......................
      1. OOP definition.....................................................................................................................................................................................................
      1. Concepts..............................................................................................................................................................................................................
    • 2.1 Classes................................................................................................................................................................................................................
    • 2.2 Objects.............................................................................................................................................................................................................
    • 2.3 Encapsulation...................................................................................................................................................................................................
    • 2.4 Abstraction.......................................................................................................................................................................................................
    • 2.5 Inheritance.......................................................................................................................................................................................................
    • 2.6 Polymorphism..................................................................................................................................................................................................
      1. Class relationship...............................................................................................................................................................................................
    • 3.1 Association.......................................................................................................................................................................................................
    • 3.3 Aggregation...................................................................................................................................................................................................... 3.2 Multiplicity........................................................................................................................................................... Error! Bookmark not defined.
    • 3.4 Composition.....................................................................................................................................................................................................
  • II. DESIGN AND BUILD CLASS DIAGRAMS USING A UML TOOL (P2)...........................................................................................................................
      1. Scenario.............................................................................................................................................................................................................
      1. Requirements.....................................................................................................................................................................................................
      1. Application flowchart.........................................................................................................................................................................................
      1. UML diagram.....................................................................................................................................................................................................
      1. Use Case Diagram..............................................................................................................................................................................................
  • IV. BIBLIOGRAPHY......................................................................................................................................................................................................
  • Figure 1: Example of OOP............................................................................................................................................................................................
  • Figure 2: OOP concepts................................................................................................................................................................................................
  • Figure 3: Example of class..........................................................................................................................................................................................
  • Figure 4: Class Person................................................................................................................................................................................................
  • Figure 5: Function in class..........................................................................................................................................................................................
  • Figure 6: Create new objects.....................................................................................................................................................................................
  • Figure 7: Class Person................................................................................................................................................................................................
  • Figure 8: Create object and use function...................................................................................................................................................................
  • Figure 9: Output.........................................................................................................................................................................................................
  • Figure 10: Abstraction example.................................................................................................................................................................................
  • Figure 11: Class Human..............................................................................................................................................................................................
  • Figure 12: Class Student.............................................................................................................................................................................................
  • Figure 13: Class Hinhhoc............................................................................................................................................................................................
  • Figure 14: CLass Hinhtron..........................................................................................................................................................................................
  • Figure 15: Class Hinhchunhat.....................................................................................................................................................................................
  • Figure 19: Example of aggegation.............................................................................................................................................................................. Figure 18: Example of Multiplicity................................................................................................................................. Error! Bookmark not defined.
  • Figure 20: Example of composition............................................................................................................................................................................
  • Figure 23: Application flowchart................................................................................................................................................................................ Figure 22: Example of realzation.................................................................................................................................... Error! Bookmark not defined.
  • Figure 24: UML diagram.............................................................................................................................................................................................
  • Figure 25: Use case diagram......................................................................................................................................................................................

Figure 1 : Example of OOP

  • OOP has a lot of advantages when coming to its usage:  Easier trouble shooting: If there is a bug in the code, let's say the user has no notion where it is. Furthermore, the user has no notion where to look in the code to correct the problem. For typical programming languages, this is quite tough. When Object-Oriented Programming is used, however, the user knows exactly where to look in the code when an error occurs. There's no need to look at other code parts because the error will reveal the source of the problem.  Code reusability: The concept of inheritance is one of the most essential concepts supplied by Object-Oriented Programming. The same attributes of a class are not necessary to be written twice thanks to inheritance. This avoids the problem of having to write the same code many times in a code. The code block can now be used as many times as needed in the program thanks to the introduction of the idea of classes. A child class is built using the inheritance

mechanism, and it inherits the parent class's fields and functions. The parent class's methods and values can be readily overridden. The features of one class can be inherited by another class by expanding the class through inheritance.  Productivity: The adoption of Object-Oriented Programming boosts the productivity of two codes. This is because the OOP has made new programs more accessible by providing so many libraries. Additionally, because it allows for code reuse, the length of a code is reduced, allowing for speedier development of newer codes and programs.  Data redundancy: The term data redundancy refers to the fact that information is duplicated twice. This signifies that the same data appears multiple times. Data redundancy is considered a benefit in Object-Oriented Deprogramming. The user, for example, needs a feature that is comparable to practically all of the classes. In such circumstances, the user can build classes that have comparable functionality and inherit them as needed.  Code flexibility: Polymorphism is a notion that allows for flexibility. For a better understanding of the concept, imagine a situation. There are many types of different shapes and each shape have its own way to calculate the area. In this case, it can be shown that when the shapes change, the way too calculate the area is changed as well. This could help to clarify the concept of polymorphism as well as its adaptability. Polymorphism benefits developers in the following ways: simplicity and extensibility.

  • OOP is one of the most widely accepted development methodologies by developers around the world. This programming approach may be found in several prominent programming languages, including Java, C++, Python, JavaScript, and C#. Classes can be arranged into modules by defining sets of classes that represent and encapsulate objects in a program. This improves the structure and organization of software applications. As a result, developers frequently employ OOP as a tool for creating large programs since classes and their relationships are easier to reason about. 2. Concepts
  • Encapsulation, abstraction, inheritance, and polymorphism are the four essential concepts of object-oriented programming. While these concepts may appear to be difficult to grasp, knowing the broad framework in which they operate will help you grasp the fundamentals of an OOP computer program.

variables; these data members and member functions together define the properties and behavior of the objects in a Class. Basically, a Class is a blueprint that represents a collection of objects that share some common features and behaviors

  • In the picture below, I have defined a class called Human. This class has all features of a basic class, including properties, constructor and function. From this class, I can create an objects with all needed properties and that objects can use all function that are defined in this class. Figure 3 : Example of class.

2.2 Objects

  • An object is an abstract data type defined by a developer. It may contain other objects as well as various properties and functions. Objects are specified as classes in most computer languages. Objects allow for a more organized approach to programming. A developer can quickly generate several comparable objects and edit existing objects within a program by designating a dataset as a custom object. Objects also provide "encapsulation," which means that the data contained within an object is protected from being updated or destroyed by other functions or methods unless specifically allowed.
  • For example, I created a class called Person with properties and constructors. I also override a function called ToString to display the information of the Person Figure 4 : Class Person

(functions or methods). In computer science, a class is an example of encapsulation since it consists of data and procedures that have been bundled into a single unit.

  • Encapsulation can also refer to a mechanism that prevents users from directly accessing specific components of an object, such as state values for all of the object's variables. Data members as well as data functions or methods linked with an instantiated class or object can be hidden via encapsulation. The most significant benefit of encapsulation is data security. The following are some of the advantages of encapsulation:  Clients are prevented from accessing an object if it is encapsulated.  Encapsulation allows you to access a level without disclosing the intricate details beneath it.  It lowers the number of human errors.  The application's maintenance is made easier.
  • For example, Using the person class, the class may contain private data, such as "ID," that should not be visible to other objects in the program. Outside programs would not have direct access to this data member because it was encapsulated as a private variable in the class, and it would remain safe within that person's object. If a method called "personInfo()" is implemented in the person class to make, that function might then access the "ID" variable as needed. Such a class would effectively encapsulate a person's personal information. Figure 7 : Class Person

Figure 8 : Create object and use function

  • The code user will not able to know how the code works, but only see the result of it. Figure 9 : Output 2.4 Abstraction
  • When you can isolate the interface of a class from its implementation and focus on the interface, it's often easier to reason and create a program. Because we are abstracting away the implementation details of a class and just exposing a clear, easy-to- use interface via the class's member methods, this process is called "abstraction" in OOP. When utilized correctly, abstraction isolates the impact of code modifications so that if something goes wrong, it only affects the implementation details of a class and not the outside code.
  • The practice of identifying only the required attributes of an object while discarding the irrelevant information is known as data abstraction. The features and behaviors of an object assist to distinguish it from other things of the same type, as well as classify and group them.
  • Some advantages of abstraction are:  It simplifies the process of seeing things.  Reduces duplication of code and improves reusability.  Only essential details are supplied to the user, which helps to increase the security of an application or software.

2.5 Inheritance

  • The concept of "inheritance" is almost always supported by object-oriented languages that offer classes. Classes can be grouped into hierarchies, with each parent or child class having one or more children. If a class has a parent class, it is said to be derived or inherited from it, and it represents an "IS-A" relationship. That is, the parent class's child class has the "IS-A" type.
  • As a result, if a class inherits from another class, it inherits much of that class's functionality and properties and can be modified to include different code and data. Because the functions of a parent class do not need to be re-defined, inheritance generally leads to effective code reuse. Inheritance can be divided into 2 types, which are single heritance and multiple heritances.
  • For example, we have a problem with human resources. We can create a Human superclass, which contains all properties of a car such as first name, last name and so on. Each person will be a subclass of the Human superclass. Therefore, they can inherit all the properties and assign data for them without having to redefine it In the Human superclass, I will define property name and a constructor as below Figure 11 : Class Human
  • As I create class Student that inherit Human super class, I can instant create a constructor without having to define property name. This is because name has already been inherited from Car class, so I don’t need to redefine it anymore. I can declare more properties for this object without having any problems. Beside Car superclass, these subclasses can also inherit from other classes and have their properties. Figure 12 : Class Student 2.6 Polymorphism
  • Polymorphism is a feature of OOP that allows for the uniform treatment of classes in a hierarchy. As a result, calling code only needs to be written to handle objects from the hierarchy's root, and any object instantiated by any child class will be treated in the same manner. Because derived objects and their parents have the same interface, the caller code can utilize any function in

Figure 13 : Class Hinhhoc

  • Next, I will create 2 other classes, which are the subclass of the Hinhhoc class. These class will inherit the tinhDienTich function, but the code inside will be different, since each of the class is a different shape Figure 14 : CLass Hinhtron Figure 15 : Class Hinhchunhat

3. Class relationship

  • In specific ways, classes are linked to one another. Relationships in class diagrams, in particular, encompass several sorts of logical linkages. In UML, there are several sorts of logical linkages that can be made: 3.1 Association
  • Association is a wide phrase that refers to any logical relationship or connection between classes. For example, we have 2 different classes, which are Driver and Car. Both classes has a name a function. These 2 functions will demonstrate the link between these classes.