Presentation is loading. Please wait.

Presentation is loading. Please wait.

213500 Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES 213500 PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.

Similar presentations


Presentation on theme: "213500 Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES 213500 PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week."— Presentation transcript:

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

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

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

4  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 THIS WEEK

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

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

7 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 PYTHON

8 public class Linear { public int linear(int [] data, int value) { 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 Software Systems - Programming - Week 1 8 LINEAR SEARCH IN JAVA Types for arguments and result Polymorphism: week 5 Code structured by { and } Consecutive statements separated by ;

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

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

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

12 Primitives types  Integers: int  Reals: double  Truth values: boolean  Characters: char  See 1.2 & 1.10 of Nino & Hosch for more Software Systems - Programming - Week 1 12 JAVA TYPES ( 1, 5, -10, etc.) ( 1.0, -3.234, etc.) ( true, false ) ( 'c', '&', '\n', etc.)

13  Truth values  Should be familiar from first 3 weeks of Mathematics, Module 1  Recommendation: variable name should express property  isLeapYear  isClosed  isOpen Software Systems - Programming - Week 1 13 BOOLEANS George Boole (1815 – 1864)

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

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

16  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 Software Systems - Programming - Week 1 16 INSTANCE VARIABLES – FIELDS, ATTRIBUTES Numbers: 0 Boolean: false References: null

17 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  This Friday: static Software Systems - Programming - Week 1 17 CONSTANTS Use constants whenever possible

18  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: list of expressions  Methods with return value: query  object.method( )  Arguments: list of expressions Software Systems - Programming - Week 1 18 EXPRESSIONS This Friday: more about arguments If call is to current object, no explicit object receiver needed Should contain a return expression

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

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

21 isLeapYear = (year % 4) == 0;  = is assignment  == is comparison Software Systems - Programming - Week 1 21 BEWARE!

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

23  Instruction to do something only if a condition is satisfied  With or without ‘else’ part if ( ) { ; // 1 or more statements } if ( ) { ; } else { ; } Software Systems - Programming - Week 1 23 CONDITIONAL STATEMENT

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

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

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

27 public void update(){ if (getValue() != MAX) next(); else { maxC = maxC + 1; reset(); } Software Systems - Programming - Week 1 27 CORRECT SOLUTION

28  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 MAIN POINTS


Download ppt "213500 Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES 213500 PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week."

Similar presentations


Ads by Google