1 / 30

Pertemuan #5 Java Language Fundamental

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.

olisa
Télécharger la présentation

Pertemuan #5 Java Language Fundamental

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  8. 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

  9. 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

  10. 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

  11. 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 } }

  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

  24. 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; }

  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’; else if (n>=75) grade = ‘B’; else if (n>=65) grade = ‘C’; else if (n>=55) grade = ‘D’; else grade = ‘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]; or int [] 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"; };

More Related