Assignment Report
Assignment Report
Contents
Algorithm ................................................................................................................................................ 2
Object-Oriented Programming................................................................................................... 4
Examples ................................................................................................................................................. 5
Examples ................................................................................................................................................. 9
1
HND-38 Hlaing Bwar Oo
Algorithm
Examples of Algorithm
2
HND-38 Hlaing Bwar Oo
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.
6. In step 6, we tell our computer to take a close look at this email address--
is it really an email address?
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
Object-Oriented Programming
and then you can give those objects properties and you can make them do
certain things.
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:
Now that you have your band, it's time to pick the song:
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.
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
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.
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
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.
Listener Description
Mouse Listener Listens for mouse events (like double-click, right-click, etc.)
Mouse Motion
Listens for mouse motion
Listener
Component Listens for changes to components (like a label being moved, hidden,
Adjustment
Listens for adjustments (like a scroll bar being engaged)
Listener
8
HND-38 Hlaing Bwar Oo
Examples
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
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
12
HND-38 Hlaing Bwar Oo
13