0% found this document useful (0 votes)
66 views

Assignment Report

The document discusses different types of programming paradigms including procedural programming, object-oriented programming, and event-driven programming. It provides examples of algorithms and how they work. Key terms related to event-driven programming in Java like event object, event source, and event listener are defined. Examples are given to illustrate the differences between procedural and object-oriented programming approaches.

Uploaded by

Hlaing Bwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Assignment Report

The document discusses different types of programming paradigms including procedural programming, object-oriented programming, and event-driven programming. It provides examples of algorithms and how they work. Key terms related to event-driven programming in Java like event object, event source, and event listener are defined. Examples are given to illustrate the differences between procedural and object-oriented programming approaches.

Uploaded by

Hlaing Bwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

HND-38 Hlaing Bwar Oo

Contents
Algorithm ................................................................................................................................................ 2

Examples of Algorithm .................................................................................................................... 2

Procedural Programming .............................................................................................................. 4

Object-Oriented Programming................................................................................................... 4

Examples ................................................................................................................................................. 5

Event-Driven Programming ......................................................................................................... 6

Examples ................................................................................................................................................. 9

1
HND-38 Hlaing Bwar Oo

Algorithm

Algorithms are sets of step-by-step instructions for the computer to follow.


They are at the heart of all computer programs.

You can think of an algorithm as similar to a food recipe. If you make a


sandwich, you follow a set of steps to put the different ingredients together.
You bring ingredients together, assemble them as you like, and produce a final
product - the sandwich. If you were asked to write down instructions to make
a sandwich, you could create a written algorithm.

Within your sandwich algorithm you would need to specify:

• inputs - ingredients and quantities

• the process - recipe or method

• output - what the finished sandwich will be like

Input Process Output

Examples of Algorithm

An algorithm can be written as a list of steps using text or as a picture with


shapes and arrows called a flowchart.And here I will be explaining the
flowchart from step by steps.

2
HND-38 Hlaing Bwar Oo

1. Step 1 is really just a reminder that this is a procedure with a beginning


and an end.

2. In step 2, we make a place in the computer to store what the user types in,
also called a variable

3. In step 3, we clear this variable because we might need to use it again and
don't want the old contents mixed in with the new.

4. In step 4, we prompt the user for an email address

5. In step 5, we stick it in our nifty variable.

6. In step 6, we tell our computer to take a close look at this email address--
is it really an email address?

7. In step 7, we make a decision; if we got a valid email address, proceed to


step 8 (the End), and if not, well, we'd better go back and get one that is!

8. Step 8 - End

3
HND-38 Hlaing Bwar Oo

As you can see, if the email address is invalid, we jump back to step 3, clear
the old one out and stash the new one there.

Procedural Programming

Procedural programming uses a list of instructions to tell the computer what


to do step-by-step. Procedural programming relies on - you guessed it -
procedures, also known as routines or subroutines. A procedure contains a
series of computational steps to be carried out. Procedural programming is
also referred to as imperative programming. Procedural programming
languages are also known as top-down languages.

Object-Oriented Programming

Object-oriented programming, or OOP, is an approach to problem-solving


where all computations are carried out using objects. An object is a
component of a program that knows how to perform certain actions and how
to interact with other elements of the program. Objects are the basic units of
object-oriented programming. A simple example of an object would be a
person. Logically, you would expect a person to have a name. This would be
considered a property of the person. You would also expect a person to be
able to do something, such as walking. This would be considered a method of
the person.

A method in object-oriented programming is like a procedure in procedural


programming. The key difference here is that the method is part of an object.
In object-oriented programming, you organize your code by creating objects,
4
HND-38 Hlaing Bwar Oo

and then you can give those objects properties and you can make them do
certain things.

Examples

One of the most important characteristics of procedural programming is that


it relies on procedures that operate on data - these are two separate concepts.
In object-oriented programming, these two concepts are bundled into objects.
This makes it possible to create more complicated behavior with less code.
The use of objects also makes it possible to reuse code. Once you have created
an object with more complex behavior, you can use it anywhere in your code.

Here,I will set some keys as an examples;

You want to write a program that plays a song. Your band playing the song
will have four members, and you start off by giving each of them a name:

myband = ('Aung Aung,' 'Mg Mg,' 'Htun Htun,' 'Kaung Kaung')

Now that you have your band, it's time to pick the song:

mysong = 'Yellow Submarine'

Now you want your band to play the song, so you need two more things. First,
you need to have the lyrics and the notes of the song. Second, each band
member needs to know what to do: perform vocals, play a particular
instrument or both. We'll just consider the first requirement here.

In procedural programming, you will need to include all the text and notes in
the program so the band members can play it. However, you want to have
another band play the song in another program. In procedural programming,

5
HND-38 Hlaing Bwar Oo

you could copy and paste the code into another program so there is no need
to manually type the same text again. However, why not save the text into a
separate file and every time your band - or any band for that matter - wants
to play that song, you simply call up the file. That way, your code for the song
itself never needs to get duplicated.

This is exactly what object-oriented programming does. In technical terms,


you would use a class called 'song' to create an object, 'Yellow Submarine.'
This object has a number of properties, including lyrics and music notes. The
class describes how to organize lyrics and music notes so it can be read by
your band members. Other songs will have a similar organization, but with
different lyrics and music notes. When you write your program for your band
to play a song, you would call the object 'Yellow Submarine' so your band
members have access to the lyrics and music notes.

In object-oriented programming, you still have to write out the lyrics and
music notes of the song, but only once, and then any band can use the lyrics
and music at any time. From now on, every other program can call the same
object and play the song. And, other songs will be organized in a similar
manner, so one band can play any song without having to change your code
very much.

Event-Driven Programming

When you perform an action on a graphical component you generate an event.


In event-driven programming the program responds to events. The program
responds to events that the user generates in interacting with GUI
components. The order of events is controlled by the user.

6
HND-38 Hlaing Bwar Oo

For example, you can generate an event by clicking on this button: Or you
could click on this button: Click on the buttons a few more times in any
order. The program (your Web browser, in this case) responds to whatever
events you generate. Whenever you take action on a component,
an event occurs. Whether anything happens after that event is up to you, the
programmer, to determine. If you have actions defined for these events, then
you have created an event-driven program or application.

These are the terms when we use Java application GUI;

Event Object

Everything in Java is object-oriented. It stands to reason then, that even


events are tied to objects. Basically, the event itself is an object; thus, we
refer to this general concept as the event object.

Event Source

Knowing that an event object exists doesn't help unless you know what the
element firing the event object is. For example, is it a button, a checkbox, or
a drop-down menu.The event source is the object that is triggered in the
event.

Event Listener

7
HND-38 Hlaing Bwar Oo

In Java GUI applications, we need to specifically identify components that


should react to events. A button, for example, is just a button unless you tell
Java that you want to actually do something when it's clicked. In other words,
we need to make sure the button listens for an event.

The technical term for this is an Event Listener. It's basically program code
that listens for changes, additions, user interaction, etc. When one of these
actions is performed, it then does something based on that event.

There are serval event listeners in java applications;

Listener Description

ActionListener Responds to actions (like a button click or text field change)

Item Listener Listens for individual item changes (like a checkbox)

Key Listener Listens for keyboard interaction

Mouse Listener Listens for mouse events (like double-click, right-click, etc.)

Mouse Motion
Listens for mouse motion
Listener

Window Listener Acts on events in the window

Container Listener Listens for container (like JFrame or JPanel) events

Component Listens for changes to components (like a label being moved, hidden,

Listener shown, or resized in the program)

Adjustment
Listens for adjustments (like a scroll bar being engaged)
Listener

8
HND-38 Hlaing Bwar Oo

Examples

Here are some examples for event listener;

Action Listener

1. package eventdriven;
2. import java.awt.event.*;
3. import javax.swing.*;
4. public class EventDriven {
5. //Frames, panels and header text
6. private JFrame f;
7. private JLabel header;
8. private JLabel msg;
9. private JPanel statusPanel;
10. public EventDriven(){
11. prepareGUI();
12. }
13. public static void main(String[] args){
14. EventDriven swingControlDemo = new EventDriven();
15. swingControlDemo.showEventDemo();
16. }
17. private void prepareGUI(){
18. f = new JFrame("The Twilight Zone");
19. f.setSize(400,400);
20. f.setLocation(625, 50);
21. f.setLayout(new FlowLayout());
22. header = new JLabel("",JLabel.CENTER );
23. msg = new JLabel("",JLabel.CENTER);
24. msg.setSize(350,100);
25. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

9
HND-38 Hlaing Bwar Oo

26. statusPanel = new JPanel();


27. statusPanel.setLayout(new FlowLayout());
28. f.add(header);
29. f.add(statusPanel);
30. f.add(msg);
31. f.setVisible(true);
32. }
33. private void showEventDemo(){
34. header.setText("... Not only of sight, but of mind...");
35. JLabel lblUserName = new JLabel("Enter User Name");
36. final JTextField txtUserName = new JTextField();
37. txtUserName.setColumns(14);
38. JButton submitButton = new JButton("Submit");
39. submitButton.setActionCommand("Submit");
40. //add the action listener here and display user name
41. submitButton.addActionListener(new ActionListener() {
42. public void actionPerformed(ActionEvent e) {
43. String userName = txtUserName.getText();
44. msg.setText("User Name = " + userName);
45. }
46. });
47. statusPanel.add(lblUserName);
48. statusPanel.add(txtUserName);
49. statusPanel.add(submitButton);
50. f.setVisible(true);

And this is the output of ActionListener;

10
HND-38 Hlaing Bwar Oo

Item Listener

1. import java.awt.event.*;
2. import javax.swing.*;
3. public class ItemListenerDemo implements ItemListener {
4. JCheckBox pan, spatula;
5. JLabel label;
6. ItemListenerDemo() {
7. JFrame f = new JFrame("Spatula City!");
8. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9. label = new JLabel("Pick an Option");
10. label.setSize(400, 100);
11. label.setLocation(100, 5);
12. pan = new JCheckBox("Pan");
13. pan.setBounds(100,100, 150,50);
14. spatula = new JCheckBox("Spatula");
15. spatula.setBounds(100,150, 150,50);

11
HND-38 Hlaing Bwar Oo

16. f.add(pan); f.add(spatula); f.add(label);


17. pan.addItemListener(this);
18. spatula.addItemListener(this);
19. f.setSize(400,400);
20. f.setLayout(null);
21. f.setVisible(true);
22. }
23. public void itemStateChanged(ItemEvent e) {
24. if(e.getSource() == pan) {
25. label.setText("You picked a Pan");
26. }
27. if(e.getSource() == spatula) {
28. label.setText("That is the Right Choice!");
29. }
30. }
31. public static void main(String[] args) {
32. new ItemListenerDemo();

And here,this is the output of item listener;

12
HND-38 Hlaing Bwar Oo

13

You might also like