Pertemuan #5 Java Language Fundamental

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

Object Oriented Programming in JAVA
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Introduction to Java Kiyeol Ryu Java Programming Language.
Some basic I/O.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Getting Started What is Java? A programming language –Fully buzzword-compliant: A simple, object oriented, distributed, interpreted, robust, secure,
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Java Workshop for Teachers May 6, 2005 A Brief Look at the Java Programming Language.
Advanced Java New York University School of Continuing and Professional Studies.
DAT602 Database Application Development Lecture 5 JAVA Review.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
1 Part I : Chapter 01 Introduction to Java Programming.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Introduction to Computer Systems and the Java Programming Language.
POS 406 Java Technology And Beginning Java Code
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 2.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to Java Programming. Introduction Course Objectives Organization of the Book.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and web applications Very rich GUI libraries Portability (machine independence) A real Object.
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
JAVA Programming “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
Object Oriented Programming (OOP)
Object Oriented Programming in
Information and Computer Sciences University of Hawaii, Manoa
Basic Introduction to C#
JAVA MULTIPLE CHOICE QUESTION.
Java Applet Programming Barry Sosinsky Valda Hilley
Chapter 1 Introduction to Computers, Programs, and Java
Chapter No. : 1 Introduction to Java.
Internet and Java Foundations, Programming and Practice
What is Java? A programming language Fully buzzword-compliant:
Data types and variables
Distributed Computing, M. L. Liu
Introduction to Programming in Java
Distributed Computing, M. L. Liu
Statements, Comments & Simple Arithmetic
Introduction to Java.
Starting JavaProgramming
An Introduction to Java – Part I, language basics
UNIT-5.
Units with – James tedder
Units with – James tedder
CSC 551: Web Programming Spring 2004
elementary programming
CSC 551: Web Programming Spring 2004
Introducing Java.
Presentation transcript:

Pertemuan #5 Java Language Fundamental Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2 Pertemuan #5 Java Language Fundamental

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

Introduction to Java Programming Language Outline Materi Introduction to Java Programming Language Fundamental of Java Programming: Data Type Variable Declaration Operator Branching Method Array

“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

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)

Architecture

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

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>

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

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

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: DataType varName; Example: int number; Declaration and Initialization DataType varName = varValue; Example: int number = 0;

Relational and Conditional Shift and Logical Operator Arithmetic Relational and Conditional Shift and Logical Shift: >>, << Logical: && (and), || (or), ! (not) Assignment Other ., [], instanceof, new, ?:, etc

Operators: Example: Arithmetic Operator Addition: + Subtraction: - Multiplication: * Division: / Increment: ++ Decrement: -- Example: int a=10, b=5, c; c = a++ + ++b; // c = 10 + 6 -> 16

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

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

Looping Example for ( int i = 1; i <= 100; i++ ); 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; else if (n>=75) grade = ‘B’; if (n>=65) grade = ‘C’; if (n>=55) grade = ‘D’; 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”; case -30: // if (errorCode == -30) msg = “Write Error”; default: // otherwise msg = “Unknown Error”; }

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

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"; };