Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Programming

The document summarizes three programming paradigms: procedural programming, object-oriented programming (OOP), and event-driven programming. It defines each paradigm and discusses their principles, pros, and cons. For procedural programming, it focuses on breaking programs into sequenced instructions and procedures. For OOP, it emphasizes organizing programs around objects that contain data and behaviors. For event-driven programming, it focuses on handling events rather than continuously checking for specific conditions. The document also compares the paradigms and provides examples of source code that implements them.

Uploaded by

Minh Tuấn Ngô
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Programming

The document summarizes three programming paradigms: procedural programming, object-oriented programming (OOP), and event-driven programming. It defines each paradigm and discusses their principles, pros, and cons. For procedural programming, it focuses on breaking programs into sequenced instructions and procedures. For OOP, it emphasizes organizing programs around objects that contain data and behaviors. For event-driven programming, it focuses on handling events rather than continuously checking for specific conditions. The document also compares the paradigms and provides examples of source code that implements them.

Uploaded by

Minh Tuấn Ngô
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PROGRAM TITLE: …….

…Programming……………

UNIT TITLE: …………Unit 1: Programming………….

ASSIGNMENT NUMBER: ……………01………………

ASSIGNMENT NAME: … …….

SUBMISSION DATE:

DATE RECEIVED: …………………………………………….

TUTORIAL LECTURER: ……… …….……

WORD COUNT: …………………3758……………

STUDENT NAME: ……………… …………

STUDENT ID: …………

MOBILE NUMBER: …………………………

1
Summative Feedback:

Internal verification:

2
TABLE OF CONTENTS
I. Overview.........................................................................................................................................4
II. Procedural programming.............................................................................................................4
1. Definition........................................................................................................................................
2. Some principles of procedural programming.................................................................................
3. Pros and Cons of Procedural Programming....................................................................................
III. Object-orientated programming..................................................................................................5
1. Definition........................................................................................................................................
2. Structure of object-oriented programming.....................................................................................
3. Four basics of OOP.........................................................................................................................
4. Pros and Cons of OOP..................................................................................................................
IV. Event-driven programming........................................................................................................14
1. Definition......................................................................................................................................
2. Some principles of event-driven programming............................................................................
3. Pros and Cons...............................................................................................................................
V. The different of the procedural, object-orientated, and event-driven paradigms......................15
1. The procedural, object-orientated, and event-driven paradigms..................................................
2. Procedural Programming and Object-Oriented Programming.....................................................
VI. An analysis of suitable IDEs.....................................................................................................16
1. IDE................................................................................................................................................
2. Some suitable IDEs.......................................................................................................................
VII. Compare the procedural, object-orientated, and event-driven paradigms used in given source
code of an application..........................................................................................................................18
1. Examples.......................................................................................................................................
2. Evaluate the source code of an application which implements the procedural, object-
orientated. 20
VIII. References..............................................................................................................................20

3
I. Overview.
It is quite important to secure and run the software effectively in projects. The investigation
programming paradigms are necessary for the job. The term programming paradigm refers to a style
of programming. In another way, programming paradigms are a way to classify programming
languages based on their features. It does not refer to a specific language, but rather it refers to the
way you program. This report includes research about procedural, object-orientated, and event-
driven programming.

II. Procedural programming.


1. Definition.
Procedural programming is a programming paradigm built around the idea that programs are
sequences of instructions to be executed. They focus heavily on splitting up programs into named sets
of instructions called procedures, analogous to functions.

A procedure can store local data that is not accessible from outside the procedure’s scope and can
also access and modify global data variables.

Procedural Programming.

2. Some principles of procedural programming.


The procedural approach divides a large program (function) into blocks of functions or functions
(procedures) that are small enough to be easy to program and test. Each function has a start and an end

4
point has its data and logic. In a program system, variables have certainly visible scopes. In a program,
functions work independently of each other. Data is converted back and forth through function call
parameters. Dividing a program into functions allows multiple people to participate in the program’s
construction. Each person constructs one or more functions independently of the other. This approach
leads to a new concept – abstraction. Abstraction can be thought of as the ability to observe a thing
without considering its internal details.

In a procedural program, we just need to know what a particular function can do. It doesn’t matter
how it does the job, as long as the function is reliable, you can use it without knowing how it does its
job. This is called functional abstraction (also known as specialization) and is the foundation of
procedural programming.

3. Pros and Cons of Procedural Programming.


 Pros:
 Procedural Programming is excellent for general-purpose programming.
 The coded simplicity along with ease of implementation of compilers and
interpreters.
 A large variety of books and online course material available on tested algorithms,
making it easier to learn along the way.
 Because the source code is portable, it can also be used to target a different CPU.
 It is not necessary to replicate the code because it can be reused in different parts of
the program.
 Through the Procedural Programming technique, the memory requirement also
slashed.
 The program’s flow can be simply followed.
 Cons:
 When Procedural Programming is used, the program code is more difficult to write.
 The Procedural code is often not reusable, which may pose the need to recreate the
code if is needed to use in another application.
 Difficult to relate with real-world objects.
 The importance is given to the operation rather than the data, which might pose in
some data-sensitive cases.
 The data is exposed to the whole program, making it not so much security friendly.

III. Object-orientated programming.


1. Definition.
Object-oriented programming (OOP) is a computer programming paradigm that organizes
software design around data, or objects, rather than functions and logic. An object can be defined as a
data field that has unique attributes and behaviour.

OOP focuses on the objects that developers want to manipulate rather than the logic required to
manipulate them. This approach to programming is well-suited for programs that are large, complex,

5
and actively updated or maintained. This includes programs for manufacturing and design, as well as
mobile applications; for example, OOP can be used for manufacturing system simulation software.

2. Structure of object-oriented programming.


The structure or building blocks of object-oriented programming include the following:

 Classes: are user-defined data types that act as the blueprint for individual objects, attributes, and
methods.

 Objects: are instances of a class created with specifically defined data. Objects can correspond to
real-world objects or an abstract entity. When class is defined initially, the description is the only
object that is defined.

 Methods: are functions that are defined inside a class that describe the behaviors of an object. Each
method contained in class definitions starts with a reference to an instance object. Additionally, the
subroutines contained in an object are called instance methods. Programmers use methods for
reusability or keeping functionality encapsulated inside one object at a time.

 Attributes: are defined in the class template and represent the state of an object. Objects will have
data stored in the attributes field. Class attributes belong to the class itself.

Object Oriented Programming.

6
3. Four basics of OOP.

a. Encapsulation.
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that
binds together code and the data it manipulates. Another way to think about encapsulation is, it is a
protective shield that prevents the data from being accessed by the code outside this shield.

 The variables or data of a class is hidden from any other class and can be accessed only
through any member function of its own class in which it is declared.
 Encapsulation can be achieved by declaring all the variables in the class as private and
writing public methods in the class to set and get the values of variables.
 Some advantages of encapsulation: data hiding, increased flexibility, reusability, and
testing code is easy.

Encapsulation in Java.

 Example:
public class Student {
private String name;
private int khoa;

public Student() {
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getKhoa() {


return khoa;
}

public void setKhoa(int khoa) {


this.khoa = khoa;
}
}

7
public class TestStudent {

public static void main(String[] args) {


Student s1 = new Student();
s1.setName("Pham Ha Ngan");
s1.setKhoa(12);
System.out.println("Ten: " + s1.getName() + "\nKhoa: " + s1.getKhoa());

 Result:
Ten: Pham Ha Ngan
Khoa: 12

b. Abstraction.
Abstraction is the property by virtue of which only the essential details are displayed to the user.
The trivial or the non-essentials units are not displayed to the user.

Data abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object
differentiate it from other objects of similar type and also help in classifying or grouping the objects.


Abstract classes and Abstract methods:
- An abstract class is declared with an abstract keyword while an abstract method
is declared without implementation.
- An abstract class may or may not have all abstract methods. Some of them can
be concrete methods.
- There can be no object of an abstract class. That is, an abstract class cannot be
directly instantiated with the new operator.
 Some advantages of Abstraction:
- It reduces the complexity of viewing the things.
- Avoids code duplication and increases reusability.
- Helps to increase the security of an application of program as only important
details are provided to the user.
 Example:
package Abstraction;

abstract class Shape {


String color;

abstract double area();

public abstract String toString();

public Shape(String color) {


System.out.println("Shape constructor called");
this.color = color;
}

public String getColor() {

8
return color;
}
}

class Rectangle extends Shape {

double length;
double width;

public Rectangle(String color, double length, double width) {


// calling Shape constructor
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}

@Override
double area() {
return length * width;
}

@Override
public String toString() {
return "Rectangle color is " + super.getColor() + " and area is : " + area();
}
}

public class Test {


public static void main(String[] args) {

Shape s2 = new Rectangle("Yellow", 4, 5);

System.out.println(s2.toString());
}
}

 Result:
Shape constructor called
Rectangle constructor called
Rectangle color is Yellow and area is : 20.0

c. Inheritance.
Inheritance is a mechanism in which one object acquires all the properties and behaviours of a
parent object. It is an important part of OOP.

The idea behind inheritance is that you can create new classes that are built upon existing
classes. The inherited class can reuse methods and fields of the parent class.

Terms used in Inheritance:

 Class: is a group of objects which have common properties.


 Sub Class: is a class which inherits the other class.
 Super Class: is the class from where the subclass inherits the features.

9
 Reusability: is a mechanism which facilitates you to reuse the fields and methods of an
existing class when creating a new class.

The keyword used for inheritance is extends.

To reduce the complexity and simplify the language, multiple inheritance is not supported in
Java.

 Example:
public class Father {
private String name;
private int age;

public Father() {
super();
}

public Father(String name, int age) {


super();
this.name = name;
this.age = age;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

public void play() {


System.out.println("Thich choi da bong");
}
}

public class Son extends Father {


public Son() {
super();
}

public Son(String name, int age) {


super(name, age);
}

public void play() {

System.out.println("Thich choi bong ro");


}

public void play(String moTa) {

10
System.out.println(getName() + " " + moTa);
}
}

public class Main {

public static void main(String[] args) {


Son s = new Son("Son", 10);
Father f = new Father("Father", 35);
s.play();
s.play("choi rat hay");
f.play();

}
}

 Result:
Thich choi bong ro
Son choi rat hay
Thich choi da bong

d. Polymorphism.
Polymorphism is the ability of an object to take many forms. To simply put, polymorphism in
Java allows performing the same action in many different ways.


Polymorphism in Java can be performed by two different methods:
- Method Overloading: a process that can create multiple methods of the same name
in the same class, and all the methods work in different ways. Method overloading
occurs when there is more than one method of the same name in the class.
- Method Overriding: a process when the subclass has the same method as declared
in the parent class.
 Some advantages of Polymorphism:
- It provides reusability to the code.
- A single variable can be used to store multiple data values.
- With lesser lines of code, it becomes easier for the programmer to debug code.
 Example:
public class DaGiac {
private int soCanh;
private double kichThuoc[];

public DaGiac(int soCanh) {


this.soCanh = soCanh;
this.kichThuoc = new double[soCanh];
}

public DaGiac(double kichThuoc[]) throws Exception {


if (kichThuoc.length < 3)
throw new Exception("Kich thuoc toi thieu bang 3");
for (double d : kichThuoc) {
if (d <= 0) {
throw new Exception("Kich thuoc canh phai duong");

11
}
}

this.kichThuoc = kichThuoc;
this.soCanh = kichThuoc.length;
}

public int getSoCanh() {


return soCanh;
}

public double[] getKichThuoc() {


return kichThuoc;
}

public double chuVi() {


double t = 0;
for (double d : kichThuoc) {
t += d;
}
return t;
}
}

public class TamGiac extends DaGiac {


public TamGiac(double[] kichThuoc) throws Exception {
super(kichThuoc);
if (kichThuoc.length != 3)
throw new Exception("Tam giac co 3 canh");

private boolean kiemTraTamGiac() {


double a = getKichThuoc()[0];
double b = getKichThuoc()[1];
double c = getKichThuoc()[2];
if (a + b > c && b + c > a && a + c > b)
return true;
return false;
}

public double chuVi() {


if (kiemTraTamGiac())
return super.chuVi();
else
return -1;
}

public double dienTich() {


if (!kiemTraTamGiac())
return -1;
double p = chuVi() / 2;
return (Math.sqrt(p * (p - getKichThuoc()[0]) * (p - getKichThuoc()[1]) * (p
- getKichThuoc()[2])));
}
}

import java.util.Scanner;

public class Main2 {

12
public static void main(String[] args) {
double kt[] = { 3.0, 4.0, 6.0 };
try {
TamGiac t = new TamGiac(kt);
System.out.println("Chu vi: " + t.chuVi() + ", Dien tich: " +
t.dienTich());
} catch (Exception e) {
e.printStackTrace();

}
System.out.println("Nhap so canh: ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double kt2[] = new double[n];
for (int i = 0; i < n; i++) {
System.out.printf("Canh[%d]= ", i);
kt2[i] = in.nextDouble();
}
in.close();
try {
DaGiac daGiac = new DaGiac(kt2);
System.out.println(daGiac.chuVi());
} catch (Exception e) {
e.printStackTrace();
}
}
}

 Result:

Chu vi: 13.0, Dien tich: 5.332682251925386


Nhap so canh:
4
Canh[0]= 2
Canh[1]= 3
Canh[2]= 4
Canh[3]= 5
14.0

4. Pros and Cons of OOP.


 Pros:
 Modularity: Encapsulation enables objects to be self-contained, making troubleshooting
and collaborative development easier.
 Reusability: Code can be reused through inheritance, meaning a team does not have to
write the same code multiple times.
 Productivity: Programmers can construct new programs quicker through the use of
multiple libraries and reusable code.
 Easily upgradeable and scalable.
 Security: Using encapsulation and abstraction, complex code is hidden, software
maintenance is easier and internet protocols are protected.
 Cons:

13
 Object-Oriented programs tend to be slower and use up a high amount of memory.
 Over-generalization.
 Programs built using this paradigm may take longer to be created.

IV. Event-driven programming.


1. Definition.
Event-driven programming is a programming paradigm in which the flow of program execution is
determined by events - for example, a user action such as a mouse click, keypress, or a message from
the operating system or another program. An event-driven application is designed to detect events as
they occur and then deal with them using an appropriate event-handling procedure.

Example of Event-driven programming.

2. Some principles of event-driven programming.


Central to event-driven programming is the stream of data or events. It is intended for push
(reactive) but can be used for pull as well. It is lazy rather than eager and it is usually used
asynchronously. And it can represent 0, 1, many, or infinite values or events over time.

 A set of functions that handle events. Depending on the implementation these can be
blocking or non-blocking.
 A mechanism for binding the registered functions to events.
 The main loop constantly polls for new events and calls the matching event handler when a
registered event is received.

3. Pros and Cons.


 Pros:

14
 Suitability for Graphical Interfaces.
 Simplicity of programming.
 It is one good way to model systems that need to be both asynchronous and reactive.
 It can be implemented using hardware interrupts, which will reduce the power used
by the computer.
 It allows sensors and other hardware to easily interact with software.
 Cons:
 Difficult to find the error.
 Programs with complex Graphical User Interface (GUI) may be slower to load and
run than simpler programs particularly if RAM is insufficient.

V. The different of the procedural, object-orientated, and


event-driven paradigms.
1. The procedural, object-orientated, and event-driven paradigms.
Procedural Programming Object-Oriented Event Driven Programming
Paradigm Programming Paradigm Paradigm

Provides character user interface Provides command writing in Provides graphical user interface
to write the commands. modules. to create the programs.

Commands are written in linear Objects and functions are Actions are defined on events.
fashion and executed also in prepared for interaction to These events could be occurred
linear fashion. perform specific tasks. by mouse clicking and moving
or keyboard strokes.
Focuses on sequential execution Focuses on objects or data and Focuses on selecting user
of steps. facilitate to secure it from interface.
unauthorized access.

Most common languages which Most common languages which Most common languages which
follow this paradigm are Basic, follow this paradigm are follow this paradigm are Visual
Fortran and COBOL. Smalltalk, C++, and JAVA. Basic and C#.

2. Procedural Programming and Object-Oriented Programming.


Procedural Programming, unlike OOP, sheds focus on the steps which will be performed to
complete a task, rather than the interaction between the objects. The tasks are broken down into
subroutines, variables and data structures. At any point in time, these procedures can be called within
the program execution.

15
Procedural Programming Object-Oriented Programming

Program is divided into small parts called Program is divided into small parts called
functions. objects.

Follows top down approach. Follows bottom up approach.

There is no access specifier. Have access specifiers like private, public,


protected…
Less secure. More Secure.

Overloading is not possible. Overloading is possible.

Function is more important than data. Data is more important than function.

VI. An analysis of suitable IDEs.


1. IDE.
An Integrated Development Environment (IDE) is a software suite that consolidates the basic tools
developers need to write and test software. Typically, an IDE contains a code editor, a compiler or
interpreter, and a debugger that the developer accesses through a single graphical user interface. An
IDE may be a standalone application, or it may be included as part of one or more existing and
compatible applications.

An Integrated Development Environment (IDE).

16
2. Some suitable IDEs.
These are some IDEs suitable for different paradigms, devices, and programming languages:

 Eclipse: It functions by having a base workspace and an extensible plugin environment where
we can download and customize the workspace available. It supports debugging and profiling.
It has a drag and drop functionality and allows us to analyze the code using static analysis. If
you are comfortable in eclipse IDE, you can code in any famous language like Python, Java, and
C++. The OS supports for this IDE are Windows, Linux, and macOS.

Screenshot of Eclipse.

 Visual Studio: It has a wide functioning which allows it to develop almost anything. It also
supports IntelliSense coding for C++. It can be used in both Windows and MAC and allows us
to create applications for any platform. It enables us to test apps and correct errors on the go.

Screenshot of Visual Studio Code.

17
 Eclipse and Visual Studio Code are primarily classified as "Integrated Development
Environment" and "Text Editor" tools respectively.
"Does it all", "Integrates with most of tools" and "Easy to use" are the key factors why
developers consider Eclipse; whereas "Powerful Multilanguage IDE", "Fast" and "Front-end
develop out of the box" are the primary reasons why Visual Studio Code is favored.
Visual Studio Code is an open source tool with 78.4K GitHub stars and 10.9K GitHub forks.

 In my opinion, I prefer to use Eclipse for Java programming. The Eclipse IDE is a well-known
open-source framework that was originally released in late 2001 and has been long favored for
Java code development. Although initially tailored to Java, the IDE is highly extensible and has
support for many languages including C, C++, C#, Java, and several others. Its fan
base numbers in the millions and its users are pleased with the code highlighting and refactoring
options, version control system integration, and advanced debugging features. Eclipse is being
continuously developed and updated, with releases currently on a 13-week cycle. With the
concept of open-source at the very heart, it has an exceptionally active community and
worldwide contributors to the project.

VII. Compare the procedural, object-orientated, and event-


driven paradigms used in given source code of an
application.
1. Examples.
 Example: Write a Java program to input length and width. Finding perimeter and area of
rectangle.
 Input:
- Length of rectangle: 5
- Width of rectangle: 4
 Output:
- Perimeter of rectangle: 18.00
- Area of rectangle: 20.00
 This is source code of an application used procedural and object-orientated paradigms:

a. Procedural paradigm.
 Code:
public class HCN {
float chieuDai;
float chieuRong;

public HCN() {

public float chuVi() throws Exception {


if (chieuDai <= 0 || chieuRong <= 0) {
throw new Exception("canh phai la so duong");
}
return (chieuDai + chieuRong) * 2;

18
}

public float dienTich() throws Exception {


if (chieuDai <= 0 || chieuRong <= 0) {
throw new Exception("canh phai la so duong");
}
return (chieuDai * chieuRong);
}

public class Main2 {

public static void main(String[] args) {


try {
HCN h1 = new HCN();
h1.chieuDai = 5;
h1.chieuRong = 4;
System.out.printf("Chu vi h1: %.2f, dien tich %.2f\n",
h1.chuVi(), h1.dienTich());
} catch (Exception e) {
System.out.println("Loi " + e);
}
}

 Result:
Chu vi h1: 18.00, dien tich 20.00

b. Object-orientated paradigm.
 Code:
import java.util.Scanner;

public class HCN {

public static void main(String[] args) {


try {
float a, b;
System.out.println("Nhap do dai 2 canh a, b: ");
Scanner in = new Scanner(System.in);
a = in.nextFloat();
b = in.nextFloat();
in.close();
if (a <= 0 || b <= 0) {
throw new Exception("canh phai la so duong");
}
System.out.printf("Chu vi: %.2f, dien tich: %.2f", (a + b)
* 2, a * b);
} catch (Exception e) {
System.out.println("Loi " + e);
}
}

 Result:

19
Nhap do dai 2 canh a, b:
4
5
Chu vi: 18.00, dien tich: 20.00

2. Evaluate the source code of an application which implements the procedural,


object-orientated.
In procedural programming, the main program is divided into small sections known as functions,
whereas in object-oriented programming, the program is divided into objects. In contrast to procedural
programming, object-oriented programs use a bottom-up approach.

A significant difference lies in the usage of access specifiers in object-oriented programs. Access
specifiers protect data from unauthorized access and are implementations of the data encapsulation
principle mentioned above. Due to the data encapsulation and abstraction principles, programs designed
in object-oriented programming are much more secure and based on the real world.

VIII. References.
 Procedural Programming, Wikipedia, https://en.wikipedia.org/wiki/Procedural_programming
 Procedural Programming, deepsource, https://deepsource.io/glossary/procedural-programming/
 Sagar B. , 2021, Procedural Programming, hackr.io, https://hackr.io/blog/procedural-
programming
 Object-Oriented Programming, Wikipedia, https://en.wikipedia.org/wiki/Object-
oriented_programming
 Alexander S. Gillis, Object-Oriented Programming (OOP), TechTarget,
https://searchapparchitecture.techtarget.com/definition/object-oriented-programming-OOP
 Event-Driven Programming, Wikipedia, https://en.wikipedia.org/wiki/Event-
driven_programming
 What is An Integrated Development Environment (IDE), Veracode,
https://www.veracode.com/security/integrated-development-environment

20

You might also like