1. penulisan array yang benar adalah : double[] mylist; mylist = new double[10]; array with mylist...

Post on 13-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

REVIEW ALGORITMA DAN MOOP

ARRAY1. 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 Value at […] be in in the form of integer

variable.

2D ARRAY

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

Bina Nusantara

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 ARRAYEasy to duplicate easy

int[] sourceArray = new int[10];int[] targetArray = new int[sourceArray.length];

Easy to loopingfor( int i = 0 ; i < sourceArray.length ; i++ )

targetArray[i] = sourceArray[i];

IMPLEMENTATION

BREAK

Bina Nusantara

for(int i=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++

Bina Nusantara

BREAK

CONTINUE

for(int i=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 loopingFollowing statement be ignoredValue j==2 (following statement) not printed in j++Value j==3 (following statement) printed

CONTINUE

EXCEPTION HANDLING

Bina Nusantara

EXCEPTION HANDLING Statements that can cause exception

are in scope try Exception catch are in scope catch Statement that in scope catch 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

Bina Nusantara

Method Declaration

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);

modifier return value method name

formal parameter

method header

parameter list

method body

return value

Calling of method

actual parameters (arguments)

METHOD OVERLOADING

FACTORIAL

PALINDROM

BUBBLE SORT ASCENDING

Bina Nusantara

Bubble Sort

• Adjacent value compared• If increasing, then change to become decreasing

BUBBLE SORT WITH FLAG

SELECTION SORT

• Search for the biggest value put at the end of array

RANDOM NUMBER Import Declaration

import java.util.Random;

Random InitializationRandom r = new Random();

Using of random numberint i = r.nextInt(int n) int >= 0 and < n. int i = 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 (true atau false). double d = r.nextGaussian() double mean 0.0 dan standar deviasi 1.0.

IMPLEMENTATION

CURRENCY FORMAT

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

top related