Class Attendance
Lectures schedule: Class A: Monday, 08.00 10.00 Class B: Monday, 10.30 13.00 Practical work schedule: Class A: Wednesday, 08.00 10.00 Class B: Wednesday, 10:00 12:00 All students must meet 75% of lectures attendances
On Time
Students are expected to come on time for both
lectures and practicals sessions. More than 10 minutes means no proof of attendace
Practical Works
There are several practical works Checkpoint system (1-5 stage for each pracs)
Checkpoint 1 - basic concepts (everyone) Checkpoint 2 - understand and describe (nearly everyone) Checkpoint 3 - apply to new contexts (most) Checkpoint 4 - compare, synthesize (only top 40%) Checkpoint 5 - generalize, hypothesize, analyze (only top 20%) Try your best, do not copy others work
Editor: Notepad, EditPlus, TextPad, UltraEdit, dll Java Editor: JCreator, DrJava IDE: Eclipse, IntelliJ IDEA
Silabus Perkuliahan
Pertemuan
Minggu 1 Minggu 2 Minggu 3 Minggu 4 Minggu 5 Minggu 6 Perkuliahan ditiadakan Pengenalan Pemrograman Berorientasi Objek (Java) Mengenal lebih dalam Java API dan Java Documentation Konsep Pemrograman Berorientasi Objek: Class, Object Method, Attributes, Encapsulation, & Overloading Libur Hari Raya Idul Fitri
Perkuliahan
Materi Tanggal 09 Agt 16 Agt 23 Agt 30 Agt 06 Sep 13 Sep
Praktikum
P1 Java Lang 1 P2 Java Lang 2 P3 Java Sun Rise P4 Smiley Face P5 Java Clock P6 Color Matcher
Minggu 7 Minggu 8 UTS Minggu 9 Minggu 10 Minggu 11 Minggu 12 Minggu 13 Minggu 14 Minggu 15 UAS
Konsep Inheritance, Overriding, & Polymorphism Review Materi Ujian Teori Abstract Class dan Interface OOP Advanced Features & Exception Handling Java Collections Framework Java Graphical User Interface (GUI) Bagian 1 Java Graphical User Interface (GUI) Bagian 2 Java Input/Output (Java IO) Review Materi Ujian Teori
20 Sep 27 Sep 4-6 Okt 18 Okt 25 Okt 01 Nov 08 Nov 15 Nov 22 Nov 29 Nov 6-18 Des
What Java?
1991: project Oak by James Gosling
Berorientasi objek, berdasarkan bahasa C++ Dirancang untuk memprogram home applicances seperti TV sets, freezers, dll Perlu berjalan di berbagai platform/processor dan harus murah
mirip Java yaitu C# sebagai bagian dari platform .NET, sekaligus menghilangkan Java dari Windows (harus install manual) 2006: Sun released parts of Java as Free/Open Source Software! Versi saat ini: Java Development Kit (JDK) versi 1.6, biasa disebut JDK 6 In 2009, Sun Microsystem (the creator of Java) was acquired by Oracle (2nd largest software company in the world)
Why Java?
cheap almost all tools/softwares/components are available for
free! simple - partially modeled on C & C++ but greatly simplified and improved (eliminates pointers, simplifies multiple inheritance, etc.) object-oriented programming language
any conceptual component in your problem can be represented as an object in your program
portable Java programs are compiled (translated) into the Java Virtual Machine (JVM) code called bytecode bytecode is machine-independent and run on any machine with a Java interpreter the Java interpreter (part of JVM) further translates the bytecode into the machine language of the target machine once compiled programs can run on any platform (with JVM installed) without being recompiled
mechanisms, early checking for errors, etc. secure - several built-in security mechanisms multithreaded - can perform several tasks simultaneously
Where Java?
Desktop applications
Web applications Ebay, Amazon, AirAsia, klikBCA Mobile Devices: mobile phone, smartphone, Blackberry,
PDA Smart Card: Simcard, Credit Card, RFID Card (Contoh: E-Tol) Enterprise Applications SAP, JD Edward, PeopleSoft, Oracle Business Suite All machines & OS: windows, linux, mac, solaris
Java Platform
http://download.oracle.com/javase/6/docs/
menggunakan Java:
Tahap 1: Creating a program Tahap 2: Compiling the program into bytecodes Tahap 3: Loading the program into memory Tahap 4: Byte verification Tahap 5: Execution
menulis kode program. Kode program disimpan pada file berekstensi .java Editor yang dapat digunakan:
Editor teks sederhana: notepad, editPlus, Textpad, UltraEdit Editor teks berbasis Java: JGrasp, JCreator, DoctorJava Editor teks + diagram sederhana: BlueJ Integrated Development Environment (IDE):
Free:
NetBeans, Eclipse, Oracle JDeveloper Commercial: IntelliJ IDEA, IBM, BEA, Borland JBuilder
Tahap 2: Compiling
Pada tahap ini, kita compile kode program menjadi bytecodes
Kita membuat program dengan nama Welcome.java Perintah compile: javac Welcome.java Apabila proses compile berhasil, maka compiler akan menghasilkan sebuah file dengan ekstensi .class yaitu Welcome.class
JVM inilah yang akan mengeksekusi bytecodes ke dalam bahasa mesin JVM inilah yang menyebabkan Java begitu powerful, secure, scalable dan multi-platform/portable Bytecode ini dapat dieksekusi di mesin/platform apapun asalkan telah diinstal JVM yang dapat mengerti versi Java tempat bytecode tersebut dicompile sebelumnya
Tahap 3-5
Phase 3: Loading a Program into Memory
In Phase 3, the program must be placed in memory before it can execute a process known as loading. The class loader takes the .class files containing the program's bytecodes and transfers them to primary memory. The class loader also loads any of the .class files provided by Java that our program uses.
In Phase 4, as the classes are loaded, the bytecode verifier examines their bytecodes to ensure that they are valid and do not violate Java's security restrictions. In Phase 5, the JVM executes the program's bytecodes, thus performing the actions specified by the program.
Phase 5: Execution
Latihan 1: HelloMe.java
Berdasarkan program HelloWorld.java, buatlah
sebuah program baru bernama HelloMe.java Pada saat di-run, program harus menampilkan pesan sbb: Hello World, Niko! Note:
Ganti dengan nama Anda sendiri Biasakan beri NRP dan Nama anda (dalam bentuk comment)
println ?
Pembahasan Program
0: // Exercise 1: HelloMe.java 1: // NIK: 730015, NAMA: Niko Ibrahim 2: public class HelloMe { 3: public static void main (String args[ ]) { 4: System.out.println("Hello World, Niko!"); 5: } 6: } Line 0: Komentar program, judul program, keterangan penting. Line 1: Selalu tuliskan identitas penulis program. Line 2: - Nama kelas. Setiap program Java minimal memiliki 1 deklarasi kelas. - Kelas diawali huruf kapital untuk setiap kata. - Untuk men-save public class ke file, harus diberi nama sesuai dengan nama kelas tsb dan diakhiri dengan ekstensi .java. Tentang public akan dibahas nanti. Line 3: main method the starting point of every Java application Line 4: instruksi pada komputer untuk menulis suatu String (kata-kata)
untuk melakukan compile dan run program Java. Jalankan command window:
Start menu run ketik cmd Masuk ke direktori tempat Anda menyimpan file HelloMe.java
PERINTAH COMPILE
PERINTAH RUN
pilihan methods:
println posisi akhir kursor berada di garis yang baru (seolah menekan enter)
Contoh:
System.out.println("Hello World");
Karakter Khusus
Kita juga dapat memasukan karakter khusus ke dalam String.
Sequence
\n \t \r \ \\
Character
String
"Code: JSPS\nPrice: $50.25" "Niko\tIbrahim\r\Maranatha\tUniversity" "Type \"x\" to exit" "C:\\java\\files"
Result
Code: JSPS
Bits
Bytes
1 2 4 8 4 8 2 1
Minimum Range
-128 or -27 -32,768 or -215 -2,147,483,648 or -231 -263 -3.4E38 -1.7E308 n/a n/a
Maximum Range
127 or 27-1 32,767 or 215-1 2,147,483,647 or 231-1 263-1 3.4E38 1.7E308 n/a n/a
artinya kita harus mendeklarasikan nama variabel sebelum meng-assign suatu nilai Sintaks: tipeData namaVariabel Contoh:
variabel:
int counter = 1; boolean valid = false; char letter = A; char letter = 65; double distance = 3.65e+9;
final:
String di Java
String dapat berisi huruf, angka, dan karakter khusus String bukan merupakan tipe data primitif melainkan
tipe data reference Untuk menyambung (concat) dua/lebih string, gunakan operator + Syntax:
String variableName = value;
Examples:
String String String String String String message1 = "Invalid data entry"; message2 = " "; message3 = null; firstName = "Niko"; lastName = "Ibrahim"; name = firstName + " " + lastName;
Operator Aritmatika
Program seringkali membutuhkan berbagai operasi
Parentheses ( )
Kita dapat menggunakan tanda kurung dalam
melakukan operasi aritmatika. Contoh: a dikalikan dengan hasil penjumlahan antara b dengan c Yang manakah yang benar?
1)
2)
3)
a * b + c a * (b + c) (a * (b + c))
pertama kali!
(precedence)
*
/ %
Subtraction Multiplication
Division Modulus
2 2
2 2
Subtract the right operand from the left Multiplies the right operand and left operand
Divides the right operand into the left operand Returns the value that is left over after dividing the right operand into the left operand Adds 1 to the operand (x = x + 1)
++
Increment
-+
Decrement
Positive Sign
1
1
Negative Sign
int x = 14;
int y = 8; Operations Results
double a = 8.5;
double b = 3.4; Operations Results
int result1 = x + y;
int result2 = x y;
result1 = 22
result2 = 6
double result9 = a + b;
double result10 = a b;
result9 = 11.9
result10 = 5.1
int result3 = x * y;
int result4 = x / y;
result3 = 112
result4 = 1
double result11 = a * b;
double result12 = a / b;
result11 = 28.90
result12 = 2.5
int result5 = x % y;
int result6 = -y + x; int result7 = --y;
result5 = 6
result6 = 6 result7 = 7
int result8 = ++x; result8 = 15, x = 15 double result16 = ++b; Character Arithmetic: char letter1 = C; char letter2 = ++letter1; // letter1 = C // letter2 = D
-=
*=
/= %=
count += 1; // count -= 1; // total += 100.0; // total -= 100.0; // price *= .8; sum += nextNumber;//
count is increased by 1 count is decreased by 1 total is increased by 100.0 total is decreased by 100.0 // price is multiplied by 0.8 sum is increased by the value of nextNumber
console adalah dengan menggunakan Scanner class Untuk menampilkan output ke console adalah menggunakan method (fungsi) System.out.println Sebelum menggunakan Scanner class, kita harus melakukan proses import sbb:
import java.util.Scanner;
Untuk membaca input dari console, kita buat objek scanner dengan cara
menuliskannya sbb:
sesuai kebutuhan:
Method next() or nextLine() nextInt() nextDouble() nextBoolean() Description Reads a String value from the user Reads an integer value from the user Reads a double value from the user Reads a boolean value from the user
public class Addition { // main method begins execution of Java application public static void main( String args[] ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in );
int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user
sebenarnya berkorenpondensi dengan lokasi di memori komputer. Setiap variabel memiliki nama, tipe, ukuran, dan nilai Kode dari contoh 2:
number1 = input.nextInt(); // read first number from user
Pada saat kode tsb dieksekusi, nomor yang diketik oleh user akan disimpan ke suatu lokasi dimemori yang telah diberikan kepada number1 oleh compiler. Misal, user memasukkan angka 45, maka komputer akan menyimpan nilai integer ke lokasi number1 sbb:
Memory locations after calculating and storing the sum of number1 and number2.
Latihan 3: InvoiceApp.java
// Latihan 3: InvoiceApp.java import java.util.Scanner; public class InvoiceApp { public static void main (String args[]) { // create a Scanner object Scanner sc = new Scanner(System.in); // read a string System.out.print("Enter product code: "); String productCode = sc.next(); // read a double value System.out.print("Enter price: "); double price = sc.nextDouble(); // read an int value System.out.print("Enter quantity: "); int quantity = sc.nextInt(); // perform a calculation and display the result double total = price * quantity; System.out.println(); System.out.println(quantity + " " + productCode + " @ " + price + " = " + total); } }
Ekspresi Boolean
Ekpresi Boolean biasanya digunakan untuk mengatur control
statements. Ekpresi Boolean menghasilkan nilai true atau false. Ada 6 operator relational yang membandingkan operand bertipe data primitif dan menghasilkan nilai boolean. Dalam suatu ekpresi boolean, suatu operand dapat berupa literal, ekpresi aritmatika, maupun keyword true atau false.
Operator == != Equality Inequality Name Description Returns a true value if both operands are equals Returns a true value if the left and right operands are not equal
>
< >= <=
Greater Than
Returns a true value if the left operand is greater than the right operand Less Than Return a true value if the left operand is less than the right operand Greater Than Or Equal Returns a true value if the left operand is greater than or equal to the right operand Less Than or Equal Return a true value if the left operand is less than or equal to the right operand
discountPercent == 2.3 letter == y isValid == false subtotal != 0 years > 0 i < months subtotal >= 500
quantity <= reorderPoint
// // // // // // //
equal to a numeric literal equal to a char literal equal to the false value not equal to a numeric literal greater than a numeric literal less than a variable greater than or equal to a numeric literal // less than or equal to a variable
Example a && b a || b a ^ b !a
Description
true if both a and b are true true if either a or b (or both) is true true if only a or b is true true if a is not true
String Comparison
Karena String adalah suatu objek, bukan tipe data primitif,
kita tidak dapat menggunakan operator relational untuk membandingkannya. Kita harus menggunakan method equals atau equalgnoreCase yang dimiliki oleh kelas String. Contoh:
firstName.equals("Niko"); // equal to a string literal firstName.equalsIgnoreCase("Niko"); // equal to a string literal firstName.equals(""); // equal to an empty string !lastName.equals("Ibrahim"); // not equal to a string literal !code.equalsIgnoreCase(productCode); // not equal to another string variable firstName == null // equal to a null value firstName != null // not equal to a null value
Next Week!
Struktur Control: Looping
For
Branching
If
Konsep Arrays
Java Documentation