CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Similar documents
Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Loops and Expression Types

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

AP CS Unit 3: Control Structures Notes

H212 Introduction to Software Systems Honors

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Midterm Examination (MTA)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Introduction to Computer Science Unit 2. Notes

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Claremont McKenna College Computer Science

CS111: PROGRAMMING LANGUAGE II

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 3. Selections

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Control Structures: if and while A C S L E C T U R E 4

Array. Array Declaration:

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

King Saud University College of Computer and Information Sciences Computer Science Department

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Question: Total Points: Score:

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Elementary Programming

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Conditionals, Loops, and Style

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Flow of Control. Chapter 3 Part 3 The Switch Statement

Lecture Set 4: More About Methods and More About Operators

Following is the general form of a typical decision making structure found in most of the programming languages:

In this chapter, you will:

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 2 Primitive Data Types and Operations

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Flow Control. Key Notion. Statement Categories. 28-Oct-10

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Data Structure and Programming Languages

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Programming with Java

Building Java Programs

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Iteration statements - Loops

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Prof. Navrati Saxena TA: Rochak Sachan

Values in 2 s Complement

Lecture 3 Operators MIT AITI

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CMPT 125: Lecture 4 Conditionals and Loops

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CS 231 Data Structures and Algorithms Fall 2018

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Repetition, Looping. While Loop

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Building Java Programs

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Question: Total Points: Score:

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

Arithmetic Compound Assignment Operators

4 WORKING WITH DATA TYPES AND OPERATIONS

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS141 Programming Assignment #6

CompSci 125 Lecture 11

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Full file at

Oct Decision Structures cont d

CS 106 Introduction to Computer Science I

CS 161: Object Oriented Problem Solving

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

DM503 Programming B. Peter Schneider-Kamp.

Java Bytecode (binary file)

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

St. Edmund Preparatory High School Brooklyn, NY

Some Sample AP Computer Science A Questions - Solutions

++x vs. x++ We will use these notations very often.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

JAVA OPERATORS GENERAL

CS111: PROGRAMMING LANGUAGE II

Course Outline. Introduction to java

Important Java terminology

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

CS 335 Lecture 02 Java Programming

Transcription:

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