Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

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

PROGRAMMING FUNDAMENTALS

Introduction to the Java Basics: Control Flow Statements

ECE 122. Engineering Problem Solving with Java

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Programming Using Java (98-388)

Principles of Computer Science

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Object-Oriented Programming

CS/ENGRD 2110 FALL 2018

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CSC324 Principles of Programming Languages

Programming with Java

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

The Warhol Language Reference Manual

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

Loops and Expression Types

Condi(onals and Loops

YOLOP Language Reference Manual

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

Object-oriented programming in...

Example: Monte Carlo Simulation 1

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Array. Prepared By - Rifat Shahriyar

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

LECTURE 5 Control Structures Part 2

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

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

CS 11 java track: lecture 1

Simple Java Reference

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Controls Structure for Repetition

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

CompSci 125 Lecture 11

Java Basic Programming Constructs

Lec 7. for loops and methods

Repetition CSC 121 Fall 2014 Howard Rosenthal

Lecture Set 4: More About Methods and More About Operators

egrapher Language Reference Manual

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Expressions & Flow Control

CS 231 Data Structures and Algorithms, Fall 2016

Introduction. C provides two styles of flow control:

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

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

Computer Programming I - Unit 5 Lecture page 1 of 14

Full file at

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Lecture 14. 'for' loops and Arrays

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Introduction to Java

CS/ENGRD 2110 SPRING 2019

CS/ENGRD 2110 SPRING 2019

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CS/ENGRD 2110 SPRING 2018

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Lec 3. Compilers, Debugging, Hello World, and Variables

COMPUTER PROGRAMMING LOOPS

CS 211: Methods, Memory, Equality

Unit 2: Java in the small

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Programming Language Basics

Fall 2017 CISC124 9/16/2017

( &% class MyClass { }

CSEN 202 Introduction to Computer Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

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

CSI33 Data Structures

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lecture Set 4: More About Methods and More About Operators

CS1150 Principles of Computer Science Arrays

Flow Control. CSC215 Lecture

Java is an objet-oriented programming language providing features that support

CA4003 Compiler Construction Assignment Language Definition

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

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

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

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

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

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

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Transcription:

Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su

Lecture 1 Outline 2 Languages Overview Imperative vs Declarative. Types Variable Assignment Control Flow and Procedures scoping

Languages 3 Declarative vs Imperative languages Declarative: Specify what should be done, not how Ex: SQL select * from people where name = Natacha Imperative Specify both how and what should be done Java is imperative and procedural. What about Python?

What about type boolean? Types 4 Definition: A type is a set of values together with operations on them Java defines primitive types and reference types Primitive types: Built-in types that act as building blocks for more complicated types that we ll look at next lecture Reference types: Next lecture :) Example Type: integer: values:, 3, 2, 1, 0, 1, 2, 3, operations: +,, *, /, unary

Most-used primitive types 5 int: values: 2 31.. 2 31 1 operations: +,, *, /, %, unary size: 32 bits as signed integer double: values like : 22.51E6, 24.9 operations: +,, *, /, %, unary size: 64 bits as floating point number char: values like : 'V' '$' '\n' operations: none size: 16 bits boolean: values: true false operations:! (not), && (and), (or) size: 1 bit

Strong Typing 6 Matlab and Python are weakly typed: One variable can contain at different times a number, a string, an array, etc. One isn t so concerned with types. Java strongly typed: A variable must be declared before it is used and can contain only values of the type with which it is declared

7 Strong Typing Matlab and Python are weakly typed: One variable can contain at different times a number, a string, an array, etc. One isn t so concerned with types. Java strongly typed: A variable must be declared before it is used and can contain only values of the type with which it is declared Valid Python sequence: x= 100; x= Hello World ; x= (1, 2, 3, 4, 5 );

8 Strong Typing Matlab and Python are weakly typed: One variable can contain at different times a number, a string, an array, etc. One isn t so concerned with types. Java strongly typed: A variable must be declared before it is used and can contain only values of the type with which it is declared Illegal assignment: Hello is not an int Valid Python sequence: x= 100; x= Hello World ; x= (1, 2, 3, 4, 5 ); Corresponding Java int x; x= 100; x= Hello ; Declaration of x: x can contain only values of type int

Must place myclass in file myclass.java Program Structure in Java 9 The reason for all of this may not seem clear right now, will become clearer in next couple of lectures package packagename; class myclass{ void proc() {...} int fun() {...} public static void main(string[] args) { fun(); proc(); } }

Basic variable declaration 10 10 Declaration: gives name of variable, type of value it can contain int x; double area; int[] a; Declaration of x, can contain an int value Declaration of area, can contain a double value Declaration of a, an int array.

Assignment statement 11 11 Assignment: assigns value to a variable. Much like in other languages need ; at end: <variable>= <expression> ;

Assignment statement 12 12 Assignment: assigns value to a variable. Much like in other languages need ; at end: <variable>= <expression> ; int x; x= 10; other code x= x+1; Have to declare x before assigning to it. int x= 10; other code x= x+1; Can combine declaration with an initializing assignment. Shorthand for a declaration followed by an assignment.

Weakly typed versus strongly typed 13 x = 75 + Hello ; int x = 75 + Hello ; What happens in Python? What happens in Java? myvar = 100; myvar = myvar + 1 print myvar int myvar = 100; myvar = myvar + 1; System.out.println(myVar); What happens in Python? What happens in Java?

Weakly typed versus strongly typed 14 Weakly typed: Shorter programs, generally. Programmer has more freedom, language is more liberal in applying operations to values. Strongly typed: Programmer has to be more disciplined. Declarations provide a place for comments about variables. More errors caught at compile-time (e.g. it s a syntax error to assign a string to an int variable). Note: weak and strong typing not well defined; literature has several definitions

Functions & Procedures. 15 Group linked actions into a single unit of execution Functions take input parameters and return something Procedures take input parameters and return nothing

Functions & Procedures. 16 Group linked actions into a single unit of execution Functions take input parameters and return something Procedures take input parameters and return nothing /** return sum of a and b */ Specification: in comment before function public double sumfunction(double a, double b) { Parameter declarations System.out.println( Sum of a + and + b); Function Body return a + b; } Return Type /** prints sum of a and b */ public void sumprocedure(double a, double b) { System.out.println( Sum is + (a + b)); } No Return Type for Void procedures

Control Flow Recap 17 Control flow syntax is similar to other languages For (initialisation; termination; increment) For (int i = 0 ; i < 10 ; i++) { } While(boolean_expresion) While (i < 10) { ; i++} If (boolean_exp) If { } else { } Branching statements Break: Exit loop Continue: Skip concurrent iteration of loop Return: Exit function immediately

Local Variables 18 Definition: defined inside a function/procedure or in any conditional block. They have block-level scope and are only accessible in the block where they are defined.

Local Variables 19 Definition: defined inside a function/procedure or in any conditional block. They have block-level scope and are only accessible in the block where they are defined. /** return sum of a and b */ public double sumfunction(double a, double b) { double sum = a + b; System.out.println( Sum of a + and + b); return sum; } Use local variables to write clean code and avoid repetition!

Local Variables - Scoping 20 Definition: defined inside a function/procedure or in any conditional block. They have block-level scope and are only accessible in the block where they are defined. A block is defined by a starting bracket { and a closing bracket } Local variables are destroyed once they go outside of scope

Local Variables - Shadowing 21 Definition: A variable shadows another if they have the same name and are accessible in the same place. To what declaration does a name refer? Code in a block can reference names declared in that block, as well as names that appear in enclosing blocks. Use inside-out rule: Look first in method body, starting from name and moving out; then look at parameters; then look outside method in the object.

Local Variables - Shadowing 22 double sum = 0.0; /** return sum of a and b */ double sumfunction(double a, double b) { } if (a>0.0) { double sum = a + b; System.out.println( Sum is + sum); } System.out.println( Sum is + sum); return sum; What will the print statements output? Always give clear names to your variables k Create variables with the smallest possible scopes. As close to their first use

References in JavaHyperText 23 type primitive type type, strong versus weak typing function function call procedure procedure call variable variable declaration expression assignment statement local variables