300 likes | 439 Views
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.
E N D
Pertemuan #5Java 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 • Introduction to Java Programming Language • Fundamental of Java Programming: • Data Type • Variable Declaration • Operator • Branching • Method • Array
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
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)
Java 2 Platform, Standard Edition (J2SE) For desktop, client/server application Java Family Suite • 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
Typical Java Environment public class HelloWorld { public static void main (String[] args) { System.out.println(“Hello World !”); } } Client using Web Browser HelloWorld.java compiler Interpreter Interpreter Interpreter Interpreter Compiler produces java byte code Java bytecode (.class) Java byte code placed on Web Server for download Write Once Run Everywhere ! Web Server
Compile and Run Java Application /** * 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!"); } } C:\>javac HelloWorldApp.java
Compile and Run Java Applet 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:\appletviewer Hello.html <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> C:\javac HelloWorld.java
Applet and HTML import java.awt.*; //menyertakan paket awt import java.applet.*; //menyertakan paket applet //kelas Salam turunan(extends) dari kelas Applet public class Salam extends Applet { Font f = new Font ("TimesRoman", Font.BOLD, 50);//set jenis font String nama; //buat variabel nama dengan tipe data String public void init() { //tempat inisialisasi nama=getParameter ("nama"); if (nama==null) //jika kosong nama="Muhammad Subchan";//set nama nama="Hai " + nama; } public void paint (Graphics g) { g.setFont (f); /set applet dengan font g.setColor(Color.green);//berwarna hijau g.drawString(nama, 60,60);//tampilkan string di posisi 60,60 } }
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>
Appletviewer C:\appletviewer Salam.html
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
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
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
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
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)
Variable Declaration • Variable Declaration: • DataType varName; • Example: int number; • Declaration and Initialization • DataType varName = varValue; • Example: int number = 0;
Operator • Arithmetic • Relational and Conditional • Shift and Logical • Shift: >>, << • Logical: && (and), || (or), ! (not) • Assignment • Other • ., [], instanceof, new, ?:, etc
Arithmetic Operator • Operators: • Addition: + • Subtraction: - • Multiplication: * • Division: / • Increment: ++ • Decrement: -- • Example: int a=10, b=5, c; c = a++ + ++b; // c = 10 + 6 -> 16
forkeyword Control variable Required semicolon separator Final value of control variable for which the condition is true Required semicolon separator for ( int counter = 1; counter <= 10; counter++ ) Initial value of control variable Loop-continuation condition Increment of control variable Looping • while (condition) {statement;} • do {statement;} while (condition); • for (initialization; condition; incr/decr) { statement; }
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--;
Branching • if (condition) statement; if (n>=85) grade = ‘A’; • if (condition) statement1; else statement2; if (n>=85) grade = ‘A’; else if (n>=75) grade = ‘B’; else if (n>=65) grade = ‘C’; else if (n>=55) grade = ‘D’; else grade = ‘E’; • switch (condition) { .. }
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”; }
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; } }
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]; or int [] ar = null; ar = new int[10];
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"; };