HOORCOLLEGE 2 INTERACTIE EN Programming CONDITIES Introducing Java

  • Slides: 28
Download presentation
HOORCOLLEGE 2: INTERACTIE EN Programming CONDITIES Introducing Java Variables, Types and Methods 213500 PROGRAMMEREN

HOORCOLLEGE 2: INTERACTIE EN Programming CONDITIES Introducing Java Variables, Types and Methods 213500 PROGRAMMEREN 1 213500 Programmeren 201300071 -1 B Module 12: Software Systems 6 SEPTEMBER 2009 6 september 13 November 2010 2013 Software Systems - Programming - Week 1 1

OVERVIEW PROGRAMMING LINE Week 1 Week 6 Week 7 • Values, conditions • Classes,

OVERVIEW PROGRAMMING LINE Week 1 Week 6 Week 7 • Values, conditions • Classes, objects • Exceptions • Stream I/O • GUIs • Concurrency • Networking Week 2 Week 5 Week 8 • Specifications • Testing • Collecitons • Generic structures • Security Week 3 Week 4 Week 9/10 • Abstraction, inheritance • Interfaces, abstract classes • Arrays Project Software Systems - Programming - Week 1 2

MODULE 1 § Week 2: Algorithms § Search § Sorting § Week 6: Functional

MODULE 1 § Week 2: Algorithms § Search § Sorting § Week 6: Functional Programming § Types § Recursion Software Systems - Programming - Week 1 3

THIS WEEK § Today § From Python (and Haskell) to Java § Variables, types,

THIS WEEK § Today § From Python (and Haskell) to Java § Variables, types, methods, basic statements § Friday § Objects and classes § Nino & Hosch: Chapter 0 - 4 Software Systems - Programming - Week 1 4

HELLO WORLD IN PYTHON print “Hello World” > python hello. py “Hello World” hello.

HELLO WORLD IN PYTHON print “Hello World” > python hello. py “Hello World” hello. py Software Systems - Programming - Week 1 5

HELLO WORLD IN JAVA package ss. week 1; Overall structure of application /** *

HELLO WORLD IN JAVA package ss. week 1; Overall structure of application /** * Hello World class. */ public class Hello { In Java, everything is inside a class // @param args command-line arguments; currently unused public static void main(String[] args) { System. out. println("Hello world"); Where execution starts This is the real print command } } Software Systems - Programming - Week 1 6

LINEAR SEARCH IN PYTHON def linear(data, value): """Return index of ’value’ in list ’data’,

LINEAR SEARCH IN PYTHON def linear(data, value): """Return index of ’value’ in list ’data’, or -1 if it does not occur""" i=0 while i < len(data) and data[i] != value: i=i+1 if i == len(data): return -1 else: return i Software Systems - Programming - Week 1 7

LINEAR SEARCH IN JAVA public class Linear { public int linear(int [] data, int

LINEAR SEARCH IN JAVA public class Linear { public int linear(int [] data, int value) { Types for arguments and result Polymorphism: week 5 int i = 0; while (i < data. length && data[i] != value) { i = i + 1; } if (i == data. length) { return -1; } else { return i; } } // end of method } // end of class Code structured by { and } Consecutive statements separated by ; Software Systems - Programming - Week 1 8

PYTHON VERSUS JAVA: MAIN DIFFERENCES § Typed language § Classes public class C 1

PYTHON VERSUS JAVA: MAIN DIFFERENCES § Typed language § Classes public class C 1 {. . } § Syntactical differences {…} ; § Compiled § Compile: java Example. java § Produce: class files: C 1. class C 2. class § Execute: java C 2 (class with main) public class C 2 {. . public static void main (…) } Example. java Software Systems - Programming - Week 1 9

IMPLEMENTATION CONSTRUCTS § Variables § Expressions § Statements § Methods Software Systems - Programming

IMPLEMENTATION CONSTRUCTS § Variables § Expressions § Statements § Methods Software Systems - Programming - Week 1 10

VARIABLES § Name for storage space of a value § Declaration specifies type of

VARIABLES § Name for storage space of a value § Declaration specifies type of the variable § Example: int count; Declaration In the code count Name to refer to the value 10 Memory space for integer value Software Systems - Programming - Week 1 11

JAVA TYPES Primitives types § Integers: int (1, 5, -10, etc. ) § Reals:

JAVA TYPES Primitives types § Integers: int (1, 5, -10, etc. ) § Reals: double (1. 0, -3. 234, etc. ) § Truth values: boolean (true, false) § Characters: char ( 'c', '&', 'n', etc. ) § See 1. 2 & 1. 10 of Nino & Hosch for more Software Systems - Programming - Week 1 12

BOOLEANS § Truth values § Should be familiar from first 3 weeks of Mathematics,

BOOLEANS § Truth values § Should be familiar from first 3 weeks of Mathematics, Module 1 § Recommendation: variable name should express property § is. Leap. Year § is. Closed § is. Open George Boole (1815 – 1864) Software Systems - Programming - Week 1 13

JAVA TYPES Primitives types § Integers: int (1, 5, -10, etc. ) § Reals:

JAVA TYPES Primitives types § Integers: int (1, 5, -10, etc. ) § Reals: double (1. 0, -3. 234, etc. ) § Truth values: boolean (true, false) § Characters: char ( 'c', '&', 'n', etc. ) § See 1. 2 & 1. 10 of Nino & Hosch for more Reference types § Type: class § Value: instance of a class § Special case: String ("word", ”sentencen”) § More: this Friday Software Systems - Programming - Week 1 14

VARIABLES OF CLASS TYPE A declaration Room r represents a storage space for a

VARIABLES OF CLASS TYPE A declaration Room r represents a storage space for a reference to an object that is an instance of class Room r; Declaration In the code r Name to refer to the object Memory space for a reference Software Systems - Programming - Week 1 Object (class instance) in memory 15

INSTANCE VARIABLES – FIELDS, ATTRIBUTES § Instance variable § Part of an object §

INSTANCE VARIABLES – FIELDS, ATTRIBUTES § Instance variable § Part of an object § Exists throughout the whole lifetime of the object § Every object has its own copy of these variables (with its own values) § When object is created, instance variables are initialised to default values Numbers: 0 Boolean: false References: null Software Systems - Programming - Week 1 16

CONSTANTS Declarations public static final int ROOK = 0; public static final int KNIGHT

CONSTANTS Declarations public static final int ROOK = 0; public static final int KNIGHT = 1; public static final int BISHOP = 2; § Not really variables, cannot be changed: final § Purpose § Understandability § Maintainability Use constants whenever possible § This Friday: static Software Systems - Programming - Week 1 17

EXPRESSIONS § Constants § Concrete values: 1, -0. 1, true, null, "text” § Named

EXPRESSIONS § Constants § Concrete values: 1, -0. 1, true, null, "text” § Named constants § Calculations using operators: +, /, -, etc. § Boolean expressions using operators: &&, ||, etc. § Creation-expressions (‘constructors’) § new Class (<arguments>) § Arguments: list of expressions § Methods with return value: query § object. method(<arguments>) § Arguments: list of expressions Should contain a return expression This Friday: more about arguments If call is to current object, no explicit object receiver needed Software Systems - Programming - Week 1 18

LAZY EVALUATION § Operators && and || do not always evaluate both arguments §

LAZY EVALUATION § Operators && and || do not always evaluate both arguments § This depends on evaluation of first argument Example (i 1 < 10) && (i 1 > 0) (i 1 < 11) || (i 1 > 100) Only left-hand side evaluated if e. g. , i 1 == 10 Purpose Protection against possible problematic cases, e. g. , (x == 0) || (y/x < 10) Software Systems - Programming - Week 1 19

BASIC STATEMENTS § Return-statements ® return method result return; // without result value return

BASIC STATEMENTS § Return-statements ® return method result return; // without result value return <expression>; // with result value § Assignment <variable> = <expression>; // existing variable <type> <variable> = <expression>; // new local variable § Method call without result: commando’s object. method(<arguments>); May contain a return; (not necessary) Software Systems - Programming - Week 1 20

BEWARE! is. Leap. Year = (year % 4) == 0; § = is assignment

BEWARE! is. Leap. Year = (year % 4) == 0; § = is assignment § == is comparison Software Systems - Programming - Week 1 21

METHODS AND CONSTRUCTORS public class Counter { private int value; public Counter( ) {

METHODS AND CONSTRUCTORS public class Counter { private int value; public Counter( ) { value = 0; // attribute (instance variable) // Constructor // initialisation } public int get. Value( ) { return value; // declaration query // return statement (type int) } public void next( ) { value = value + 1; // declaration command // assignments } public void reset( ) { value = 0; } } Software Systems - Programming - Week 1 22

CONDITIONAL STATEMENT § Instruction to do something only if a condition is satisfied §

CONDITIONAL STATEMENT § Instruction to do something only if a condition is satisfied § With or without ‘else’ part if (<boolean. Expression>) { <statement>; // 1 or more statements } if (<boolean. Expression>) { <then. Statement>; } else { <else. Statement>; } Software Systems - Programming - Week 1 23

EXAMPLE CONDITIONAL if (counter. get. Value() == 100) { // test counter value counter.

EXAMPLE CONDITIONAL if (counter. get. Value() == 100) { // test counter value counter. reset(); // counter reset } else { counter. next(); // increase counter } Software Systems - Programming - Week 1 24

COUNTER AND MAX Add attribute int max. C and constant int MAX to Counter

COUNTER AND MAX Add attribute int max. C and constant int MAX to Counter max. C increased when max is reached start yes get. Value() == MAX no max. C = max. C + 1 next() reset() end Software Systems - Programming - Week 1 25

CORRECT SOLUTION? public class Counter { // as before public void update(){ if (get.

CORRECT SOLUTION? public class Counter { // as before public void update(){ if (get. Value() != MAX) next(); else max. C = max. C + 1; reset(); } } Why? reset always executed Software Systems - Programming - Week 1 26

CORRECT SOLUTION public void update(){ if (get. Value() != MAX) next(); else { max.

CORRECT SOLUTION public void update(){ if (get. Value() != MAX) next(); else { max. C = max. C + 1; reset(); } } Software Systems - Programming - Week 1 27

MAIN POINTS § Variables, expressions, statements and methods are typed § Methods grouped in

MAIN POINTS § Variables, expressions, statements and methods are typed § Methods grouped in classes § Methods: queries and commands § Curly brackets { } to structure the code § Java intended to be ‘type-safe’ (there are some exceptions) Software Systems - Programming - Week 1 28