250 likes | 331 Views
Review ALGORITMA dan MOOP. Array. Penulisan array yang benar adalah : double [] myList ; myList = new double [10]; Array with myList has variable dimension 10 Index begin from 0 till 9 Value at array dimension > 0 Begin from 0 till n-1
E N D
Array • Penulisan array yang benar adalah : double[] myList; myList = newdouble[10]; • Array with myList has variable dimension 10 • Index begin from 0 till 9 • Value at array dimension > 0 • Begin from 0 till n-1 • Value at […] be in in the form of integer variable.
Array length • To know length of array can use array. length Example: Array 1 dimension: int [] bilangan = new int[10]; System.out.println(“length of array 1 dimension: "+bilangan.length);
Array string • Example of array data char type: char[] city = {‘D’,’a’,’l’,’l’,’a’,’s’}; to print: System.out.println(city); • Example array for String: String[] name={"Andre", "Bunga", "Christine", "Dedianto"}; To print name index-0 System.out.println(name[0]); To print name index-1 System.out.println(name[1]);
Advanced array • Easy to duplicate easy int[] sourceArray = new int[10]; int[] targetArray = new int[sourceArray.length]; • Easy to looping for( inti = 0 ; i < sourceArray.length ; i++ ) targetArray[i] = sourceArray[i];
BReak for(inti=1; i<=3; i++) { for(int j=1; j<=3; j++) { if(j==2) break; System.out.println("i="+i+" dan j="+j); } } • At j==2, execution exit from inner looping • Value j==2, and j==3 not printed • Looping continued at i++
continue for(inti=1; i<=3; i++) { for(int j=1; j<=3; j++) { if(j==2) continue; System.out.println("i="+i+" dan j="+j); } } • At j==2, execution not exit from looping • Following statement be ignored • Value j==2 (following statement) not printed in j++ • Value j==3 (following statement) printed
Exception handling • Statements that can cause exception are in scope try • Exception catch are in scope catch • Statement that in scopecatch is a operation that done if exception occurred • Exception catch at catch Exception e) • After catch, then program will be back to normal. • The next Statement will be running normal
Method Declaration Method formal parameter return value modifier method name public static int max(int num1, int num2) { int result; if(num1>num2) result = num1; else result = num2; return result; } int z = max(x, y); method header parameter list method body return value Calling of method actual parameters (arguments) Bina Nusantara
Bubble Sort Bubble sort ascending • Adjacent value compared • If increasing, then change to become decreasing Bina Nusantara
Selection sort • Search for the biggest value put at the end of array
Random number • Import Declaration import java.util.Random; • Random Initialization Random r = new Random(); • Using of random number inti = r.nextInt(int n) int >= 0 and < n. inti = r.nextInt() int (full range). long l = r.nextLong() long (full range). float f = r.nextFloat() float >=0.0 dan < 1.0. boolean b = r.nextBoolean() boolean (trueataufalse). double d = r.nextGaussian() double mean 0.0 danstandardeviasi 1.0.
Pausing execution • pausing execution for certain time • Useful for simple animation • Syntax: try { Thread.sleep(milliseconds); } catch(Exception e) { } • 1 second = 1000 milliseconds
preparation • For multiple choice, read theories and examples from BINUSMAYA • For essay, Please try to make programs for : - 2 dimension array and read data for arrays • Declaring method and calling it • Bubble and selection sort