Pertemuan #5 Java Language Fundamental

Preview:

DESCRIPTION

Pertemuan #5 Java Language Fundamental. Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Memahami elemen dasar pemrograman Java Membuat program Java applet dan swing. Outline Materi. - PowerPoint PPT Presentation

Citation preview

1

Pertemuan #5Java Language Fundamental

Matakuliah : T0053/Web Programming

Tahun : 2006

Versi : 2

2

Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa

akan mampu :

• Memahami elemen dasar pemrograman Java

• Membuat program Java applet dan swing

3

Outline Materi

• Introduction to Java Programming Language

• Fundamental of Java Programming:– Data Type– Variable Declaration– Operator– Branching– Method– Array

4

What Is Java?

• “Write Once, Run Everywhere”

• Semi compiled code (byte code), run under JVM (Java Virtual Machine)

• Free JDK for programmer, license company

• Like C++, SmallTalk and Tcl/Tk

• Safe, Portable, Multithreaded, High Performance, Distributed, but Simple

• Riched and Powerfull Library

• Running Application inside Web Browser

5

History of Java

• Green Project, named Oak, start at Dec 1990, Tim Leader by:James Gosling

• *7 (Star7), handheld wireless PDA with touch screen, finished at 1992

• Target: language that suitable for electronic consumer product, can run in many platform and small footprint

• First launch on May 23, 1995, in SunWorld magazine

• Small team (<30 people), joined with Netscape Inc to incorporate with Netscape Navigator

• Release of Hotjava Web Browser and JDK 1.0 (Java Developer Kit)

6

Architecture

7

Java Family Suite

• Java 2 Platform, Standard Edition (J2SE)– For desktop, client/server

application

• Java 2 Platform, Enterprise Edition (J2EE)– For e-business, e-commerce

web based application

• Java 2 Platform, Micro Edition (J2ME)– For small devices, like palm,

hand phone, etc

8

Typical Java EnvironmentTypical Java Environment

compiler

public class HelloWorld {

public static void main (String[] args) {

System.out.println(“Hello World !”);

}

}

public class HelloWorld {

public static void main (String[] args) {

System.out.println(“Hello World !”);

}

}

HelloWorld.java

Java bytecode(.class)

Compiler producesjava byte code

Web Server

Java byte code placedon Web Server fordownload

Interpreter Interpreter Interpreter Interpreter

Client using Web Browser

Write Once Run Everywhere !Write Once Run Everywhere !

9

Compile and Run Java Application

C:\>javac HelloWorldApp.java

/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */class HelloWorldApp { public static void main(String[] args) { // Display "Hello World!" System.out.println("Hello World!"); }}

10

Compile and Run Java Applet

<HTML><HEAD><TITLE>A Simple Program</TITLE></HEAD><BODY>Here is the output of my program:<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25></APPLET></BODY></HTML>

import java.applet.*;import java.awt.*; /** * The HelloWorld class implements an applet that * simply displays "Hello World!". */public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); }}

C:\javac HelloWorld.java

C:\appletviewer Hello.html

11

Applet and HTML

import java.awt.*; //menyertakan paket awtimport java.applet.*; //menyertakan paket applet //kelas Salam turunan(extends) dari kelas Appletpublic class Salam extends Applet { Font f = new Font ("TimesRoman", Font.BOLD, 50);//set jenis fontString nama; //buat variabel nama dengan tipe data Stringpublic void init() { //tempat inisialisasinama=getParameter ("nama");if (nama==null) //jika kosongnama="Muhammad Subchan";//set nama nama="Hai " + nama;}public void paint (Graphics g) {g.setFont (f); /set applet dengan fontg.setColor(Color.green);//berwarna hijaug.drawString(nama, 60,60);//tampilkan string di posisi 60,60}}

12

Applet and HTML

<html><head><title>Salam Applet </title></head><body bgcolor=pink><font color=blue><h3> Contoh Applet</h3></font><applet code ="Salam.class" width=300 height=200

CODEBASE="\j2se\ bin"><align=top><param name=nama value="Iwan"></applet></body></html>

13

Applet and HTML

14

Appletviewer

C:\appletviewer Salam.html

15

Swing

Swing is GUI Based Windows application.

Usually We need :• javax.swing package• JFrame class• JLabel,JButton, JTextBox, JPasswordField,

JChecxbox etc for creating controls

for creating Swing application

16

Swing

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Gambar{public static void main (String[] args) { JFrame f= new JFrame ("Memasukkan gambar Java"); JLabel l = new JLabel ("Ini gambar antik dari Mr. Widodo"); //buat objek p dan berisi gambar jpg //simpan gambar jpg anda di drive c:\ JLabel p = new JLabel ( new ImageIcon ("c:/xml-pic.jpg")); p.setPreferredSize(new Dimension (100,230)); f.getContentPane().setLayout ( new FlowLayout()); //tempelkan ke frame f.getContentPane().add(l); f.getContentPane().add(p); f.pack(); f.setVisible(true); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }} c:\java

Gambar

17

Which Java will we Use?

• For all Java family, use the same Java Fundamental Programming concept that we will learn in this chapter

• For building GUI Based application, you will need to learn how to use Swing and AWT ( we are not focusing here)

• For Web Application, we will use Java Servlet and JSP, that is a part of J2EE

18

Java Data Types

• All Java Data Types will have the same size and characteristic for all platform

• Java Data Types consist of:– Primitive Data Types

• Simple built-ins data types, ie: int, char, boolean, float, etc

– Reference Types• For all object, ie: array, object, interface, etc

19

Java Primitive Data Types

• Integer:– byte: 8-bit signed integer– short: 16-bit signed integer– int: 32-bit signed integer– long: 64-bit signed integer

• Real Number– float: single precision floating point 32-bit IEEE 754– double: double precision floating point 32-bit IEEE

754• Other Types:

– char: a single character, 16-bit Unicode character– boolean: a boolean value (true/false)

20

Variable Declaration

• Variable Declaration:– DataType varName;– Example: int number;

• Declaration and Initialization– DataType varName = varValue;– Example: int number = 0;

21

Operator

• Arithmetic

• Relational and Conditional

• Shift and Logical– Shift: >>, <<– Logical: && (and), || (or), ! (not)

• Assignment

• Other– ., [], instanceof, new, ?:, etc

22

Arithmetic Operator

• Operators:– Addition: +– Subtraction: -– Multiplication: *– Division: /– Increment: ++– Decrement: --

• Example:int a=10, b=5, c;c = a++ + ++b;// c = 10 + 6 -> 16

23

Relational and Conditional

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

24

Looping

• while (condition) {statement;}

• do {statement;} while (condition);

• for (initialization; condition; incr/decr)

{ statement; }

for ( int counter = 1; counter <= 10; counter++ )

Increment of control variable

Control variable

Final value of control variable for which the condition is true

for keyword

Loop-continuation condition

Initial value of control variable

Required semicolon separator

Required semicolon separator

25

Looping Example

• for ( int i = 1; i <= 100; i++ );• for ( int i = 100; i >= 1; i-- );• for ( int i = 7; i <= 77; i += 7 );• int i=0; do {i++;} while (i<10);• int i=10; while (i>0) i--;

26

Branching

• if (condition) statement;if (n>=85) grade = ‘A’;

• if (condition) statement1; else statement2;if (n>=85) grade = ‘A’;elseif (n>=75) grade = ‘B’;elseif (n>=65) grade = ‘C’;elseif (n>=55) grade = ‘D’;elsegrade = ‘E’;

• switch (condition) { .. }

27

Branching: switch ()

switch (errorCode){ case 0: // if (errorCode == 0) msg = “No Error”; break; case -10: // if (errorCode == -10) msg = “Read Error”; break;

case -30: // if (errorCode == -30) msg = “Write Error”; break;

default: // otherwise msg = “Unknown Error”;}

28

Method

• Method is a function that reside in class

• Method declaration is the same of function declaration in C++

• Example:class Calculator{

int add(int op1, int op2)

{ return op1+op2; }

}

29

Array

• Definition– Data structures– Related data items of same type– Remain same size once created

• Fixed-length entries

– Group of variables• Have same data type

– Reference Data type (treat as object)

• Declaration:int [] ar = new int[10];

orint [] ar = null;ar = new int[10];

30

Applet Database

Please add some codes at file c:\j2sdk\jre\lib\security\java.policy

for permission to access database.

grant{

permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";

permission java.util.PropertyPermission "file.encoding", "read";

};

Recommended