CS 231 Data Structures and Algorithms Fall 2018 Event Based Programming Lecture 06 - September 17, 2018 Prof. Zadia Codabux 1
Agenda Event-based Programming Misc. Java Operator Precedence Java Formatting Scanner Class Switch Statement 2
Administrative Give back quiz Take class picture Homework uploaded later today. Wait until Wednesday s lecture to attempt it. 3
Event Based Programming 4
Event-Driven Programming The flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. (Source: Wikipedia) 5
Event-Driven Programming The listener waits for user input events The event is an interaction with a GUI component The listener handle the events by triggering the appropriate user defined callback functions 6
Java GUI Libraries Swing: the main Java GUI library Abstract Windowing Toolkit (AWT): Initial GUI library Swing is built on top of AWT All Swing components begin with J e.g. Jframe, Jbutton, etc. Reference: https://docs.oracle.com/javase/tutorial/uiswing/index.html 7
Components Component: an object having a graphical representation that can be displayed on the screen and that can interact with the user e.g. buttons, checkboxes, and scrollbars, etc. Container: A component that contains other components e.g. panel, box, etc. Window: a top-level container e.g. frame, dialog box, applet, etc. Example: WinButton.java 8
Miscellaneous Java useful for homework and project 9
Operator Precedence Precedence rules can be overridden by explicit parentheses. 10
Type casting and precedence Casting has a HIGHER precedence (priority) when Java evaluates mathematical expressions (+,-,*,/,%,etc.) System.out.println ( (int) (10 / 3.0) * 8.0 ); // 24.0 System.out.println ( (int) ((10 / 3.0) * 8.0) ); // 26 11
Omit Curly Braces int x = 3; if (x == 4) System.out.println(4); else if (x == 5) System.out.println(5); else System.out.println(3); 12
Omit Curly Braces public class TestNoCurlyBraces { public static void main(string[] args) { int grade = 75; Example: TestNoCurlyBraces.java if (grade >= 59) System.out.println("You passed"); else System.out.println("You failed"); System.out.println("Happy Holiday"); } } 13
Java Formatting Style Guide One statement per line Use whitespace public class Test { public static void main(string[] args) {System.out.println("Hello world!");}} not this! Python uses indentation. Java uses curly braces { } (https://google.github.io/styleguide/javaguide.html#s4-formatting) 14
Increment/Decrement Operators ++ Increment operator : increments a value by 1 -- Decrement operator : decrements a value by 1 Example: TestIncDec.java ++i increments and then uses the variable (pre-increment) i++ uses and then increments the variable (post-increment) int x = 5; System.out.println(x++); // x holds its value System.out.println(x); //after above statement, x = 6 System.out.println(++x); //now we adding 1 to new x. x is now 7. System.out.println(++x + ++x + x++); // 8 + 9 + 9 15
Java.util.Scanner Class import java.util.scanner; Example: TestScanner.java public class testscanner { public static void main(string[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter an integer number"); int integer = in.nextint(); System.out.println("You entered " +integer ); in.close(); //prevent resource leakage } } (Explore Methods: https://docs.oracle.com/javase/8/docs/api/java/util/scanner.html) 16
Decision-Making Given the following if statement code, write the equivalent switch statement. int num = 2; if (num == 1) { System.out.println("odd number"); } else if (num == 2) { System.out.println("even number"); } 17
Decision-Making Example: TestSwitch.java Given the following if statement code, write the equivalent switch statement. int num = 2; if (num == 1) { System.out.println("odd number"); } else if (num == 2) { System.out.println("even number"); } int num = 2; String output; switch (num) { case 1: output = "odd number"; break; case 2: output = "even number"; break; default: output = "invalid"; break; } System.out.println(output); 18