001_A_Pemrograman Berorientasi Object

41
Pemrograman Berorientasi Object Pengantar Pemograman Berorientasi Object ImanLHakim 2011

Transcript of 001_A_Pemrograman Berorientasi Object

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 1/41

Pemrograman Berorientasi

Object

Pengantar Pemograman Berorientasi

ObjectImanLHakim 2011

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 2/41

Tujuan Instruksional Khusus

• Mahasiswa mengetahui perbedaan antara

paradigma Pemrograman Beorientasi Objek

dengan paradigm pemrograman yang lainnya

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 3/41

Sub Pokok Bahasan

• Pengenalan dan Sejarah

PBO

• Paradigma

Pemrograman• Pengenalam Konseptual

Objek Model

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 4/41

Others Recommended Reading

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 6/41

Good Programmer

• Any fool can write code that acomputer canunderstand. Goodprogrammers write code thathumans can understand. --

Martin Fowler• Everyone knows that

debugging is twice as hard aswriting a program in the firstplace. So if you are as cleveras you can be when you write

it, how will you ever debugit? --Brian Kernighan

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 7/41

Progamming Paradigm

• A programming paradigm is afundamental style of computerprogramming. (Compare witha methodology, which is a style of solving specific softwareengineering problems.)

Paradigms differ in the conceptsand abstractions used torepresent the elements of aprogram (such as objects,functions, variables, constraints,etc.) and the steps that composea computation (assignment,evaluation, continuations, dataflows, etc.). –wikipedia

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 8/41

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 9/41

Programming Paradigm is all about

“Complexity Taming” 

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 10/41

OOP history of Java

• FORTRAN Nov. 1954 – First successful multi platfrom programming language

• SIMULA 67, 1967 – Introduced Class (Data +Method), Class instance (Object) and Inheritance

• ALGOL, 1958, – Introduced code block with delimiter (“begin”, “end”) 

• C, 1971,

 – It’s Syntaxes inspired many later programming languages .  – Unix made it language of choice.

• Smalltalk, 1969, – Dynamic Object (create, change, delete)

 – First time words “Object Oriented Programming” used. 

• Objective-C, 1983Introduced Reflection taken from meta-circular of 3 LISP

• C++ – C with Class, April 1980

 – C++, July. 1983, Introduced Template taken from Generic Programming of Ada

• Java – Oak , June 1991

 – Java 1, May 23 1995, sekarang Java 6->7

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 11/41

What java have now

• Principles

 – It should be "simple, object oriented, and

familiar".

 – It should be "robust and secure".

 – It should be "architecture neutral and portable".

 – It should execute with "high performance".

 – It should be "interpreted, threaded, anddynamic".

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 12/41

What java have now(cont…) 

JDK 1.1 (Feb 19, 1997)

JDK 1.0 (codename:OAK, Jan 23, 1996)

an extensive retoolingof the AWT eventmodel

inner classes 

JavaBeans 

JDBC 

RMI 

reflection 

J2SE 1.2 (Dec 8, 1998)

strictfp keyword

IntegratedSwing graphical API

JIT compiler 

Java Plug-in 

Java IDL for CORBA interoperability

Collections framework

J2SE 1.3 May 8, 2000

HotSpot JVM included

RMI was modified tosupport optionalcompatibilitywith CORBA 

JavaSound 

Java Naming andDirectory

Interface (JNDI)

Java Platform DebuggerArchitecture (JPDA)

Synthetic proxy classes

J2SE 1.4 (February 6, 2002)

assert keyword

regular expressions 

exception chaining 

IPv6 support

NIO

logging APIimage I/O API

integrated XML parserand XSLT processor

integrated security andcryptography extensions(JCE, JSSE, JAAS)

Java WebStart Preferences API(java.util.prefs)

J2SE 5.0 (September 30,2004)

Generics 

Metadata 

Autoboxing/unboxing

Enumerations 

Swing: New skinnable lookand feel, called synth.

Varargs 

Enhanced for each loop

1.6 and 1.7 will notcovered in this course

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 13/41

Some Definitions

• Source code, merupakan sekumpulan input element yangdisusun sesuai dengan syntax bahasa pemograman.

• Syntax, adalah aturan baku cara menyusun input elementuntuk membuat sebuah statement.

Statement adalah suatu pernyataan utuh dalam bahasapemograman yang bisa mengakibatkan perubahan kondisi(state) pada komputer. dengan kata lain statement adalahsebuah perintah yang harus dilakukan komputer.

• Input element

 –Input element dalam source code java merupakan sekumpulankarakter UNICODE

 – Input element dalam source code java memiliki sifat case-sensitive

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 14/41

Kind of Input element

• White Space, adalah karakter yang diabaikan olehcompiler java, biasanya white space hanya digunakansupaya layout/struktur kode lebih mudah dibaca, yangtermasuk white space adalah spasi, tab, garis baru.

Untuk lebih detil lihat dokumen dari methodisWhitespace di class java.lang.Character. white spacetidak diabaikan jika berada dalam tanda kutif (sebagaistring atau character)

• Comment, adalah text untuk memberi infomasitambahan pada source code, comment tidak diprosesoleh compiler/Interpreter.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 15/41

Kind of Input element

• Token, unit-unit yang membentuk code program yangdiproses oleh compiler/interpreter. token bisadikategorikan sebagai berikut: – Identifiers, identitas dari sebuah element seperti package, class,

function, attribute etc. indentitas sebuah element biasanya

dibedakan dari namanya. – Key Words, Kata2 yang dimengerti dan sudah ditentukan

peruntukannya oleh bahasa pemograman untuk menyusuncode, semua key words ditulis dalam huruf kecil (lower case) .

 – Literals, Representasi suatu nilai dalam source code. contoh 1

(satu integer), 1.0F (satu float), ‘a’ (character a) dan “bandung”(String bandung)

 – Separator/pemisah. berikut ini separator pada ( ) { } [ ] ; , .

 – Operator, berfungsi untuk melakukan perubahan2 nilai.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 16/41

Java’s Identifier rules 

• Start with letter, followed by letters, digits or

both without white character(space, tab,

newline).

• In unicode.

• As long as you like.

• Not a keyword.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 17/41

Java Keywords

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 18/41

How to Read Rail-Road Diagram

All lower-case

is keyword

Rounded Box,

Type as is

AlternativeOnly one

occurred

Box, Fill with

Specified

Element

Empty path,

means another

part is optional

Alternative

may more than

one occurred,

repetition

Capital Case, seeanother diagram

with this name

UPPER CASE,

no other

diagram see

specification

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 19/41

What is Class

• In Java world we can say that OOP is an effort

to create number of classes to be used as

template of number of objects that live on

JVM to solve assigned problem.

• Class is basic building block of Object-oriented

programming code.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 20/41

Component of Class

• A class definition has an interface and body.

• Class body may have data member (field), method, constructor(special type of method).

• Field place where state of object/class will be placed.

• Method specifies what class/object able to do or what behavior

belong to class/object.• Constructor is a method that specify how a class create an object.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 21/41

Location of class

• A java class is written in a file called source

code (compilation unit)

• Every class placed in a location called package.

• If we don’t specify class package, it will be

located in default (anonymous) package.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 22/41

Syntax of Compilation Unit

How to read

•Compilation Unit starts with zero or one Package Declaration, then

• Zero or many Import Declaration, then

• Zero or many Type Declaration, then

• Unicode char number 1a or blank, then• End of File.

CLASS IS A TYPE

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 23/41

Package Declaration

• Modifier…let it blank for now, 

• Package is a keyword,

• Name : name of package

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 24/41

Sample Package Declaration

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 25/41

Create a Class, a How to

• Define/declare class interface/declaration.

• Define class body:

 – Create class constructor(optional)

 – Create class components

• Field(s)

• Methods()

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 26/41

Syntax of Class Declaration

Common class modifier

•Access modifier

•public

•default (blank)

• abstract

• final

Start with letter, followed by letters, digits or both without whitecharacter(space, tab, newline).

•In unicode.

•As long as you like.

•Not a keyword.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 27/41

The most simple class declaration

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 28/41

Meaning of class access modifiers

• Access Modifier (who can use/access the

class)

 – Public, every others class can use it,

 – Default (blank),only class with same

package/location can use it.

• Other modifiers will be explained later.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 29/41

Field Declaration

• Simple field Declaration

• Common field modifiers:

 – Access modifier (who can use it?)

• public,

default (blank),• protected,

• private.

 – static

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 30/41

Sample of simple field declaration

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 31/41

Kind of Class

• From Implementation’s point of view there is 3 kind of class: Concrete Class,

Abstract Class, Pure Abstract Class (Interface).

• There is special kind of Concrete Class named Enumeration (enum).

• Also special kind of Interface named Annotation Interface.

• Class some time’s called Reference Type 

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 32/41

Simplified Java Application Creation

• Create/type source code.

• Compile source code using java compiler (javac)

• Run Application (compiled code/byte code ) on

Java Virtual Machine (JVM) using java.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 33/41

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 34/41

Structure of Stand-Alone Java

Application

• Every S-E Java Application must have “main

class”, public class that have “main method”. 

• Signature/Declaration of main method, see

line 2 (code below).

• Main method will act as gate way between

JVM and our application, code execution will

be started from this method.

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 35/41

Sample Simple Case

• We want to create an application that write“hello world” on console. 

• Save as SimpleApplication.java• Compile using command

 – javac  –d <output_folder> <source_code>

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 36/41

Run the java application

• Use JVM launcher (java).

 – java  –classpath <classes_location> ^

<main_class_name>

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 37/41

Sample case study

Create class

diagram for

application

that simulate

handheld

counter

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 38/41

UML Notation

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 39/41

Sample solution

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 40/41

How about this?

8/7/2019 001_A_Pemrograman Berorientasi Object

http://slidepdf.com/reader/full/001apemrograman-berorientasi-object 41/41

Assigment

• Implement Class counter in Java.

• Create UML Diagram of Thermometer

• Implement UML Diagram of Thermometer