JAVA APPLET

30
JAVA APPLET JAVA APPLET Creating Web Application using Java Creating Web Application using Java Applet Applet Introduction to Java (2 nd part)

description

JAVA APPLET. Creating Web Application using Java Applet. Introduction to Java (2 nd part). Tipe Data Pada Java. Tipe Data Pada Java. Contoh. Tata Cara Penulisan. Penulisan variabel (wajib) : Serangkaian Unicode karakter yang diawali dengan huruf. - PowerPoint PPT Presentation

Transcript of JAVA APPLET

Page 1: JAVA APPLET

JAVA APPLETJAVA APPLETCreating Web Application using Java AppletCreating Web Application using Java Applet

Introduction to Java (2nd part)

Page 2: JAVA APPLET

Tipe Data Pada JavaTipe Data Pada Java

Keyword Description Size/Format

(integers)

byte Byte-length integer 8-bit two's complement

short integer 16-bit two's complement

int Integer 32-bit two's complement

long Long integer 64-bit two's complement

Page 3: JAVA APPLET

Tipe Data Pada JavaTipe Data Pada Java

Keyword Description Size/Format

(real numbers)

float Single-precision floating point

32-bit IEEE 754

double Double-precision floating point

64-bit IEEE 754

(other types)

char A single character 16-bit Unicode character

boolean A boolean value (true or false)

true or false

Page 4: JAVA APPLET

ContohContoh

Literal Data Type

178 int

8864L long

37.266 double

37.266D double

87.363F float

26.77e3 double

'c' char

true boolean

false boolean

Page 5: JAVA APPLET

Tata Cara PenulisanTata Cara Penulisan

Penulisan variabel (wajib) :

Serangkaian Unicode karakter yang diawali dengan huruf.

Bukan keyword Java, boolean true/false, & null.

Harus unique dalam sebuah scope. Boleh sama jika memiliki scope yang berbeda.

Page 6: JAVA APPLET

Konvensi PenulisanKonvensi Penulisan

Konvensi penulisan untuk method dan field (tidak wajib tapi direkomendasikan) :

Nama variabel diawali huruf kecil, dan nama class diawali huruf kapital.

Nama method klausa kerja dan ditulis dengan diawali oleh huruf kecil untuk kata pertama dan huruf besar untuk setiap huruf pertama dari kata-kata berikutnya, jika ada. Contoh : hitungLuas(), hitungKell(), selesai().

Field static ditulis dengan menggunakan huruf kapital semua.Contoh : MAX_PANJANG, LEBAR, PHI

Field biasa tidak dibuat public melainkan diakses dan diubah dengan melalui pemanggilan method method.

Page 7: JAVA APPLET

ContohContoh

public class MaxVariablesDemo { public static void main(String args[]) {

//integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE;

//real numbers float largestFloat = Float.MAX_VALUE;

double largestDouble = Double.MAX_VALUE;

//other primitive types char aChar = 'S'; boolean aBoolean = true;

Page 8: JAVA APPLET

System.out.println(“max byte = " + largestByte);System.out.println(“max short = " + largestShort);System.out.println(“max integer = " + largestInteger);System.out.println(“max long = " + largestLong);

System.out.println(“max float = " + largestFloat);System.out.println(“max double = " + largestDouble);

if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is

uppercase."); } else { System.out.println("The character " + aChar + " is

lowercase."); } System.out.println("The value of aBoolean is " +

aBoolean); }}

Page 9: JAVA APPLET

OperatorOperator

Operator Use Description

+ op1 + op2 Adds op1 and op2

- op1 - op2 Subtracts op2 from op1

* op1 * op2 Multiplies op1 by op2

/ op1 / op2 Divides op1 by op2

% op1 % op2 Computes the remainder of dividing op1 by op2

Page 10: JAVA APPLET

OperatorOperator

Operator Use Description

++ op++ Increments op by 1; evaluates to the value of op before it was incremented

++op Increments op by 1; evaluates to the value of op after it was incremented

-- op-- Decrements op by 1; evaluates to the value of op before it was decremented

--op Decrements op by 1; evaluates to the value of op after it was decremented

+ +op Promotes op to int if it's a byte, short, or char

- -op Arithmetically negates op

Page 11: JAVA APPLET

OperatorOperator

Operator Use Returns True if

> op1 > op2 op1 is greater than op2

>= op1 >= op2 op1 is greater than or equal to op2

< op1 < op2 op1 is less than op2

<= op1 <= op2 op1 is less than or equal to op2

== op1 == op2 op1 and op2 are equal

!= op1 != op2 op1 and op2 are not equal

Page 12: JAVA APPLET

OperatorOperator

Operator Use Returns True if

&& op1 && op2 op1 and op2 are both true, conditionally evaluates op2

|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2

! ! op op is false

& op1 & op2 op1 and op2 are both true, always evaluates op1 and op2

| op1 | op2 either op1 or op2 is true, always evaluates op1 and op2

^ op1 ^ op2 if op1 and op2 are different--that is if one or the other of the operands is true but not both

Page 13: JAVA APPLET

OperatorOperator

Operator Use Operation

>> op1 >> op2 shift bits of op1 right by distance op2

<< op1 << op2 shift bits of op1 left by distance op2

>>> op1 >>> op2 shift bits of op1 right by distance op2 (unsigned)

Page 14: JAVA APPLET

OperatorOperator

Operator Use Operation

& op1 & op2 bitwise and

| op1 | op2 bitwise or

^ op1 ^ op2 bitwise xor

~ ~op2 bitwise complement

Page 15: JAVA APPLET

Assignment OperatorAssignment Operator

Operator Use Equivalent to

+= op1 += op2 op1 = op1 + op2

-= op1 -= op2 op1 = op1 - op2

*= op1 *= op2 op1 = op1 * op2

/= op1 /= op2 op1 = op1 / op2

%= op1 %= op2 op1 = op1 % op2

&= op1 &= op2 op1 = op1 & op2

|= op1 |= op2 op1 = op1 | op2

^= op1 ^= op2 op1 = op1 ^ op2

<<= op1 <<= op2 op1 = op1 << op2

>>= op1 >>= op2 op1 = op1 >> op2

>>>= op1 >>>= op2 op1 = op1 >>> op2

Page 16: JAVA APPLET

AdditionalAdditional

Operator Use Description

?: op1 ? op2 : op3 If op1 is true, returns op2. Otherwise, returns op3.

[] type [] Declares an array of unknown length, which contains type elements.

type[ op1 ] Creates and array with op1 elements. Must be used with the new operator.

op1[ op2 ] Accesses the element at op2 index within the array op1. Indices begin at 0 and extend through the length of the array minus one.

Page 17: JAVA APPLET

AdditionalAdditional

. op1.op2 Is a reference to the op2 member of op1.

() op1(params) Declares or calls the method named op1 with the specified parameters. The list of parameters can be an empty list. The list is comma-separated.

(type) (type) op1 Casts (converts) op1 to type. An exception will be thrown if the type of op1 is incompatible with type.

new new op1 Creates a new object or array. op1 is either a call to a constructor, or an array specification.

instanceof op1 instanceof op2 Returns true if op1 is an instance of op2

Page 18: JAVA APPLET

ARRAYARRAY

public class ArrayDemo {public static void main(String[] args) {

int[] anArray;anArray = new int[10];

for (int i = 0; i < anArray.length; i++) {anArray[i] = i; System.out.print(anArray[i] + " ");

}}

}

Page 19: JAVA APPLET

ARRAYARRAY

public class ArrayOfStringsDemo { public static void main(String[] args) { String[] anArray = {

"String One", "String Two", "String Three" };

for (int i = 0; i < anArray.length; i++) {

System.out.println(anArray[i].toLowerCase());}

} }

Page 20: JAVA APPLET

Statement Conditional Statement Conditional dan Looping dan Looping

Page 21: JAVA APPLET

Statement Type Keyword

looping while, do-while , for

decision making if-else, switch-case

exception handling try-catch-finally, throw

branching break, continue, label:, return

Page 22: JAVA APPLET

do…while & while…dodo…while & while…do

do…while syntax

do {

statement(s)

} while (expression);

while…do syntax :

while (expression) {

statement

}

Page 23: JAVA APPLET

The for statement syntax :

for(init; termination; inc) {

statement

}

To the infinity…

for ( ; ; ) {

// infinite loop ...

}

for…for…

Page 24: JAVA APPLET

if…else…if…else…

Syntax :

if (expression) {

statement(s)

} [else {

statements (s)

}]

Page 25: JAVA APPLET

Example of if…else…Example of if…else…

public class IfElseDemo {public static void main(String[] args) {

int testscore = 76;char grade;if (testscore >= 90) {

grade = 'A'; } else if (testscore >= 80) {

grade = 'B'; } else if (testscore >= 70) {

grade = 'C'; } else if (testscore >= 60) {

grade = 'D'; } else {

grade = 'F'; } System.out.println("Grade = " + grade);

} }

Page 26: JAVA APPLET

Bentuk Lain if.. elseBentuk Lain if.. else

?: operator syntaxcondition? statement1 : statement2;

Jika condition bernilai true, maka statement1 akan dieksekusi. Sebaliknya, bila bernilai false maka statement2 akan dieksekusi.

Page 27: JAVA APPLET

SwitchSwitch

switch statement syntax : switch (integer expression) {

case integer expression:statement(s);break;

... default:

statement(s);break;

}

Page 28: JAVA APPLET

ContohContoh

public class SwitchDemo {public static void main(String[] args) { int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Heh!?"); break;}}

}

Page 29: JAVA APPLET

Introduction to ExceptionIntroduction to Exception

Untuk menangani kesalahan dalam program, Java menyediakan sebuah mekanisme yang disebut exception.

Ketika sebuah error muncul, program akan mengeluarkan exception.

Dengan adanya exception berarti program tidak berjalan dengan semestinya (misalnya file yang akan dibaca tidak ditemukan, dll), maka JVM akan mencari exception handler.

Page 30: JAVA APPLET

Bentuk UmumBentuk Umum

Exception handler adalah kode yang akan menangani masalah yang bersangkutan.

Ada banyak jenis exception, seperti FileNotFoundException, IOException, dll.

Syntax :try {

statement(s)} catch (exceptiontype name) {

statement(s)} finally {

statement(s) }