Tải bản đầy đủ (.pdf) (54 trang)

btec level 5 hnd diploma in computing unit 20 advanced programming4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.7 MB, 54 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Re-submission Date 8/6/2023 Date Received 2nd submission

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

Summative Feedback:

❒❒

Resubmission Feedback:

Lecturer Signature:

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

I. Introduction

Advanced programming is a subject that encompasses all programming language paradigms and techniques. Object-oriented programming (OOP) is a programming language model based on objects rather than procedures and data rather than reasoning. Historically, a program was thought to be a logical method that takes input data, processes it, and outputs data.

In this project, I was asked to create a software system for my organization; but, before I could do so, I needed to learn advanced programming concepts, be able to build patterns, and understand object-oriented programming solutions. In addition, I will investigate the essential components of the object-oriented programming paradigm and study design pattern types. In addition, I use a UML tool to develop and design class diagrams for certain design patterns. Finally, I will examine the link between the object-oriented paradigm and design patterns and incorporate the findings into my paper.

II. Object-oriented programming and the various class relationships (P1)

1. Object-oriented programming

Object-oriented programming (OOP) is a vital programming method in today's world. It is useful in the majority of commercial construction applications. Object-oriented programming is supported by the majority of prominent programming languages and frameworks, including Java, C#, and.NET. Most programmers have studied object-oriented programming in college, but the fundamental concepts of object-oriented programming do not always teach how to abuse the erroneous programming philosophy.

Definition of OOP: Object-oriented programming is a programming method that allows programmers to design code objects that abstract real-world items. This strategy is presently quite effective and has become one of the software development templates, particularly for corporate applications.

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

We shall define classes to model actual objects while designing applications with OOP. These classes will be initialized as objects in the program, and the methods of these objects will be called when the application is running.

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

What is the class that defines the object? Methods and attributes are included. An object is simply a class instance. Classes communicate with one another via the public API's collection of methods and public attributes.

2. Characteristics of OOP

a. Abstraction

Abstraction is a non-specific attribute in Java that just names the problem. It is the technique of concealing the inner workings of a thing and just displaying its vital characteristics to the user. If a person uses his phone to send a message, he will enter the message's text and the recipient's details, and then push the send button. When he started sending the message, he had no idea what was going on during the procedure and only knew if the message had been successfully sent to the receiver. In the above instance, the process of sending the message has been concealed and only reveals the functions that the user requires, which are the function of inputting the message content, the recipient information, and the result of successfully or unsuccessfully sending the message. That is an example of abstraction.

Advantages of using abstractions in programming:

o Abstraction helps programmers reduce the complexity of an object by just providing the object's required properties and methods in programming, boosting system maintainability.

o Abstraction allows us to focus on the basic core of the thing and not how it operates.

o When used with polymorphism and inheritance in object-oriented programming, abstraction enables numerous expanded functionalities.

Example:

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

In this case, an interface called IAnimal defines a single method called MakeSound. The MakeSound method is a universal behavior shared by all animals. Then there are two classes, Dog and Cat, that implement the Animal interface and have their own implementation of the MakeSound method.

Finally, in the Main method, we create an array of Animal objects and populate it with a Dog and a Cat object. Then we iterate through the array, calling the MakeSound method on each object. Because the MakeSound method is implemented differently in the Dog and Cat classes, the output of the MakeSound method differs for each object.

b. Encapsulation

Definition: This property does not allow the user to modify an object's intrinsic state. Only the object's internal methods enable it to be changed. Allowing the external world to influence an object's internal data is totally up to the person writing the code. This is a property that ensures an object's integrity. Encapsulation allows a collection of attributes, methods, and other components to be treated as a unified unit or object.

Implementation:

o Public: Access to all code in the program

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

o Private: Access to only members of the same class

o Protected: Access to members of the same class and its derived classes

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

o Internal: Access to the current assembly

Example:

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

In this example, we have a Car class that represents a car. Model and speed are two private fields in the class. These fields are marked as private to prevent unauthorized access.

Instead, we make these fields available to the public via two public properties: Model and Speed. These properties allow other objects to read the car's model and speed but not directly modify them.

We can ensure that the internal details of the Car class are hidden from the outside world by using encapsulation in this way, and that any changes to the car's model and speed are made in a controlled and consistent manner. Therefore, the class more user-friendly and less vulnerable to errors or misapplication.

c. Inheritance

Definition: This feature lets an object inherit the attributes that other objects already have. Objects can now share or enhance accessible features without having to redefine them. This trait, however, is not shared by all object-oriented languages. Multi-level hierarchy: Although a derived class can only have one direct base class, inheritance is transitive. ClassC inherits

the members declared in ClassB and ClassA if it is derived from ClassB, and ClassB is derived from ClassA. Example:

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

In this example, the parent class is Animal, and the child class is Cat, which inherits from Animal. The Cat class inherits the Animal class's Name and Age attributes, and it overrides the MakeSound() function to offer its own implementation. When we create a new Cat object in the main application, we can access its inherited Name and Age attributes, as well as execute the MakeSound() function to generate Cat-specific output. It's worth noting that the MakeSound() function in the Animal class is declared as virtual, allowing it to be overridden by child classes such as Cat.

Protected Access Modifier:

o The protected access modifier protects the data members that are declared using this modifier. o The protected access modifier is specified using the protected keyword.

o Variables or methods that are declared as protected are accessed only by the class in which they are declared or by a class that is derived from this class.

Base keyword: The base keyword allows you to do the following:

o Access variables and methods of the base class from the derived class. o Re-declare the methods and methods defined in the base class. o Invoke the derived class data members.

o Access the base data using the "base" keyword.

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

d. Polymorphism

Another fundamental concept in object-oriented programming (OOP) is polymorphism, which refers to an object's ability to take on multiple forms. Polymorphism in C# is accomplished through inheritance, interfaces, and method overloading.

Below is an example in C#:

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

We have an abstract Animal class with an abstract MakeSound method in this example. The MakeSound method is marked as abstract to indicate that any derived class must implement it.

Dog and Cat are derived classes that override the MakeSound method to produce different sounds.

Finally, in the Main method, we create an array of Animal objects and populate it with a Dog and a Cat object. Then we iterate through the array, calling the MakeSound method on each object. Because the MakeSound method is implemented differently in the Dog and Cat classes, the output of the MakeSound method differs for each object.

This is an example of polymorphism in action: the same method (MakeSound) is called on multiple objects (Dog and Cat), but the results vary depending on the object.

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

3. Class relationships

a. Association relationship

Definition: Association is one of the fundamental connections in object-oriented programming (OOP) and is used to describe a link or relationship between two or more classes. In an association connection, one class is associated to another class in some manner. This relationship can be one- -one, one- -many, many- -one, or many- -many. to to to to Example:

</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">

In this example, we'll make a new Teacher object called johnDoe, as well as two new Course objects called mathCourse and scienceCourse. By setting the instructor attribute of each Course object to johnDoe, we assign both courses to the johnDoe instructor. We then use the Add method of the List<Course> class to add both courses to the Courses field of the johnDoe instructor object.

Finally, we loop through the johnDoe teacher object's Courses collection, displaying the title of each course as well as the name of the instructor who teaches it.

b. Unary relationship

In object-oriented programming, a unary association is a sort of relationship between two objects in which one object has a unique link with another. It is a one- -one relationship between two items in which one is dependent on the other for its existence toor operation.

Example:

</div><span class="text_page_counter">Trang 35</span><div class="page_container" data-page="35">

The Owner class and the Car class have a unary relationship in this case. The Owner class contains a CarOwned member variable of type Car that represents the owner's car. The CarOwned reference allows the Owner class to access and use the Car class's properties and methods.

We create an instance of the Car class representing a car object in the Main function and send it to the Owner class constructor when generating an instance of the Owner class. This establishes a connection between the owner and the vehicle. The CarOwned reference within the Owner class then allows us to access the car's attributes (model and year).

c. Binary relationship

In object-oriented programming, a binary relationship is a relationship between two classes where one class has a reference to the other class. This relationship is typically expressed through a property or field in one class that holds an instance of the other class. This allows the two classes to interact with each other and exchange information.

Binary relationships can be one- -one, one- -many, or many- -many. In a one- -one relationship, each instance of one class to to to tois associated with exactly one instance of the other class. In a one- -many relationship, each instance of one class is associated towith one or more instances of the other class. In a many- -many relationship, each instance of one class is associated with one toor more instances of the other class, and vice versa.

Binary relationships are commonly used in software development to model real-world relationships between objects. For example, in a customer order system, an order may be associated with a single customer, and a customer may have multiple orders. This can be modeled using a binary relationship between the Order class and the Customer class, where the Order class has a reference to the Customer class.

</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">

In this example, there is a binary relationship between the Order class and the Customer class. An order belongs to one customer and a customer can have multiple orders. This is a one- -many relationship. to

In the main method, we create two Customer instances and three Ord instances and link them together to create the relationship erbetween customers and orders. We then print out the orders for each customer.

d. Aggregation relationship

In object-oriented programming, aggregation is a type of binary relationship between two classes where one class contains a reference to another class, but the contained class can exist independently of the containing class. This means that the contained class may belong to other objects or may exist on its own.

example of a class that has an aggregation relationship with another class in C#:

</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43">

To represent this in a UML diagram, we can use an aggregation relationship with a hollow diamond symbol on the Library class end of the relationship:

In this example, the Library class has an aggregation relationship with the Book class. The Library class has a list of Book objects as a property, represented by the Books property. The Book objects can exist independently of the Library class and may be shared by multiple Library objects.

In the main method, we create three Book objects and add them to a List<Book> object, which is then set as the value of the Books property of a Library object. We then print out the details of each book in the library using a foreach loop.

e. Composition relationship

In object-oriented programming, composition is a type of binary relationship between two classes where one class is composed of one or more instances of another class. Unlike aggregation, the contained class cannot exist independently of the containing class and is considered part of the containing class.

Here is an example in C#:

</div><span class="text_page_counter">Trang 47</span><div class="page_container" data-page="47">

In this example, we have two classes: Engine and Car. Engine has a method Start() that starts the engine, and Car has a private member variable engine of type Engine.

By creating an instance of Engine inside Car, we are using composition to create a "has-a" relationship between Car and Engine. This means that a Car object has an Engine object as a member variable, and can call the Start() method of the Engine object to start the engine.

In the Car class's own Start() method, it calls engine.Start() start the engine, and then has its own code to start the car. This todemonstrates how composition allows us to create complex objects by combining simpler objects, and reuse code across multiple classes.

</div><span class="text_page_counter">Trang 49</span><div class="page_container" data-page="49">

III. OOP scenario (P2)

</div><span class="text_page_counter">Trang 51</span><div class="page_container" data-page="51">

2. Use-case diagram

</div><span class="text_page_counter">Trang 53</span><div class="page_container" data-page="53">

According to the UCD, there are two categories of customers: those who are new to the app (unregistered users) and those who have registered and used it for a certain amount of time.

Unregistered users can only register, read music details, listen to a sample of the music, and do a restricted number of song searches.

Registered users may manage their profiles, download songs, place preorders, and place orders.

There is also a system administrator who is directly in control of all system operations. They will be in charge of maintaining client transaction data, updating customer information, and approving content before it is submitted to the system.

</div>

×