Download presentation
Presentation is loading. Please wait.
1
Pertemuan #5 Java Language Fundamental
Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2 Pertemuan #5 Java Language Fundamental
2
Memahami elemen dasar pemrograman Java
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Memahami elemen dasar pemrograman Java Membuat program Java applet dan swing
3
Introduction to Java Programming Language
Outline Materi Introduction to Java Programming Language Fundamental of Java Programming: Data Type Variable Declaration Operator Branching Method Array
4
“Write Once, Run Everywhere”
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 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 c:\java Gambar 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 consist of:
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:
DataType varName; Example: int number; Declaration and Initialization DataType varName = varValue; Example: int number = 0;
21
Relational and Conditional Shift and Logical
Operator Arithmetic Relational and Conditional Shift and Logical Shift: >>, << Logical: && (and), || (or), ! (not) Assignment Other ., [], instanceof, new, ?:, etc
22
Operators: Example: Arithmetic Operator Addition: + Subtraction: -
Multiplication: * Division: / Increment: ++ Decrement: -- Example: int a=10, b=5, c; c = a b; // c = > 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
for ( int counter = 1; counter <= 10; counter++ )
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
25
Looping Example for ( int i = 1; i <= 100; i++ );
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; else if (n>=75) grade = ‘B’; if (n>=65) grade = ‘C’; if (n>=55) grade = ‘D’; 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”; case -30: // if (errorCode == -30) msg = “Write Error”; default: // otherwise msg = “Unknown Error”; }
28
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 Declaration: int [] ar = new int[10]; or
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 grant{
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"; };
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.