Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

29
Lilian Blot 1 Lilian Blot Spring 2014 TPOP 1

Transcript of Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Page 1: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

11

Lilian Blot Spring 2014TPOP

1

Page 2: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

22

Lilian Blot Spring 2014TPOP

2

0 <5 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 =100

0

2

6

7

1

5

3

10

13

5

4

3

5 5 5

11

4

6

2

5

7

5

0

3

1

6

3

13

3

5

7

11

5

9

7 7 7

3

4

9

3

9

6

1

Chart Title

Formative 2012 Formative 2013

Page 3: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

33

Lilian Blot Spring 2014TPOP

3

Page 4: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian BlotLilian Blot

FROM PYTHON TO JAVA

Java

Spring 2014TPOP

4

Page 5: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Key Points

At the end of the lecture you should understand

Interpreted versus compiled languages Static Typing Declaration of and Assignment to variables in Java Operators and Expressions Selection Structure in Java The for loop structure

Spring 2014TPOP

5

Page 6: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Why learning another language?

The recruitment companies say employers, many of them in London, are looking for flexible IT staff that have knowledge of three or four different programming languages.

Research suggests that learning more than one programming language can increase your salary by between £3,000 and £10,000 per annum.

Spring 2014TPOP

6

Page 7: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Why learning another language?

Specific applications need specific languages.

To learn new programming paradigms.

To understand differences (sometime subtle) between languages. Knowing what to look for.

Spring 2014TPOP

7

Page 8: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Python & Java

Java syntax is more formal than Python syntax.

Java is an industrial strength language, Python is a scripting language.

Java is better for large project where several developers are working together.

Spring 2014TPOP

8

Page 9: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Python & Java

There are many features that are common to both languages: variables, loops, (while and for) conditional, if-elif-else functions, methods classes and inheritance.

Spring 2014TPOP

9

Page 10: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

First Program

A small program to print a string:

Spring 2014TPOP

10

def main(): print “Hello World"

Python Code

public class Hello { public static void main(String[] args){ System.out.println(“hello world”); }}

Java Code : file Hello.java

Page 11: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Execution

Java is not an interpreted language

Java is compiled into a Java bytecode

Java bytecode is interpreted by the Java Virtual Machine (JVM)

Spring 2014TPOP

11

Page 12: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Execution

Spring 2014TPOP

12

>>> main()“Hello World"

Python Interpreter

DOS prompt

Compile the file using: javac filename.java

execute the file using: java filename

Page 13: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

First Program

Every java program MUST define a class ALL code is inside a class

The name of the file MUST be the name of the class plus the extension .java

Every executable Java programs MUST have a function calledpublic static void main(String[] args)

Spring 2014TPOP

13

public class Hello { public static void main(String[] args){ System.out.println(“hello world”); }}

Java Code: Hello.java

Page 14: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Types

Everything in Java must have a typeThere is two classes of type:

primitives Objects

Spring 2014TPOP

14

Primitive Object (wrapper)

int Integer

long Long

short Short

float Float

double Double

char Char

boolean Boolean

Page 15: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Variables

Python is dynamically typed type checking at run-time

Java is statically typed type checking at compile-time

Consequences:A variable must be declared with a specific

typeA variable can only be assigned a value of the

declared typeA variable cannot change its type

Spring 2014TPOP

15

Page 16: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Declaration and Assignment

Declaration Syntax:Type variable_1, variable_2, … ;

Assignment Syntax:variable = expression ;

Combined Syntax:Type variable_1 = expr_1, variable_2, … ;

Spring 2014TPOP

16

Page 17: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Variables

Spring 2014TPOP

17

public class EggBasket{

public static void main(String[] args){

int numberOfBaskets, eggsPerBasket, totalEggs;

numberOfBaskets = 10; eggsPerBasket = 6;

totalEggs = numberOfBaskets * eggsPerBasket;

System.out.println("If you Have:"); System.out.println(eggsPerBasket + " eggs per basket and"); System.out.println(numberOfBaskets + " baskets, then"); System.out.println("total number of eggs is: " + totalEggs); }}

Java Code extract

variable declaration

Assignment Statements

Expression

Page 18: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Variables

Spring 2014TPOP

18

public class EggBasket{

public static void main(String[] args){

int numberOfBaskets, eggsPerBasket, totalEggs;

String numberOfBaskets = "number"; eggsPerBasket = 6;

totalEggs = numberOfBaskets * eggsPerBasket; }}

Java Code extract

Compiled

changing the type of a variable

Page 19: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Variables

Spring 2014TPOP

19

public class EggBasket{

public static void main(String[] args){

int numberOfBaskets, eggsPerBasket, totalEggs;

numberOfBaskets = "number"; eggsPerBasket = 6;

totalEggs = numberOfBaskets * eggsPerBasket; }}

Java Code extract

Compiled

Assigning a different type to a variable

Page 20: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Operator Precedence

Operator Python Operator Java Name

Precedence

+, - +, - Unary plus and minus (e.g. -10)

not !

*, /, //, % *, /, %Multiplication, division, integer division, and remainder

+, - +, - Binary plus and minus (e.g. 3-10)

<, <=, >= <, <=, >= Comparison

==, != ==, != Equality

and &&

or ||

=, +=, -=, *=, /=, //=, %=

=, +=, -=, *=, /=, %=

Assignment operators

TPOP

20

Spring 2014

Page 21: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Selection Structure

Python Code

……if condition : Statement A1 … Statement Anelse : Statement B1 … Statement Bk…

Java Code

……if (condition) { Statement A1 … Statement An} else { Statement B1 … Statement Bk}…

Spring 2014TPOP

21

Page 22: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Simple if-statement

Python Code

……if condition : Statement A1 … Statement An

statement Xstatement Y…

Java Code

……if (condition) { Statement A1 … Statement An}statement Xstatement Y…

Spring 2014TPOP

22

Page 23: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

if-else if-else statement

Python Code

…if cond_1 : Statement Aelif cond_2 : Statement B …elif cond_n : Statement C …else : Statement D

Java Code

…if (cond_1) { Statement A} else if (cond_2) { Statement B …} else if (cond_n) { Statement C …} else { Statement D}…

Spring 2014TPOP

23

Page 24: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Nested if-else statements

Python Code

……if cond_1 : Statement Aelse : Statement B1 Statement B2 if cond_2 : Statement C else : Statement D…

Java Code

……if (cond_1) { Statement A} else { Statement B1 Statement B2 if (cond_2) { Statement C } else { Statement D }}…

Spring 2014TPOP

24

Page 25: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Iteration: For loops

Python Code

sum_square = 0

for val in [1,2,3]: square_val = val * val sum_square += square_val

print sum_square

Java Code

int sum_square = 0;

for(val=1; val<=3; val+=1){ int square_val = val * val; sum_square += square_val;}

System.out.println(sum_square)

Spring 2014TPOP

25

Page 26: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Iteration: For loop Syntax

for (initialisation; boolean_expression; update){ body //executed only if boolean_expression is true}

Example:

for (int index = 10; index > 0; index--){ System.out.println(index)}

Spring 2014TPOP

26

Page 27: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Summary

During the lecture we have covered:

Interpreted versus compiled languagesStatic TypingDeclaration of and Assignment to variables in

JavaOperators and ExpressionsSelection Structure in JavaThe for loop structure

Spring 2014TPOP

27

Page 28: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Exercises

Rewrite in Java your answers to questions 1-4 of practical 2.

Spring 2014TPOP

28

Page 29: Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.

Lilian Blot

Personal Research: More on Selection

Investigate the use of the following statement:switch (expr){ case : … case : …}

How does it differs from:if – else if - else

Spring 2014TPOP

29