Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events CSC 171 FALL 2001 LECTURE 9. History: the ABC 1936-1939 John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the.

Similar presentations


Presentation on theme: "Events CSC 171 FALL 2001 LECTURE 9. History: the ABC 1936-1939 John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the."— Presentation transcript:

1 Events CSC 171 FALL 2001 LECTURE 9

2 History: the ABC 1936-1939 John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the Atanasoff- Berry Computer -- at Iowa State University. A special purpose machine for the solution to sets of linear equations Perhaps the earliest example of an electronic calculator

3 Methods Does this work? public class myClass { public static void main(String args[]){ double x = 3.0; double y = mySquare(x); System.out.println(x+“^2 ==“ + y); } public static int mySquare(int x) { return x * x; } }

4 Methods Does this work? public class myClass { public static void main(String args[]){ int x = 3; int y = mySquare(x); System.out.println(x+“^2 ==“ + y); } public static double mySquare(double x) { return x * x; } }

5 Method overloading Does this work? public class myClass { public static void main(String args[]){ int x = 3; int y = mySquare(x); System.out.println(x+“^2 ==“ + y); double z = 3.0; double k = mySquare(k); System.out.println(z+“^2 ==“ + k); } public static int mySquare(int x) { return x * x; } public static double mySquare(double x) { return x * x; } }

6 Method Overloading Methods of the same name can have different sets of parameters. – In reality the compiler mangles the name (combines it with it’s parameters)

7 Method overloading public class myClass { public static void main(String args[]){ int x = 3; int y = myPlus(x); int z = myPlus(x,y); System.out.println(“z == “ + z); } public static int myPlus(int x) { return x + 1; } public static int myPlus(int x, int y) { return x + y; } }

8 Method overloading Often useful in constructors public class Student { private String studentName; private int studentNumber; public Student(String name; int number){ studentName=name; studentNumber = number; } public Student() { // The generic student studentName=“J Doe”; studentNumber = 999999999; }

9 Events Often, we have applications that begin at the beginning, proceed step by step, and stop at the end Increasingly, however, we have programs that wait around to respond to environmental events. So, we have event handlers

10 “Listening” for “Actions” “Waiting” for “Events” In JAVA we have “actionListeners” ActionListeners listen for “actionEvents”

11 An ActionListener ActionListener is an interface in JAVA In order to implement an actionlistener, we have to write the method to handle the event public void actionPerformed(actionEvent e)

12 Set up import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class appLec extends Applet implements ActionListener{ Label promptLabel; Button doIt; int value = 0 ; String promptString = "Value is == ";

13 initialzation public void init() { promptLabel = new Label(promptString + value); add(promptLabel); doIt = new Button("DO IT!!"); doIt.addActionListener(this); add(doIt); }

14 The actionListener public void actionPerformed(ActionEvent e) { value++; promptLabel.setText(promptString + value); }

15 Distinguishing source import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class appLec2 extends Applet implements ActionListener{ Label promptLabel; Button b1,b2; // two possible sources of events int value = 0 ; String promptString = "Value is == ";

16 initilization public void init() { promptLabel = new Label(promptString + value); add(promptLabel); b1 = new Button("INCREMENT"); b1.addActionListener(this); add(b1); b2 = new Button("DECREMENT"); b2.addActionListener(this); add(b2); }

17 ActionListener public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) { value++;} if (e.getSource() == b2) { value--;} // “else” ok promptLabel.setText(promptString + value); }

18 Events: The soul of animation import java.applet.Applet; public class appLec3 extends Applet { int x = 100, y=100, inc_x = 1, inc_y = 1, size = 30 ; public void paint(Graphics g) { g.fillOval(x,y,size,size); if ((y 251)) inc_y *= -1 ; if ((x 473)) inc_x *= -1 ; x += inc_x; y += inc_y; repaint(); }


Download ppt "Events CSC 171 FALL 2001 LECTURE 9. History: the ABC 1936-1939 John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the."

Similar presentations


Ads by Google