Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS202 Java Object Oriented Programming Review of Language Basics Chengyu Sun University of California, Santa Barbara.

Similar presentations


Presentation on theme: "CS202 Java Object Oriented Programming Review of Language Basics Chengyu Sun University of California, Santa Barbara."— Presentation transcript:

1 CS202 Java Object Oriented Programming Review of Language Basics Chengyu Sun University of California, Santa Barbara

2 Overview Programming environments Basic program structure Variables and types Operators Methods and recursion Arrays

3 JDK http://java.sun.com javac Welcome.java java Welcome

4 Java IDEs JBuilder Commercial with a scaled-down free version ( JBuilder Foundation ) Eclipse http://www.eclipse.org High quality and free Many tools and plug-ins have to be installed separately Netbeans http://www.netbeans.org All-in-one package Slow on older computers

5 IDE Usage Statistics Survey by BZ Research in December 2005 Eclipse: 65.1% JBuilder: 19.2% Netbeans: 17.9%

6 Eclipse Tips – Views and Perspective Each block is called a View Window -> Show View Package Explorer view A number of views constitute a Perspective Window -> Open Perspective Java perspective

7 Eclipse Tips – Getting Started Create a project Create a class Run the program

8 Eclipse Tips – Project Information Right click on the project name then choose Properties Info Java Compiler

9 Eclipse Tips – Code Formatting Window -> Preferences -> Java -> Code Style -> Formatter Right click -> Source -> Format

10 Input and Output Console Scanner (a JDK 1.5 feature) System.out GUI File Reader and Writer InputStream and OutputStream Scanner Command line parameters

11 Scanner Usage import java.util.Scanner; … Scanner scanner = new Scanner( System.in ); String s = scanner.next(); int i = scanner.nextInt(); double d = scanner.nextDouble(); scanner.close();

12 Example: Grades.java Input A set of grades Output Highest grade Lowest grade Average grade

13 Basic Program Structure File Class VariablesMethods StatementsExpressions Statements VariablesOperatorsLiterals Comments

14 Code Conventions Required Naming conventions (2.1, 9) Comments (5)  Information not readily available in code itself Indentation of if-else (7.4) Recommended Line length (4.1, 4.2) Programming practice (10) Statements (7) http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

15 Comments Description of certain program functions Ignored by Java compiler Can appear anywhere of the program /* a comment */ /* a multiple-line comment */ // another comment /* * a better looking * multiple-line comment */

16 Variables Name Type Value // declaration and assignment int sum=0; // declaration int sum; // assignment sum = 0 Computer Memory 1byte = 8bit sum

17 Types Class types Primitive types boolean – true or false char – ‘a’, ‘b’, ‘c’, …, ‘A’, ‘B’, ‘C’, … short – integers between –2 15 and 2 15 -1 int – integers between –2 31 and 2 31 -1 float – single precision real number double – double precision real number

18 Coercion Implicit type conversion Also called type promotion No loss of precision char  int int  double … Full list on p242, [D&D 6e]

19 Cast Explicit type conversion Possible loss of precision Syntax: ( Type ) Expression double number = 3.6; int integer_part = (int) number; // cast double fraction_part = number – integer_part;

20 Values (a.k.a. Literals) boolean: true, false char: ’a’, ’b’, …, ’A’, ’B’, …, ’1’, ’2’, … float, double: -0.1, 99.99, 1.1e13 short, int: -10, 203, 0x11, 011 …

21 Number Systems Base-2 (Binary) 0, 1 Base-8 (Octal) 0, 1, …, 7 Base-10 (Decimal) 0, 1, …, 9 Base-16 (Hexadecimal) 0, …, 9, A, B, C, D, E 1 1 0 1 2 3 2 2 2 1 2 0 Bin: 1 x 2 3 + 1 x 2 2 + 0 x 2 1 + 1 x 2 0 1 1 0 1 16 3 16 2 16 1 16 0 Hex: 1 x 16 3 + 1 x 16 2 + 0 x 16 1 + 1 x 16 0

22 Operators Arithmetic + - * / % Assignment = +=, -=, *=, /=, %= Increment and decrement ++ --

23 More Operators Relational ==, != >, < >=, <= Conditional ?: Logical Negation: ! AND: && OR: ||

24 Precedence Determines the evaluation order of different types of operators Or, parenthesis to the rescue Exercise: check out the operator precedence table in the textbook (Appendix A) Increment/decrement Arithmetic Logical Assignment !a && b || c && d && a > d !a && (b || c) && d && (a > d) a + b * c – d a + b * c – d++ a + b * c - ++d

25 Associativity Determine the evaluation order of the operators with the same precedence Left-associative Most operators are left-associate E.g. a + b + c Right-associative E.g. ??

26 Control Statements Branch if if … else Switch switch Loop while do … while for Break and continue break continue

27 Method Header Access modifier Return type Name Parameter list Body

28 Recursion A method calls itself void print( int n ) { if( n <= 0 ) System.out.println(); else { System.out.print(“a”); print(n-1); }

29 Recursive Process print(2) “a” print(1) “a”print(0) “b” print(3) “a”

30 Ending Condition When the recursion should stop To avoid infinite recursion, make sure the ending condition Exists Reachable Comes before the recursive call

31 Simple Recursion Examples Factorial f(n) = 1*2*3*…*n Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, … Definition  fibonacci(0) = 0  fibonacci(1) = 1  fibonacci(n) = fibonacci(n-1)+fibonacci(n-2)

32 When Can We Use Recursion? A problem itself is recursively defined Fibonacci  f(n) = f(n-1) + f(n-2) Tree  A tree has a root  Each child of the root is also a tree A problem of size n can be reduced to a problem of size less than n Factorial: n  n-1 Sort: n  n-1 Binary search: n  n/2

33 When Should We Use Recursion? When the homework problem says so When speed of code development takes precedence over code efficiency When the problem is naturally recursive Fibonacci Series When the non-recursive solution is much harder Hanoi tower Solving maze

34 Arrays Name Type Length (or Size) – number of elements in the array Values Computer Memory a a[0]a[1]a[2]a[3]

35 Access Array Elements arrayname.length Index is from 0 to (arrayname.length-1) int a[]; a = new int[10]; a[5] = 3; // assign 3 to the 6 th element // prints out all elements for( int i=0 ; i < a.length ; ++i ) System.out.println( a[i] ); index

36 Array as Parameter Write a method sumArray() which returns the sum of the elements in a given array int sumArray( ?? ) { int sum = 0; ?? return sum; }

37 Array as Return Type Write a method createArray() which returns an integer array of given size n. ?? createArray( int n ) { return ??; }

38 Multidimensional Data HW0HW1HW2 Student19080100 Student2807585 Student3709070 Student4505080

39 Multidimensional Array Array of arrays Initialization?? Allocation??

40 Multidimensional Array as Array-of-Arrays grades2 – an array of 4 arrays grades2[0] – an array of 3 integers: 10, 80, and 100 … grades2[3] – an array of 3 integers: 10, 50, and 80 int grades2 = { {10, 80, 100}, {10, 75, 85}, {10, 90, 70}, {10, 50, 80} };

41 Arrayname.length and Friends arrayname.length is the length of the 1 st dimension arrayname[i].length is the length of the 2 nd dimension arrayname[i][j].length is the length of the 3 rd dimension …

42 More Fun with Multidimensional Array Each row doesn’t have to have the same number of elements Exercise: add initialization to the code above 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 int a[][]; // allocation a = new int[4][]; for( int i=0 ; i < a.length ; ++i ) a[i] = new int[??];

43 Array-related Operations Min, Max, and Average Search and sort

44 Bubble Sort Find a smallest element Put it in the 1 st position Find the 2 nd smallest element Put it in the 2 nd position … { 3, 28, 13, 2, 17, 1, 0 } { 0, 28, 13, 2, 17, 1, 3 } { 0, 1, 13, 2, 17, 28, 3 } { 0, 1, 2, 13, 17, 28, 3 } { 0, 1, 2, 3, 17, 28, 13 } { 0, 1, 2, 3, 13, 28, 17 } { 0, 1, 2, 3, 13, 17, 28 }

45 Binary Search Search for 28 { 0, 1, 2, 3, 13, 17, 28 } Search for 15 { 0, 1, 2, 3, 13, 17, 28 }

46 Binary Search – Code // assume a[] is sorted in ascending order int index = -1; int left = 0, right = a.length-1, mid; while( ?? ) { mid = (left+right)/2; if( a[mid] > value ) ??; else if( a[mid] < value ) ??; else { index = mid; break; }


Download ppt "CS202 Java Object Oriented Programming Review of Language Basics Chengyu Sun University of California, Santa Barbara."

Similar presentations


Ads by Google