Computer Science Introductory Course MSc - Introduction to Java

Similar documents
Computer Science Introductory Course MSc - Introduction to Java

1. Write a program to calculate distance traveled by light

Introduction to Programming (Java) 3/12

ITI Introduction to Computing II

Turing Machines Part Three

ITI Introduction to Computing II

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

DARN: A Matrix Manipulation Language

CSE 1400 Applied Discrete Mathematics Definitions

CS Exam 1 Study Guide and Practice Exam

Lecture 6: Introducing Complexity

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Reasoning About Imperative Programs. COS 441 Slides 10b

CSE 311: Foundations of Computing. Lecture 26: Cardinality

Compiling Techniques

CompA - Complex Analyzer

Lab 2: Static Response, Cantilevered Beam

Deductive Verification

CSE507. Satisfiability Modulo Theories. Computer-Aided Reasoning for Software. Emina Torlak

Model Checking: An Introduction

Axiomatic Semantics. Operational semantics. Good for. Not good for automatic reasoning about programs

CSE370: Introduction to Digital Design

A High Level Programming Language for Quantum Computing

Anatomy of SINGULAR talk at p. 1

Operational Semantics

Intelligent Agents. Formal Characteristics of Planning. Ute Schmid. Cognitive Systems, Applied Computer Science, Bamberg University

We are here. Assembly Language. Processors Arithmetic Logic Units. Finite State Machines. Circuits Gates. Transistors

CSE 200 Lecture Notes Turing machine vs. RAM machine vs. circuits

CA Compiler Construction

Loop Convergence. CS 536: Science of Programming, Fall 2018

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Binary addition example worked out

(a) Definition of TMs. First Problem of URMs

2/2/2018. CS 103 Discrete Structures. Chapter 1. Propositional Logic. Chapter 1.1. Propositional Logic

Discrete Mathematics and Its Applications

ICS141: Discrete Mathematics for Computer Science I

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Digital Electronics Part 1: Binary Logic

Specification of Chemical Formulæ in XL with Operator Overloading

Foundations of

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

Equalities and Uninterpreted Functions. Chapter 3. Decision Procedures. An Algorithmic Point of View. Revision 1.0

Programming Language Concepts, CS2104 Lecture 3

an efficient procedure for the decision problem. We illustrate this phenomenon for the Satisfiability problem.

CS 4110 Programming Languages & Logics. Lecture 16 Programming in the λ-calculus

EE260: Digital Design, Spring n Digital Computers. n Number Systems. n Representations. n Conversions. n Arithmetic Operations.

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Calculating Algebraic Signatures Thomas Schwarz, S.J.

MAT 243 Test 2 SOLUTIONS, FORM A

I. Numerical Computing

CSE 311: Foundations of Computing. Lecture 27: Undecidability

Building Algorithmic Polytopes. David Bremner with D. Avis, H.R. Tiwary, and O. Watanabe December 16, 2014

MiniMat: Matrix Language in OCaml LLVM

Quantum Computing Virtual Machine. Author: Alexandru Gheorghiu Scientific advisor: PhD. Lorina Negreanu

Big-O Notation and Complexity Analysis

First Order Logic: Syntax and Semantics

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

A Little History Incompleteness The First Theorem The Second Theorem Implications. Gödel s Theorem. Anders O.F. Hendrickson

Structural Induction

Continuing discussion of CRC s, especially looking at two-bit errors

Propositional Languages

cis32-ai lecture # 18 mon-3-apr-2006

EECS 144/244: Fundamental Algorithms for System Modeling, Analysis, and Optimization

COMP6463: λ-calculus

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Satisfiability Modulo Theories (SMT)

Weakest Precondition Calculus

Error Detection and Correction: Small Applications of Exclusive-Or

INF5110 Compiler Construction. Spring 2017

First-Order Theorem Proving and Vampire. Laura Kovács (Chalmers University of Technology) Andrei Voronkov (The University of Manchester)

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

hexadecimal-to-decimal conversion

Python. chrysn

COSE312: Compilers. Lecture 17 Intermediate Representation (2)

Chapter 5. Finite Automata

EXTRA CREDIT FOR MATH 39

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Introduction to Python

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

Lexical Analysis: DFA Minimization & Wrap Up

Information and Entropy. Professor Kevin Gold

This chapter covers asymptotic analysis of function growth and big-o notation.

Running Time Evaluation

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

Simply Typed Lambda Calculus

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Turing Machines. The Language Hierarchy. Context-Free Languages. Regular Languages. Courtesy Costas Busch - RPI 1

Theoretical Computer Science

Synchronous Sequential Circuit

COMS 6100 Class Notes

Department of Computer Science University at Albany, State University of New York Solutions to Sample Discrete Mathematics Examination II (Fall 2007)

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

CSE 311: Foundations of Computing. Lecture 12: Two s Complement, Primes, GCD

Functional Big-step Semantics

First-Order Logic. 1 Syntax. Domain of Discourse. FO Vocabulary. Terms

Transcription:

Computer Science Introductory Course MSc - Introduction to Java Lecture 1: Diving into java Pablo Oliveira <pablo@sifflez.org> ENST Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow

Introduction Introduction : Execution model Java programs run inside a Virtual Machine (JVM), a software implementation of a computer (making Java programs portable among different architectures). For the sake of performance and space, the JVM reads bytecode (a low level language). It is easier for humans to write programs in a more expressive, high level language, like Java. The Java compiler is an automatic translator from Java to Bytecode. Introduction Introduction : Concepts A computer program can be seen as a transformation from a state of a computer to a new one. So very informally, programming is a matter of transforming data : Find a computer representation of data Write methods to transform this data In this lecture we will learn about : primitive types : Basic representations of data. operators : Simple operations on data. objects : Combine data representation and methods to transform data. control flow : Control which operations are done and how many times.

Primitive types Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow Primitive types Representing data Data is encoded in memory as a binary string. b i n : 00000000 01001000 00000000 01101001 dec : 0 72 0 105 What does this value represent? Example 2d line : (0,72) to (0,105) integer : 4718697 float : 6.612303E-39 (IEEE 754) string : H i...

Primitive types Variables and Types To lift this ambiguity we introduce types, which specify how a value should be used. We also give each value a name (should start with {letter, $, and contain only {alphanumeric, $, ). We call the association (name, type, value) a variable. Example boolean is it raining = true; 1 bit logic value byte life expectancy = 70; 8 bit range [ 128..127] short year ; 16 bit defined on [ 2 15..2 15 1] char unicode character = A ; UTF-16 caracter int city population = 2167994; 32 bit range [ 2 31..2 31 1] long molecules; 64 bit range [ 2 63..2 63 1] float mean grade = 13.54; single precision(ex. 8 mt. 23) double angular speed; double precision(ex. 11 mt. 52) Primitive types Mutable variables, assignment, final variables The value of a variable can be modified during the execution of a program, except for final variables (which can only be assigned once). i n t a ; // v a r i a b l e d e c l a r a t i o n f l o a t b = 3 ; / v a r i a b l e d e c l a r a t i o n with i n i t i a l assignment / f i n a l f l o a t p i ; p i = 3. 1 4 1 5 9 ; a = 8 ; // assignment b = 1 ; // assignment p i = 0 ; // i l l e g a l (won t compile )

Primitive types Arrays Arrays model a contiguous, random access, fixed-length, collection of values. The values of an array are of the same type. // The type o f the array p r i c e i s f l o a t [ ] f l o a t [ ] p r i c e ; // r e s e r v e memory f o r 3 elements p r i c e = new f l o a t [ 3 ] ; // i n i t i a l i z e the v a l u e s o f the elements p r i c e [ 0 ] = 1. 0 0 ; p r i c e [ 1 ] = 5. 9 9 ; p r i c e [ 2 ] = 3. 2 5 ; // Syntax sugar f o r t h i s i s : f l o a t [ ] p r i c e = { 1. 0 0, 5. 9 9, 3. 2 5 ; Primitive types Strings Strings represent a collection of characters. S t r i n g message = H e l l o World ; Caution Strings and arrays are a mix between objects and primitive types and thus some care must be taken when manipulating them. In the next lecture we will explain why when we ll talk about references and immutability.

Operators Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow Operators Simple Operators Example Operators are special symbols which perform an operation on some operands. The semantic of the operator depends on the type of the operands. a = 5; b[4] = 30 assignment 5 + 4 9 sum 5 4 1 substraction hello + world hello world concatenation 4 4 16 multiplication 15 / 2 7, 15.0 / 2 7.5 integer or float division 15 % 2 1 modulo 15 > 12 true bigger than < >= <= other comparison operators

Operators Increment operators Increment (++), decrement ( ) operators : i n t a, b, c ; a = 4 2 ; b = a++; \\ h e r e b e v a l u a t e s to 42 and a to 43 a = 4 2 ; b = ++a ; \\ h e r e b e v a l u a t e s to 43 and a to 43 with ++a the value of a is incremented, then the right side of the assignment is evaluated. with a++ the right side of the assignment is evaluated, then a is incremented. Operators More Operators Operation + assignment (+= = = /= etc...) a += b ; // e q u i v a l e n t to : a = a + b ; Equality == and Inequality!= i n t a, b ; a = 5 ; c = 2 ; b = c + 3 ; a == b ; // > t r u e a!= c ; // > t r u e Referential equality when used with objects, strings and arrays (we ll come back to this later)

Operators Boolean operators Boolean Operators b o o l a = f a l s e ; b o o l b = true ; / not /! a // > t r u e / and / a && b // > f a l s e / or / a b // > t r u e / C a r e f u l : Lazy / i n t a = 0 ; ( f a l s e ) && ( a++ > 0) // a e v a l u a t e s to 0 ( true ) ( a++ > 0) // a e v a l u a t e s to 0 Operators Other operators Ternary Operator (condition?true stm: false stm ) ex : (a>b)? a : b Bitwise Operators (>> << >>> & ) instanceof

Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow Objects Definition An object is the association of : Example a State (i.e. data) some Methods (i.e. operations that change/read state) A turtle has a state composed of its color, its position and its orientation, and some methods : turn, advance and readposition. 3 2 1 y 30 (2,3) 1 2 3 x State Methods { { Turtle color: green position: (2, 3) rotation: 30.0 turn(double angle) advance() readposition()

Classes Definition A class is a blueprint for making objects. A class defines the common attributes of a family of objects : the methods they share the types of variables they have Classes Example Turtle 1 color: red position: (0, 0) rotation: 90.0 turn(double angle) advance() readposition() Turtle 2 color: blue position: (10, -5) rotation: 35.4 turn(double angle) advance() readposition()

Classes Example Turtle 1 red (0, 0) 90.0 Turtle 2 Class Turtle color: a color position: coordinates rotation: an angle turn(double angle) advance() readposition() blue '10, -5) 35,4 Anatomy of a method return type (i.e. output) name method modifiers parameters (i.e. input) static float norm (float x, float y) { return x*x + y*y ;

In Java c l a s s T u r t l e { C o l o r c o l o r ; P o s i t i o n p o s i t i o n ; double r o t a t i o n ; void t u r n ( double a n g l e ) { r o t a t i o n += a n g l e ; void advance ( ) { i n t s t e p s i z e = 5 ; p o s i t i o n. x += s t e p s i z e cos ( r o t a t i o n Math. PI / 1 8 0 ) ; p o s i t i o n. y += s t e p s i z e s i n ( r o t a t i o n Math. PI / 1 8 0 ) ; P o s i t i o n r e a d P o s i t i o n ( ) { r e t u r n p o s i t i o n ; T u r t l e t u r t l e 1 = new T u r t l e ( ) ; T u r t l e t u r t l e 2 = new T u r t l e ( ) ; P o s i t i o n pos = t u r t l e 1. r e a d P o s i t i o n ( ) ; t u r t l e 2. t u r n ( 2 0 ) ; t u r t l e 2. advance ( ) ; Instance Methods/Variables Objects turtle1 and turtle2 are called instances of class Turtle, turtle2. color represents the color for the specific instance turtle2 : color is an instance variable turtle1. readposition () returns the position of the specific instance turtle1 : readposition() is an instance method Definition An instance method or instance variable is only relevant in the context of a particular object.

Static Methods/Variables Q : how to share a common variable between all the objects in a class? c l a s s T u r t l e { s t a t i c i n t s t e p s i z e = 5 ; Q : how to write a method independent from a particular instance? c l a s s T u r t l e { s t a t i c double degreestoradians ( double deg ) { r e t u r n deg Math. PI /180; T u r t l e. degreestoradians ( 3 0 ) ; > 1.0471975512 Definition A static variable is shared among all the objects of a class. A static method is called in the context of a class and not for a specific object. Static variables are also called class variables. Local Variables / Scope Definition Variables defined inside a method are called local variables. The scope of a variable is the portion of code where it is visible (ie. where you can read it, or modify it). Local variables are only visible inside their method. Instance and Static variables are visible in all the methods from their class. In the next lecture we will talk about the visibility of methods, and about access modifiers that regulate the visibility of methods/variables outside their class.

Main : the program entry point Q : (At last...) How do we start things up? c l a s s MyFirstJavaProgram { p u b l i c s t a t i c void main ( S t r i n g a r g s [ ] ) { System. out. p r i n t l n ( H e l l o world! ) ; (Note : public means this method can be called from anywhere, this is a prerequisite for the main method) Control Flow Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow

Control Flow If - else Depending on a predicate choose which branch to execute i f ( p r e d i c a t e ) b l o c k 1 e l s e b l o c k 2 s t a t i c i n t c o l l a t z ( i n t n ) { i f ( n % 2 == 0) { r e t u r n n / 2 ; e l s e { r e t u r n 3 n + 1 ; code between { is called a block, variables defined inside a block are not visible outside Control Flow while Execute a block repeatedly while a predicate is true w h i l e ( p r e d i c a t e ) b l o c k ; s t a t i c i n t gcd ( i n t a, i n t b ) { w h i l e ( b!= 0) { i n t t = b ; b = a % b ; a = t ; r e t u r n a ; Above if the predicate is false the block is never executed. When appropriate, you can use instead do {... while ( predicate ); which always executes the block at least once.

Control Flow for More sophisticated loop f o r ( s t a t e m e n t ; p r e d i c a t e ; s t a t e m e n t ) b l o c k f o r ( i n t i = 0 ; i < 1 0 0 ; i ++) { System. out. p r i n t l n ( i ) ; Syntax sugar for : i n t i = 0 ; w h i l e ( i < 100) { System. out. p r i n t l n ( i ) ; i ++; Control Flow break and continue Escaping from loops break will exit the loop from which it is called continue will jump to the next iteration of the loop from which it is called i n t w h e r e I s ( i n t element, i n t [ ] s e t ) { i n t i ; f o r ( i = 0 ; i < s e t. l e n g t h ; i ++) { i f ( element == s e t [ i ] ) { break ; r e t u r n ( i < s e t. l e n g t h )? i : 1;

Control Flow You can also escape using return, which here avoids a test and is more elegant. i n t w h e r e I s ( i n t element, i n t [ ] s e t ) { f o r ( i n t i = 0 ; i < s e t. l e n g t h ; i ++) { i f ( element == s e t [ i ] ) { r e t u r n i ; r e t u r n 1; We won t discuss switch/case control structure. If interested you may look it up! This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.