Visual Programming. Lecture 2: More types, Methods, Conditionals

Similar documents
More types, Methods, Conditionals. ARCS Lab.

Research Group. 2: More types, Methods, Conditionals

Data Structure and Programming Languages

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Data Structure and Programming Languages

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

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

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

JAVA OPERATORS GENERAL

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

Lecture Set 4: More About Methods and More About Operators

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

Zheng-Liang Lu Java Programming 45 / 79

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1.1 Your First Program

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

Lecture Set 4: More About Methods and More About Operators

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Selenium Class 9 - Java Operators

Object-Oriented Programming

Building Java Programs

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

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

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

CIS133J. Working with Numbers in Java

DATA TYPES AND EXPRESSIONS

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Introduction to Computer Science Unit 2. Notes

Building Java Programs

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

1.1 Your First Program

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Introduction to Computer Science Unit 2. Notes

Lecture 3 Operators MIT AITI

Variables, Types, Operations on Numbers

Prof. Navrati Saxena TA: Rochak Sachan

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

MODULE 02: BASIC COMPUTATION IN JAVA

Java Basic Programming Constructs

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Lecture 2: Operations and Data Types

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

CS110: PROGRAMMING LANGUAGE I

Chapter 4: Control Structures I

Chapter 3: Operators, Expressions and Type Conversion

Program Fundamentals

Lecture 2: Intro to Java

Some Sample AP Computer Science A Questions - Solutions

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

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

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Lecture #6-7 Methods

Lecture 6: While Loops and the Math Class

COMP 110/L Lecture 13. Kyle Dewey

Selected Questions from by Nageshwara Rao

CSC 1214: Object-Oriented Programming

Algorithms and Programming I. Lecture#12 Spring 2015

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Programming Basics. Digital Urban Visualization. People as Flows. ia

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

CLASSES AND OBJECTS. Fundamentals of Computer Science I

In this chapter, you will:

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

DM503 Programming B. Peter Schneider-Kamp.

Building Java Programs

Simple Java Reference

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

CS111: PROGRAMMING LANGUAGE II

IEEE Floating-Point Representation 1

CS 106 Introduction to Computer Science I

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

Introduction to Programming. Lecture 2: Introduction to C

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Important Java terminology

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

1.1 Your First Program

Java Simple Data Types

1.1 Your First Program

4. Java Project Design, Input Methods

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

AP CS Unit 3: Control Structures Notes

Operators. Java operators are classified into three categories:

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Accelerating Information Technology Innovation

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

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

Transcription:

Visual Programming Lecture 2: More types, Methods, Conditionals Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg

Outline Lecture 1 Review More types Methods Conditionals 2

Outline Lecture 1 Review More types Methods Conditionals 3

Types Kinds of values that can be stored and manipulated. boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text ( hello, example ). 4

Variables Named location that stores a value String a = a ; String b = letter b ; a = letter a ; String c = a + and + b; 5

Operators Symbols that perform simple computations Assignment: = Addition: + Subtraction: Multiplication: * Division: / 6

Questions from last lecture? 7

Outline Lecture 1 Review More types Methods Conditionals 8

Division Division ( / ) operates differently on integers and on doubles! Example: double a = 5.0/2.0; int b = 4/2; int c = 5/2; double d = 5/2; // a = 2.5 // b = 2 // c = 2 // d = 2.0 9

Order of Operations Precedence like math, left to right Right hand side of = evaluated first Parenthesis increase precedence double x = 3 / 2 + 1; // x = 2.0 double y = 3 / (2 + 1); // y = 1.0 10

Mismatched Types Java verifies that types always match: String five = 5; // ERROR! test.java.2: incompatible types found: int required: java.lang.string String five = 5; 11

Conversion by casting int a = 2; double a = 2; // a = 2 // a = 2.0 (Implicit) int a = 18.7; int a = (int)18.7; // ERROR // a = 18 double a = 2/3; // a = 0.0 double a = (double)2/3; // a = 0.6666 12

Outline Lecture 1 Review More types Methods Conditionals 13

Methods public static void main(string[] arguments) { System.out.println( hi ); 14

Adding Methods public static void NAME() { STATEMENTS To call a method: NAME(); 15

class NewLine { public static void newline() { System.out.println(""); Example: Methods public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 16

class NewLine { public static void newline() { System.out.println(""); Example: Methods public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 17

class NewLine { public static void newline() { System.out.println(""); Example: Methods public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 18

Parameters public static void NAME(TYPE NAME) { STATEMENTS To call: NAME(EXPRESSION); 19

Example: Parameters class Square { public static void printsquare(int x) { System.out.println(x*x); public static void main(string[] arguments) { int value = 2; printsquare(value); printsquare(3); printsquare(value*2); 20

Find Error class Square2 { public static void printsquare(int x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare("hello"); printsquare(5.5); What s wrong here? 21

Find Error class Square3 { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5); What s wrong here? 22

Multiple Parameters [ ] NAME(TYPE NAME, TYPE NAME) { STATEMENTS To call: NAME(arg1, arg2); Mahmoud El-Gayyar / OOP 23

Example Multiple Parameters class Multiply { public static void times (double a, double b) { System.out.println(a * b); public static void main(string[] arguments) { times (2, 2); times (3, 4); 24

Return Values public static TYPE NAME() { STATEMENTS return EXPRESSION; void means no type 25

Example: Return Type class Square3 { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5); 26

Example: Return Type class Square4 { public static double square(double x) { return x*x; public static void main(string[] arguments) { System.out.println(square(5)); System.out.println(square(2)); 27

Variables live in the block ({) where they are defined (scope) Variable Scope Method parameters are like defining a new variable in the method Mahmoud El-Gayyar / OOP 28

Example: Variable Scope class SquareChange { public static void printsquare(int x) { System.out.println("printSquare x = " + x); x = x * x; System.out.println("printSquare x = " + x); public static void main(string[] arguments) { int x = 5; System.out.println("main x = " + x); printsquare(x); System.out.println("main x = " + x); 29

Example: Variable Scope class Scope { public static void main(string[] arguments) { int x = 5; if (x == 5) { int x = 6; int y = 72; System.out.println("x = " + x + " y = " + y); System.out.println("x = " + x + " y = " + y); 30

Methods: Building Blocks Big programs are built out of small methods Methods can be individually developed, tested and reused User of method does not need to know how it works In Computer Science, this is called abstraction 31

Mathematical Functions Math.sin(x) Math.cos(Math.PI / 2) Math.pow(2, 3) Math.log(x) 32

Outline Lecture 1 Review More types Methods Conditionals 33

if statement if (CONDITION) { STATEMENTS 34

Example: if Statement public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); public static void main(string[] arguments) { test(6); test(5); test(4); 35

Comparison operators x>y x<y x >= y x <= y : x is greater than y : x is less than y : x is greater than or equal to x : x is less than or equal to y x == y x!=y : x equals y ( equality: ==, assignment: = ) : x not equals y 36

Boolean operators && : logical AND : logical OR if (x > 6) { if (x < 9) { if ( x > 6 && x < 9) { 37

else if (CONDITION) { STATEMENTS else { STATEMENTS 38

Example: else public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); else { System.out.println(x + " is not > 5"); public static void main(string[] arguments) { test(6); test(5); test(4); 39

else if if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else { STATEMENTS 40

Example: else if public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); else if (x == 5) { System.out.println(x + " equals 5"); else { System.out.println(x + " is < 5"); public static void main(string[] arguments) { test(6); test(5); test(4); 41

int to String: Conversion by method String five = 5; // ERROR! String five = Integer.toString (5); String five = + 5; // five = 5 String to int: int foo = 18 ; // ERROR! int foo = Integer.parseInt ( 18 ); 42

Comparison operators Do NOT call == on doubles! EVER. double a = Math.cos (Math.PI / 2); double b = 0.0; a = 6.123233995736766E-17 a == b will return FALSE! 43

44