Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Programmed Title : Diploma in Computing

Learner’s name : Min Thukha Kyaw

Learner’s ID No : RF37178

Unit No :1

Unit Title : Programming

Assignment No : 2 of 3

Assignment Title : Three programming paradigms and analysis of IDE

Date of submission : 16 .9 .2023

1
Table of Contents
1. Three types of programming paradigms..................................................................................3
1.1 Procedural programming........................................................................................................3
1.1.1 Key Features of Procedural Programming......................................................................3
1.2 Object-oriented programming (OOP)....................................................................................3
1.3 Event-driven programming....................................................................................................4
2. Characteristics of procedural, object-orientated, and event-driven programming...................4
2.1 Characteristics of Procedural Programming..........................................................................4
2.2 Characteristics of Object-Orientated Programming..........................................................5
2.3 Characteristics of Event-Driven Programming.................................................................6
3. Relationship between Programming paradigms.......................................................................7
4. Analysis of suitable IDEs for three programming paradigms..................................................8
4.1 IDE.........................................................................................................................................8
5. Evaluate the source code of an application that implements the programming paradigms.....9
5.1 Using Procedural Programming.............................................................................................9
5.2 Use Object-oriented Programming (OOP)...........................................................................13
5.3 Using Event-Driven Programming......................................................................................17
References......................................................................................................................................20

2
1. Three types of programming paradigms
Programming paradigms are methods of thinking about the development of software. It
alludes to a way of programming rather than a specific language. Writing in some paradigms
is simple in some languages, but not in others.
A programming paradigm is a fundamental approach or technique that specifies how to
organize and set up your code to address issues.

1.1 Procedural programming

Procedural Programming may be the first programming paradigm that a new developer
will learn. Fundamentally, the procedural code is the one that directly instructs a device on
how to finish a task in logical steps. This paradigm connects data and methods as two distinct
things and follows a linear top-down methodology. Based on the concept of a procedure call,
procedural programming divides the program into procedures, which are sometimes referred
to as routines or functions and simply consist of a set of activities to be performed. (Bhatia,
2022)

1.1.1 Key Features of Procedural Programming

Predefined functions: A predefined function is often a name-based command. Higher-


level programming languages typically have predefined functions; however, these functions
are typically taken from libraries or registries rather than programs. 'charAt()', which looks
for a character position in a string, is an illustration of a pre-defined function. (Bhatia, 2022)

Local Variable: A local variable is a variable that is defined in the method's primary
structure and is only used inside the method's local scope. If the local variable is used outside
the method it is declared in, the code will stop working. The local variable can only be used
in the method it is defined in. (Bhatia, 2022)

Global Variable: A global variable is one that is declared outside of every other function in
the code that has been written. Because of this, global variables, as compared to local
variables, can be utilized in any function. (Bhatia, 2022)

1.2 Object-oriented programming (OOP)

The principles of classes and objects are the foundation of the computer science
programming paradigm known as object-oriented programming (OOP). In order to produce
unique instances of things, it is used to organize a software program into straightforward,
reusable code blueprints known as classes. Python, Java, C++, and Java are just a few of the
numerous object-oriented programming languages. (Doherty, 2023)
An OOP program will make use of a set of objects that are capable of carrying out certain
tasks and interacting with other application components. An object may be a person, for
3
instance. That person would know how to walk (which would be a method) and have a name
(which would be a property of the object). A method in OOP is comparable to a procedure in
PP, however in this case it is particular to an object. Classes are another significant
component of OOP. A class may be thought of as an object's blueprint. (Lili Quaknin
Felsen,2017)

1.3 Event-driven programming

The flow of a program is decided by events that take place while it is being executed in
event-driven programming. An event-driven software waits for particular events to happen
and then begins matching event handlers or callbacks to react to those events, as opposed to
doing actions in a straight line. (Miladev95, 2022)

The term "event" refers to a variety of signals, actions, or occurrences, including user
interactions (such as button pushes, mouse movements, and key presses), system events
(such as timers, file input/output, and network connection), and custom events created by the
program. (Miladev95, 2022)

Key concepts of event-driven programming:

Event: An event is a signal or notification that something has happened. Events can be
triggered by user actions, system actions, or other parts of the program. (Miladev95, 2022)

Event Handler: An event handler, often known as a callback, is a section of code that runs
in response to a particular event. The corresponding event handler is called to respond to an
event when it happens. (Miladev95, 2022)

Event Loop: The key component of event-driven programming is the event loop. It
continually scans the program's event queue for fresh events, processing them in the order
they occur. The appropriate event handler will be called when an event is discovered.
(Miladev95, 2022)

Asynchronous: Event-driven programming makes use of asynchronous actions. The


software can carry on processing other events or activities until the event is ready to be
handled rather than halting execution while waiting for an event to happen. (Miladev95,
2022)

2. Characteristics of procedural, object-orientated, and event-driven programming

2.1 Characteristics of Procedural Programming

Key Characteristics of Procedural Programming:

4
Procedure-Centric: The primary objective of procedural programming is on certain actions
or processes. A program is frequently organized as a set of processes, each of which is
responsible for performing a particular function.

Sequential Execution: The program's control flow happens in a certain order. The order in
which procedures are called affects how statements are performed and how the program
behaves.

Modularity: Procedural programming promotes code modularity. Procedures can be seen as


self-contained, reusable units of code. This modular approach can simplify program
development and maintenance.
Control Structures: Procedural programming relies on control structures like loops (for,
while) and conditionals (if, else) to control the flow of execution. These structures are
essential for making decisions and repeating actions.

Algorithmic components of issue solution: Procedural programming frequently highlights


the algorithmic components of issue solution. Developers concentrate on decomposing an
issue into more manageable steps and putting the required algorithms into place.

Separation of Data and Processes: Data and processes are frequently not fully integrated.
Data consistency and data privacy can be difficult to preserve since data is distinct from the
processes that alter it.

2.2 Characteristics of Object-Orientated Programming

Characteristics of Object-Orientated Programming:

Object: An object is an object having certain characteristics and actions. The memory is
allocated at that point for the purpose of creating an object. Each object may communicate
with one another without needing to understand the specifics of their respective codes or
information.
For example, a Dog is the object of the breed name Pomeranian, having its properties and
behaviors. (codingninjas, 2023)

Class: Classes are made up of a collection of objects with similar data component
characteristics. A class is an object's blueprint, including all of its properties, methods, and
other features. Data binding and code reuse are both aided by classes.
For example, a Dog has attributes like species, color, etc., but the attributes of the class Dog
are the same for the different breeds and colors. (codingninjas, 2023)

Encapsulation: In oops, encapsulation refers to keeping all the crucial information in a


sealed container while making the rest of the information available to the public. It's crucial
to protect the data from being altered in the database by intruders.
5
For example, if you request the ATM machine for money, the system defines your request
and hands you the cash. It is a single machine that houses both data and functions.
(codingninjas, 2023)

Abstraction: For a better user experience, a developer must include abstraction into the
software or product. Abstraction basically involves hiding all the intricacies and showing the
outer world only the most important data.
Abstraction is a key component of object-oriented programming because it shields users from
complicated details while allowing them the freedom to modify systems.
Abstraction makes it possible to add functionality without impacting others, and big code
bases lead to great complexity. (codingninjas, 2023)

Inheritance: Inheritance means inheriting someone's characteristics. Just like you inherit
features and habits from your parents, it is possible to inherit the data and methods from one
class to another. Combining and inheriting the objects and their properties is one of the most
essential and important characteristics of oops. (codingninjas, 2023)
Super Class: The class from which the properties are inherited by the subclass/child class.
Sub Class: The class which inherits the properties from the superclass/parent class.

2.3 Characteristics of Event-Driven Programming

Key characteristics of Event-Driven programming:

Event: An event is an activity that can elicit a response. Events can include mouse clicks,
keystrokes, sensor data input, messages provided by other applications, and more.
(sat.jan, 2023)

Event Handler: The block of code that is meant to be executed in response to an event is
known as the event handler. If you want a message to appear when a button is pressed, the
event handler is the code that will cause the message to appear. (sat.jan, 2023)
Event Loop: The event loop is a queue data structure that keeps track of all events as well as
any pertinent information (such as the x and y coordinates of a mouse click). The first job
added to the list will be the first work taken off and processed since messages are removed
from this queue in the order that they are received. The event loop is an operating-system-
supplied piece of code that loops around, checking the message queue, and processing messages
when they arrive. (sat.jan, 2023)

Service-Oriented Processing: Services that are running in the background of a system and
are activated by events are referred to as service-oriented processing. The simplest simple
example to grasp is the USB device identification service on a modern PC.. An event is
triggered and sent to the software service when a USB device is attached or deleted, enabling
interactivity to decide, among other things, what to do with the new item.

6
Points to clarify this:
Regularly scanning USB devices serves no purpose. Only when the associated devices
change you need to be aware.
Software applications without a user interface that runs in the background are referred to
as services. They carry out auxiliary duties. (sat.jan, 2023)
Time Driven: The system is time-driven if it is aware of the passing of time. The best way to
understand this is with an example, like an alarm clock.
A clock that functions as an alarm must periodically check to see if the time is a specific
value, such as once per second. We have time-driven code that must execute once per
second. For this, a timer that generates an event (e.g., TimeElapsed) that we may respond to
can be used.

3. Relationship between Programming paradigms

Relationship between procedural, object-oriented, and event-driven programming


Procedural programming is a type of programming language that deals with function only.
Object-oriented programming is the latest version of procedural programming was created to
get over POP's drawbacks. The primary distinction between both is that OOP divides
programs into several objects, whereas POP breaks down programs into numerous functions
to handle problems. (Anon., 2022)
Object programming language is the software structure in which objects are specified,
and event-driven programming is the computer programming in which the flow of the
program is governed by events like user actions (mouse clicks, key presses, messages from
the program, etc.). The connection between object-oriented programming and event-driven
programming Because they are orthogonal, they may be used together and share the same
operating ideas, such as encapsulation, polymorphism, and inheritance. (Anon., 2022)
The procedural paradigm is the way you consider the steps needed to solve an issue. I
perform a, b, and c in that order. There is a clear mechanism in place, and the data flow is
quite predictable. (Anon., 2022)
The actors are taken into account and represented as objects in object-oriented design.
For instance, a queue model for a movie theater. People, a queue, and a ticket counter are all
present. You thus construct objects around those actors. (Anon., 2022)
When there are unplanned situations that need to be handled, event-driven is employed.
Both procedural and object-oriented paradigms can add event handlers. Therefore, taken
alone, it isn't a whole paradigm. For example, your procedural report writer could need to
address a situation involving running out of paper. Or the object-oriented application could
need to handle an event when lightning strikes someone waiting in line. (Anon., 2022)

7
When you define a program and its subprograms as a series of steps, you are using
procedural programming.
The ideas of encapsulation, inheritance, substitution, programming to interfaces, and
other concepts are the foundation of object-oriented programming, which is a method of
structuring code. The majority of object-oriented applications are typically procedural.
Writing event-handling code and relying on the underlying system to supply the main
event loop are both aspects of event-based programming. Though not usually, writing event-
based applications in an object-oriented method is quite common.
A procedural is a series of actions that take place sequentially from beginning to end. An
object's capabilities are defined by its object-oriented design, and other objects can inherit
those capabilities to create new things that incorporate the original objects. When something
is event-driven, you specify what should happen when it does, and the program only runs IF
and WHEN such an event takes place. (Quora, 2019 April)

4. Analysis of suitable IDEs for three programming paradigms

4.1 IDE

All the tools needed for a software development project are gathered in one place by a
piece of software called an IDE, or integrated development environment. IDEs provide
interfaces that let users organize text groups, write code, and automate repetitive
programming tasks at a more fundamental level. But in contrast to a simple code editor, IDEs
integrate the capabilities of several programming processes into one. (Anon., 2022)
Dev C++ provides features including integrated debugging, CVS support, project
management, syntax highlighting, and auto code completion. For your small projects on the
Windows platform, you may utilize Dev C++, which is based on the GNU Compiler
Collection (GCC). Other capabilities include customized compiler, environment, and editor
customizations, as well as compiling and debugging. (Madhuparna, 2019 Jan)
It is suitable for procedural programming and object-oriented programming. Debugging,
code inspections, and code help are among Eclipse's primary features. User interface library
functions are required for event-driven programming. Without help from the code, getting
them is difficult. Therefore, the eclipse IDE is appropriate for event-driven programming.
Additionally, it has a base workspace with a flexible plug-in system so you may adjust the
IDE to your needs. You may create programs in different programming languages using a
plug-in. ( Alex Marshall, 2015 March)

8
5.
Evaluate the source code of an application that implements the programming paradigms

Developing function calculator application

5.1 Using Procedural Programming

#include<iostream>
using namespace std;
void menu();
void add();
void sub();
void multi();
void div();
void exit();
int getChoice();
int main(){
bool loop=true;
while (loop){
menu();
switch(getChoice()){
case 1: add(); break;
case 2: sub(); break;
9
case 3: multi(); break;
case 4: div(); break;
case 5: exit(); loop=false; break;
}
}
}
//function implementations
void menu(){

cout<<" MENU "<<endl;


cout<<"................"<<endl;
cout<<"[1] Add Numbers "<<endl;
cout<<"[2] Subtract Numbers "<<endl;
cout<<"[3] Multiply Numbers "<<endl;
cout<<"[4] Divide Numbers "<<endl;
cout<<"......................"<<endl;
cout<<"[5] Exit Program "<<endl;
cout<<"..................."<<endl;
}
int getChoice(){
int choice;
cout<<" Enter choice:";
cin>>choice;
return choice;
}
int getNum(){
int num;
10
cout<<" Enter a number:";
cin>>num;
return num;
}
int getAddResult(int num1,int num2) {
return num1+num2;
}
int getSubResult(int num1,int num2){
return num1-num2;
}
int getMultiResult(int num1,int num2){
return num1*num2;
}
float getDivResult (float num1,float num2){
return num1/num2;
}
void display(float ans){

cout<<" The answer is ="<<ans<<endl;


}
void add(){
cout<<" Add Numbers "<<endl;

int num1=getNum();
int num2=getNum();
int ans=getAddResult(num1,num2);
display(ans);
11
}
void sub(){
cout<<" Subtract Numbers "<<endl;

int num1=getNum();
int num2=getNum();
int ans=getSubResult(num1,num2);
display(ans);
}
void multi(){
cout<<" Multiply Numbers "<<endl;

int num1=getNum();
int num2=getNum();
int ans=getMultiResult (num1,num2);
display(ans);
}
void div(){
cout<<" Divide Numbers "<<endl;
float num1=getNum();
float num2=getNum();
float ans=getDivResult (num1,num2);
display(ans);

}
void exit(){
12
cout<<" Exit the Program Now.."<<endl;
}
In this example, the program is divided into functions (add and subtract) that perform specific
mathematical operations. The functions are called from the main function, demonstrating the key
principles of procedural programming.

5.2 Use Object-oriented Programming (OOP)

#include<iostream>
using namespace std;
class Calculator {
public:
void menu();
void add();
void sub();
void multi();
void div();
void exit();
int getChoice();

private:
int getNum();
int getAddResult(int num1, int num2);
int getSubResult(int num1, int num2);
int getMultiResult(int num1, int num2);
float getDivResult(float num1, float num2);
void display(float ans);
};
// Function implementations

13
void Calculator::menu() {
cout << " MENU " << endl;
cout << "................" << endl;
cout << "[1] Add Numbers " << endl;
cout << "[2] Subtract Numbers " << endl;
cout << "[3] Multiply Numbers " << endl;
cout << "[4] Divide Numbers " << endl;
cout << "......................" << endl;
cout << "[5] Exit Program " << endl;
cout << "..................." << endl;
}
int Calculator::getChoice() {
int choice;
cout << " Enter choice:";
cin >> choice;
return choice;
}
int Calculator::getNum() {
int num;
cout << " Enter a number:";
cin >> num;
return num;
}

int Calculator::getAddResult(int num1, int num2) {


return num1 + num2;
}
14
int Calculator::getSubResult(int num1, int num2) {
return num1 - num2;
}

int Calculator::getMultiResult(int num1, int num2) {


return num1 * num2;
}
float Calculator::getDivResult(float num1, float num2) {
return num1 / num2;
}
void Calculator::display(float ans) {
cout << " The answer is =" << ans << endl;
}
void Calculator::add() {
cout << " Add Numbers " << endl;
int num1 = getNum();
int num2 = getNum();
int ans = getAddResult(num1, num2);
display(ans);
}

void Calculator::sub() {
cout << " Subtract Numbers " << endl;
int num1 = getNum();
int num2 = getNum();
int ans = getSubResult(num1, num2);
display(ans);
15
}
void Calculator::multi() {
cout << " Multiply Numbers " << endl;
int num1 = getNum();
int num2 = getNum();
int ans = getMultiResult(num1, num2);
display(ans);
}
void Calculator::div() {
cout << " Divide Numbers " << endl;
float num1 = getNum();
float num2 = getNum();
float ans = getDivResult(num1, num2);
display(ans);
}
void Calculator::exit() {
cout << " Exit the Program Now.." << endl;
}
int main() {
Calculator calc;
bool loop = true;
while (loop) {
calc.menu();
switch (calc.getChoice()) {
case 1: calc.add(); break;
case 2: calc.sub(); break;
case 3: calc.multi(); break;
16
case 4: calc.div(); break;
case 5: calc.exit(); loop = false; break;
}
}
return 0;
}

This code demonstrates how OOP principles like encapsulation, abstraction, modularity, and
object creation are used to structure and design a calculator application. OOP allows you to
model real-world entities as objects and define their behavior through methods, leading to
cleaner, more organized, and maintainable code.

5.3 Using Event-Driven Programming

package calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame implements ActionListener {


// Create GUI components
private JTextField inputField;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};

private double num1, num2, result;


private String operator;

public Calculator() {
// Set up the JFrame
setTitle("Calculator");
17
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

// Create the input field


inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.PLAIN, 24));
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setEditable(false);
add(inputField, BorderLayout.NORTH);

// Create buttons and add action listeners


JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
buttons = new JButton[16];

for (int i = 0; i < 16; i++) {


buttons[i] = new JButton(buttonLabels[i]);
buttons[i].setFont(new Font("Arial", Font.PLAIN, 20));
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}

add(buttonPanel, BorderLayout.CENTER);
}

@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

if (command.matches("[0-9]")) {
inputField.setText(inputField.getText() + command);
} else if (command.equals("C")) {
inputField.setText("");
num1 = num2 = result = 0;
operator = null;
} else if (command.matches("[+\\-*/]")) {
if (inputField.getText().isEmpty()) {
inputField.setText("0");
}
num1 = Double.parseDouble(inputField.getText());
operator = command;
inputField.setText("");
} else if (command.equals("=")) {
18
if (operator != null && !inputField.getText().isEmpty()) {
num2 = Double.parseDouble(inputField.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
inputField.setText("Error");
return;
}
break;
}
inputField.setText(String.valueOf(result));
}
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
Calculator calculator = new Calculator();
calculator.setVisible(true);
});
}
}

Event-driven programming in this code is implemented by capturing user interactions (events)


and responding to those events based on the user's choices. The code continuously listens for
user input and reacts accordingly, making it a simple example of an event-driven application.

References

19
Anon., 2022. coursehero. [Online]
Available at: https://www.coursehero.com/file/p4h07uhi/Relationship-between-procedural-
object-oriented-and-event-driven-programming/
[Accessed 2 jul 2023].
Anon., 2022. studocu. [Online]
Available at: https://www.studocu.com/row/document/esoft-metro-campus/hnd-in-computing/
p1-pragramming-asessment-short-note/30499955
Bhatia, S., 2022. https://hackr.io/blog/procedural-programming. [Online]
Available at: https://hackr.io/blog/procedural-programming
[Accessed 13 Dec 2022].
codingninjas, 2023. codingninjas. [Online]
Available at: https://www.codingninjas.com/studio/library/characteristics-of-object-oriented-
programming
[Accessed 23 jul 2023].
Doherty, E., 2023. https://www.educative.io/blog/object-oriented-programming. [Online]
Available at: https://www.educative.io/blog/object-oriented-programming
[Accessed 23 Aug 2023].
Miladev95, 2022. https://medium.com/@miladev95/event-driven-programming-cbd3ed8ec2ca.
[Online]
Available at: https://medium.com/@miladev95/event-driven-programming-cbd3ed8ec2ca
[Accessed 1 jul 2022].
sat.jan, 2023. BTEC Computing. [Online]
Available at: https://bteccomputing.co.uk/event-driven-programming-features/
[Accessed 23 jan 2023].
sat.Jan, 2023. BTEC Computing. [Online]
[Accessed jan 2023].

20
21

You might also like