Download - 01. Pengenalan Java
PEMROGRAMAN FRAMEWORK J2EE
PERTEMUAN IPENGENALAN JAVA (I)
Outline
Introduction “Hello world !” Compiling & Running Program Looping dan Branching Variabel dan Tipe Data Operator dan Pemberian Nilai Methods / functions Contoh program
Introduction
Java adalah salah satu bahasa pemrograman tingkat tinggi
Java digunakan dalam banyak aplikasi termasuk games dan embedded system.
Java mirip sekali dengan C/C++. Java dapat diimplementasikan pada
desktop dan web (berupa applet).
Introduction
Java : code once, run anywhere. (platform independent).
Java mendukung penuh konsep OOP. Lebih sederhana daripada C++ : tidak ada
pointer Multithreading Fitur Garbage Collector.
Hello World !
/* Program HelloWorld.java */
class HelloWorld { public static void main (String args[]) {
System.out.println("Hello World!"); }
}
Compiling & Running Program CLASSPATH adalah environment variable yang
merujuk dimana library Java disimpan dalam sistem.
CLASSPATH digunakan dalam compiling dan running program Java.
Set CLASSPATH di Windows
CompilingC:> javac HelloWorld.java
C:> java HelloWorld
Hello World
C:>
C:\> SET CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip
Looping dan Branching
Looping : for, while, do while Branching : switch-case, if-else
Variabel dan tipe data
Variabel di Java bersifat case-sensitive (sama seperti C/C++)
Tipe data : Primitif : byte, short, int, long, float, double,
boolean, char Non-primitif/reference type : all instance of
objects Collection : Array, ArrayList, Dictionary, etc. Enum type. Etc.
Operator dan Pemberian Nilai Operator dan Pemberian Nilai pada Java
sama dengan C/C++ Untuk variabel berupa object, harus
diinisialisasi terlebih dahulu menggunakan keyword new.
Methods
Komponen-komponen methods di Java secara umum Modifiers : public, private Return type Method name Parameter list Exception Method body
Contohpublic double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) {
//do the calculation here
}
Methods/ Functions
Overloading Overloading memungkinkan kita
mendefinisikan sebuah methods dengan banyak jenis input.
Contohpublic class DataArtist {
...
public void draw(String s) { ... }
public void draw(int i) { ... }
public void draw(double f) { ... }
public void draw(int i, double f) { ... }
}
Contoh Program
Fibonacci Sieve of Erastothenes
Tugas I
Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a program that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on the dial range from zero to thirty-six and three numbers in sequence are needed to open the lock.
Suppose the lock isn't a very good one and any number that's no more than two away from the correct number in each digit will also work. In other words if the combination is 17-6-32, then 18-5-31, 19-4-32, 15-8-33 and many other combinations will also open the lock. Write a program that prints out a minimal list of combinations you would need to try to guarantee opening the lock.