Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2."— Presentation transcript:

1 Lilian Blot 11 Spring 2014 TPOP 1

2 Lilian Blot 22 Spring 2014 TPOP 2

3 Lilian Blot 33 Spring 2014 TPOP 3

4 Lilian Blot FROM PYTHON TO JAVA Java Spring 2014 TPOP 4

5 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 2014 TPOP 5

6 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 2014 TPOP 6

7 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 2014 TPOP 7

8 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 2014 TPOP 8

9 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 2014 TPOP 9

10 Lilian Blot First Program A small program to print a string: Spring 2014 TPOP 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

11 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 2014 TPOP 11

12 Lilian Blot Execution Spring 2014 TPOP 12 >>> main() Hello World" Python Interpreter DOS prompt Compile the file using: javac filename.java execute the file using: java filename

13 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 called public static void main(String[] args) Spring 2014 TPOP 13 public class Hello { public static void main(String[] args){ System.out.println(hello world); } Java Code: Hello.java

14 Lilian Blot Types Everything in Java must have a type There is two classes of type: primitives Objects Spring 2014 TPOP 14 PrimitiveObject (wrapper) intInteger longLong shortShort floatFloat doubleDouble charChar booleanBoolean

15 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 type A variable can only be assigned a value of the declared type A variable cannot change its type Spring 2014 TPOP 15

16 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 2014 TPOP 16

17 Lilian Blot Variables Spring 2014 TPOP 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 declarationAssignment StatementsExpression

18 Lilian Blot Variables Spring 2014 TPOP 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

19 Lilian Blot Variables Spring 2014 TPOP 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

20 Lilian Blot Operator Precedence Operator PythonOperator JavaName +, - 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

21 Lilian Blot Selection Structure Python Code … if condition : Statement A1 … Statement An else : Statement B1 … Statement Bk … Java Code … if (condition) { Statement A1 … Statement An } else { Statement B1 … Statement Bk } … Spring 2014 TPOP 21

22 Lilian Blot Simple if-statement Python Code … if condition : Statement A1 … Statement An statement X statement Y … Java Code … if (condition) { Statement A1 … Statement An } statement X statement Y … Spring 2014 TPOP 22

23 Lilian Blot if-else if-else statement Python Code … if cond_1 : Statement A elif 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 2014 TPOP 23

24 Lilian Blot Nested if-else statements Python Code … if cond_1 : Statement A else : 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 2014 TPOP 24

25 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 2014 TPOP 25

26 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 2014 TPOP 26

27 Lilian Blot Summary During the lecture we have covered: 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 2014 TPOP 27

28 Lilian Blot Exercises Rewrite in Java your answers to questions 1-4 of practical 2. Spring 2014 TPOP 28

29 Lilian Blot Personal Research: More on Selection Investigate the use of the following statement: switch (expr){ case : … } How does it differs from: if – else if - else Spring 2014 TPOP 29


Download ppt "Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2."

Similar presentations


Ads by Google