Anda di halaman 1dari 56

Elementary Programming

Kelas E & F
DDP2 Semester Genap 2020/2021
Fasilkom UI
Referensi
[Buku Thinkjava] A. Downey and C. Mayfield,
Think Java 2nd Edition.

https://greenteapress.com/wp/think-java-2e/

[Buku Liang] Beberapa slides adalah slides yang


disediakan oleh buku: Liang, Introduction to Java
Programming, Tenth Edition, Global Edition,
Pearson education limited 2015 (02slide.ppt)
Variables
Tipe Data di Python
Ingat, semua data di python adalah object!

x = 5
y = "lima"
Tipe Data di Python
Ingat, semua data di python adalah object!

x = 5
y = "lima"
Memory
y : str

?
“lima”

x : int
5
Tipe Data di Python
Ingat, semua data di python adalah object!

x = 5
y = "lima"
Memory Memory
y : str str y
“lima”

x : int int
5 x

str int
5
“lima”

*Sebuah variable berisi pointer/reference ke object di lokasi lain.


Tipe Data di Java

Primitive data types
– Integer: byte, short, int, long
– Floating point: float, double
– boolean
– char

Reference/object data types
– String, Scanner, ...
– Dan yang lainnya, selain 8 tipe data primitif tersebut

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
Deklarasi sebuah variable
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
Deklarasi & Inisialisasi
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
double
5.0

Catatan:
Ingat, tipe data primitif
bukan object di Java!
[Baca Buku Thinkjava, page 17-20]
Tipe Data di Java
Inisialisasi variable num yang sudah dideklarasikan
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
double
4 5.0

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
double
Catatan:
4 5.0

String
Ingat, String adalah tipe str

object di Java! String


“halo”

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
double
4 6.0
5.0
String
str

String
“halo”

[Baca Buku Thinkjava, page 17-20]


Tipe Data di Java
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
Catatan: 4
double
6.0
5.0

String bersifat immutable!
String

Hati-hati, tidak semua object str
immutable!
String String
Ada juga yang mutable. “halo” “wow”

[Baca Buku Thinkjava, page 17-20] Akan dihapus garbage collector java
[Baca Buku Liang, page 67]
Tipe Data Primitif Java

byte -27 to 27 – 1 (-128 to 127) 8-bit two’s comp.

short -215 to 215 – 1 (-32768 to 32767) 16-bit two’s comp.

int -231 to 231 – 1 (-2147483648 to 2147483647) 32-bit two’s comp.

long -263 to 263 – 1 64-bit two’s comp.

float Negative range: -3.4028235E+38 to -1.4E-45 32-bit IEEE 754


Positive range: 1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
boolean true, false 1-bit

char ‘\u0000’ to ‘\uFFFF’ 16-bit unicode


Contoh: ‘A’ = ‘\u0042’, ‘a’ = ‘\u0061’
byte num = 127;
num = num + 1;
System.out.println(num);
Integer Overflow
byte num = 127;
-128
num = num + 1;
System.out.println(num);
Sign 0 artinya integer +

0 1 1 1 1 1 1 1 +127

0 0 0 0 0 0 0 1 +1

+
1 0 0 0 0 0 0 0
Sign 1 artinya integer -
Di representasi two’s complement, ini adalah -128
short hargaBuku = 50000;
hargaBuku += 1;
System.out.println(hargaBuku);
short hargaBuku = 50000;
hargaBuku += 1;
System.out.println(hargaBuku);

Compile Error:
Javac says “...error: incompatible types: possible lossy conversion
from int to short ...”
Double vs Float
The double type values are more accurate than the
float type values

System.out.println("1.0 / 3.0 is " + 1.0 / 3.0);


d isp la y s 1 . 0 / 3 . 0 i s 0 . 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

1 6 d ig its

System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F);


d isp la y s 1 . 0 F / 3 . 0 F i s 0 . 3 3 3 3 3 3 3 4
7 d ig its

[Slide Liang, Slide 30]


Round-off Errors
Note that Floating-point numbers are approximated,
and are not stored with complete accuracy.

System.out.println(1.0 – 0.1 – 0.1 – 0.1);


// shows 0.7000000000000001
// not 0.7

System.out.println(1.0 – 0.9);
// shows 0.0999999999999998
// not 0.1

[Slide Liang, Slide 25; Baca Buku Thinkjava, page 24-25]


Literals

int, byte, short
– 1, 34, 0, 5, -4, -456, -87
– Hexadecimal: 0x12, 0x4F
● long
– Dengan akhiran L atau l
– 34L, -769L, 8l, -12l

[Baca Buku Thinkjava, page 37-38]


Literals
● double
– 5., 0., 0.345, 34.56, -34.56
– Dengan akhiran D atau d: 123.D, -45.33d, 5.67D
– Scientific: 1E6 untuk 1000000, dan 2.96E-2 untuk 2.96 x 10-2
● float
– Dengan akhiran F atau f: 123.D, -45.33d, 5.67D
– Scientific: 1E3f untuk 1000, dan 2.96E-2f untuk 2.96 x 10-2
– float phi = 3.14; (X) (Compile Error)
– float phi = 3.14f; (O)

[Baca Buku Thinkjava, page 37-38]


Literals
● char
– Kutip tunggal: 'A', 'B', 'C', ..., 'a', 'b',
'c', ...
– Spesial: '\t' untuk tab, '\n' untuk newline, ...
– Unicode: '\u12FF', ...
● String
– Dengan kutip ganda
– "halo"

[Baca Buku Thinkjava, page 37-38]


Constants

Nilai di-assign sekali, dan tidak akan berubah.

Dengan reserved word final

● final double PI = 3.14;


● final int MAX_VALUE = 999;
● final double KILOMETERS_PER_MILE = 4.0;

[Baca Buku Thinkjava, page 37-38]


Identifiers

Identify variable, method, class

Sequence of characters that consists of letters, digits,
underscores (_), and dollar signs ($).

It must start with a letter, an underscore (_) or a dollar sign ($).

It cannot start with a digit.

It cannot be a reserved words.

It cannot be true, false, or null.

It can be of any length.

[Slide Liang, Slide 11]


Aturan Penamaan

Meaningful and descriptive names

Variables:
– age, aVariableName, numberOfStudents, ...

Methods:
– aMethodName(...), getHeight(...), ...

Classes:
– AClassName, Shape, Employee, BookStore...

Constants:
– PI, MAX_VELOCITY, ACCELERATION_OF_GRAVITY, ...
Reading Numbers from the Keyboard


Kita perlu import kelas Scanner
– import java.util.Scanner;

Buat sebuah object Scanner
– Scanner input = new Scanner(System.in);

Meminta input
– int value = input.nextInt();
Reading Numbers from the Keyboard
Keyword untuk menghidupkan

Kita perlu import kelas Scanner sebuah object dari sebuah
kelas.
– import java.util.Scanner;

Buat sebuah object Scanner
– Scanner input = new Scanner(System.in);

Meminta input
– int value = input.nextInt();
Object Scanner untuk parsing Sumber input: System.in
text streams artinya adalah standard input,
Method pada object Scanner, untuk input seperti keyboard
integer bertipe int
By the way ...
import java.util.Scanner; Object standard input
public class ProgramKu {
public static void main(String[] args) {
//membuat sebuah object Scanner
Scanner input = new Scanner(System.in);

//meminta input user


double value = input.nextDouble();

//tampilkan nilai
System.out.println("value = " + value);
}
} Object standard output
Reading Numbers from the Keyboard

Method Description

nextByte() reads an integer of the byte type.


nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.

[Slide Liang, Slide 20; Baca Buku Thinkjava 34-37]


Contoh / Demo

Program menghitung volume tabung

Program meminta input jari-jari
alas dan tinggi tabung
Kesalahan Umum: Redundant Input Objects

Scanner input = new Scanner(System.in);


System.out.print("Enter an integer: ");
int v1 = input.nextInt();
 
Scanner input1 = new Scanner(System.in);
System.out.print("Enter a double value: ");
double v2 = input1.nextDouble();

[Slide Liang, Slide 62]


Kesalahan Umum: Redundant Input Objects

Scanner input = new Scanner(System.in);


System.out.print("Enter an integer: ");
int v1 = input.nextInt();
 
Scanner input1 = new Scanner(System.in);
System.out.print("Enter a double value: ");
double v2 = input1.nextDouble();

Cukup sekali saja instansiasi object Scanner!


[Slide Liang, Slide 62]
Operators
Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2

[Slide Liang, Slide 21]


Integer Division

5 / 2 yields an integer 2

5.0 / 2.0 yields a double value 2.5

5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the
division)

[Adaptasi Slide Liang, Slide 22]


Precedence of Operators
Precedence of Operators
When two operators share an operand, the operator with the
higher precedence goes first*.
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53

*https://introcs.cs.princeton.edu/java/11precedence/ [Slide Liang, Slide 33]


Precedence of Operators
When an expression has two operators with the
same precedence, the expression is evaluated
according to its associativity.

x = y = z = 10;

val = 20 / 5 * 3 / 4;

[https://introcs.cs.princeton.edu/java/11precedence/]
Precedence of Operators
When an expression has two operators with the
same precedence, the expression is evaluated
according to its associativity.

x = y = z = 10; x = (y = (z = 10));
Sama seperti

val = 20 / 5 * 3 / 4; Sama seperti

val = ((20 / 5) * 3) / 4;
[https://introcs.cs.princeton.edu/java/11precedence/]
Augmented Assignment Operators

[Slide Liang, Slide 36]


Increment & Decrement Operators

Operator ini tidak ada padanannya di Python!

[Slide Liang, Slide 37]


Increment & Decrement Operators
int i = 10;
int newNum = 10 * i++;

int i = 10;
int newNum = 10 * ++i;

[Slide Liang, Slide 38]


Increment & Decrement Operators
int i = 10;
int newNum = 10 * i++;

int i = 10;
[Slide Liang, Slide 68-71]
int newNum = 10 * i;
i += 1;

int i = 10;
int newNum = 10 * ++i;
int i = 10;
i += 1;
int newNum = 10 * i;
[Slide Liang, Slide 38]
Casting Primitive Data Type
Widening Casting (Implicit)
byte -> short -> int -> long -> float -> double

double d = 3;
Widening Casting (Implicit)
byte -> short -> int -> long -> float -> double

double d = 3; Memory

d: double
3.0
Tipe: double
Tipe: int

Tipe: double
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte

double pi = 3.14;
int val = (int) pi;
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte

double pi = 3.14; Memory


int val = (int) pi; pi: double
3.14

Tipe: int
Tipe: double val: int
3
Casting Eksplisit
Fraction part is truncated!
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte

int val = 5 / 2.0;

... error: incompatible types: possible


lossy conversion from double to int
Conversion Rules
x1 op x2
dimana x1 dan x2 mempunyai tipe yang berbeda.

Java converts the operand based on the following rules:


– Jika salah satunya double, yang lain jadi double
– Else Jika .... float, ... float
– Else Jika .... long, ... long
– Else, kedua-duanya (x1 dan x2) diubah ke int

[Adaptasi Slide Liang, Slide 42]


Conversion Rules
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

[Adaptasi Slide Liang, Slide 41]


[Adaptasi Slide Liang, Slide 41]

Conversion Rules
3: int
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

i: byte -> int

> i (byte) * 3 (int) + 4 (int)


> i (int) * 3 (int) + 4 (int)
> 300 (int) + 4 (int)
> 304 (int)

Hasil dari operasi i * 3 + 4 kemudian di-casting ke tipe long


dan disimpan di variable k
[Adaptasi Slide Liang, Slide 41]

Conversion Rules
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

> i (byte) * 3.1 (double) + k (long) / 2 (int)


> i (double) * 3.1 (double) + k (long) / 2 (int)
> i (double) * 3.1 (double) + k (long) / 2 (long)
> 310.0 (double) + k (long) / 2 (long)
> 310.0 (double) + 152 (long)
> 310.0 (double) + 152.0 (double)
> 462.0
Variable d berisi 462.0
Augmented Expression ternyata mengandung
casting ...

x1 op= x2 <=> x1 = (T) (x1 op x2)


dimana T adalah tipe dari x1

int sum = 0;
sum += 4.5; //sum becomes 4 after this statement

Mengapa?
sum += 4.5; is equivalent to sum = (int)(sum + 4.5);

[Adaptasi Slide Liang, Slide 41]

Anda mungkin juga menyukai