Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec2_Java1 4 Data Types 4 Operators 4 Control Flow Statements 4 Arrays 4 Functions -- Static Methods 4 View Classes as modules Introduction to the Java.

Similar presentations


Presentation on theme: "Lec2_Java1 4 Data Types 4 Operators 4 Control Flow Statements 4 Arrays 4 Functions -- Static Methods 4 View Classes as modules Introduction to the Java."— Presentation transcript:

1 Lec2_Java1 4 Data Types 4 Operators 4 Control Flow Statements 4 Arrays 4 Functions -- Static Methods 4 View Classes as modules Introduction to the Java Language

2 Lec2_Java2 Exampe1: Hello.java class Hello { public static void main(String[] args){ System.out.println("Hello World!"); }

3 Lec2_Java3 Data Types 4 byte,short,int,long 4 float,double 4 char 4 boolean 4 reference array object

4 Lec2_Java4 Operators 4 Arithmetic operators +, -, *, /, %, ++, -- 4 Relational operators >, >=, <, <=, ==, != 4 Conditional operators &&, ||, ! 4 Bitwise operators >>, >>, &, |, ^, ~

5 Lec2_Java5 Control Flow Statements 4 decision making –if-else, switch-case 4 loop –for, while, do-while 4 exception –try-catch-finally, throw 4 miscellaneous –break, continue, label:, return

6 Lec2_Java6 Type[] a = new Type[n] a[0]a[1]a[n-1] Arrays 4 Dynamic 4 Array initialization 4 a.length Type[] a;... a = new Type[expn] a = new Type[]{e1,e2,...,en}; A = {e1,e2,…,en}

7 Lec2_Java7 Example 2: Max.java class Max { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; int max; max = a[0]; for (int i=1; i<a.length; i++){ if (a[i]>max) max = a[i]; } System.out.println(" max="+max); }

8 Lec2_Java8 Example 3: PrintArray class PrintArray { public static void main(String[] args){ int[][] a = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}; for (int i=0; i<a.length; i++){ for (int j=0; j< a[0].length; j++){ System.out.print(a[i][j]+" "); } System.out.println(); }

9 Lec2_Java9 Functions -- Static Methods class Max { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; System.out.println(" max="+max(a)); } static int max(int[] a){ int max = a[0]; for (int i=1; i<a.length; i++){ if (a[i]>max) max = a[i]; } return max; }

10 Lec2_Java10 Functions -- Static Methods class PrintArray { public static void main(String[] args){ int[][] a = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}; printArray(a); } static void printArray(int[][] a){ for (int i=0; i<a.length; i++){ for (int j=0; j< a[0].length; j++){ System.out.print(a[i][j]+" "); } System.out.println(); }

11 Lec2_Java11 View Classes as Modules class Sort { public static void selectionSort(int a[]){ int tmp; for (int i=0; i<a.length; i++) for (int j=i+1; j<a.length; j++) if (a[i]>a[j]){ tmp = a[i]; a[i] = a[j]; a[j] = tmp; }

12 Lec2_Java12 View Classes as Modules public class TestSort { public static void main(String[] args){ int[] a = new int[]{5,6,1,2,7,9,0,10}; Sort.selectionSort(a); for (int i=0; i<a.length; i++){ System.out.print(a[i]+" "); }


Download ppt "Lec2_Java1 4 Data Types 4 Operators 4 Control Flow Statements 4 Arrays 4 Functions -- Static Methods 4 View Classes as modules Introduction to the Java."

Similar presentations


Ads by Google