Modul Penyeleksian

Transcription

Modul Penyeleksian
Judul
TIU
TIK
Materi
Modul Penyeleksian
Ganjil 2014/2015
Mahasiswa memahami Konsep Penyeleksian
1. Mahasiswa mampu menggunakan perintah dalam percabangan
2. Mahasiswa mampu menggunakan perintah dalam percabangan bersarang
A conditional is a programming statement executed only if a specific condition is met. The most
basic conditional in Java is the if keyword. The if conditional uses a Boolean expression to
decide whether a statement should be executed. If the expression produces a true value, the
statement is executed.
Using if, you can include only a single statement as the code to execute if the test expression is
true and another statement if the expression is false.
Example
The if statement in this example contains the test expression temperature > 660. If the
temperature variable contains a value higher than 660, the block statement is executed, and
two things occur:
The status variable is given the value returning home.
- The speed variable is set to 5.
If the temperature variable is equal to or less than 660, the entire block is skipped, so nothing
happens.
switch Conditionals
A common programming practice is to test a variable against a value, and if it doesn’t match,
test it again against a different value, and so on.
This approach can become unwieldy if you’re using only if statements, depending on how many
different values you have to test. For example, you might end up with a set of if statements
something like the following:
if (operation == ‘+’)
add(object1, object2);
else if (operation == ‘-’)
subtract(object1, object2);
else if (operation == ‘*’)
multiply(object1, object2);
else if (operation == ‘/’)
divide(object1, object2);
In Java, you can group actions together with the switch statement. The following example
demonstrates switch usage:
1/4
Praktikum Alpro – Modul 2
A switch statement is built on a test variable; in the preceding example, the variable is the value
of the grade variable, which holds a char value. The test variable, which can be the primitive
types byte, char, short, or int, is compared in turn with each of the case values. If a match is
found, the statement or statements after the test are executed.
If no match is found, the default statement or statements are executed. Providing a default
statement is optional—if it is omitted and there is no match for any of the case statements, the
switch statement might complete without executing anything.
The Java implementation of switch is limited—tests and values can be only simple primitive
types that can be cast to an int. You cannot use larger primitive types such as long or float,
strings, or other objects within a switch, nor can you test for any relationship other than
equality. These restrictions limit switch to the simplest cases. In contrast, nested if statements
can work for any kind of test on any possible type.
Another example
2/4
Praktikum Alpro – Modul 2
The break statement included with each case section determines when to stop executing
statements in response to a matching case. Without a break statement in a case section, after a
match is made, the statements for that match and all the statements further down the switch
are executed until a break or the end of the switch is found
Tugas
1. Buatlah flowchart/pseudocode dan program dalam java untuk menghitung akar-akar
Pendahuluan
persamaan
Y  A* X 2  B * X  C
Untuk mendapatkan akar-akar persamaan X1, X2, maka perlu dicek terlebih dahulu nilai
diskriman persamaan tersebut menggunakan persamaan
Disk  B 2  4 * A * C
X 1, 2

Disk  0

 Disk  0

  b  Disk 
X1  

 2* A 
  b Disk * (1)

X1  

& "i" 
2* A
 2* A

  b  Disk 
X2 

 2* A 
  b Disk * (1)

X2  

& " i" 
2* A
 2* A

Tanda & menghubungkan nilai yang berada disebelah kiri dan disebelah kanannya
2. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output nilai
dalam bentuk karakter dan predikat dari nilai yang dicapainya dengan menggunakan aturan
berikut ini
No
1
2
3
4
5
Batas Bawah
0
55.0001
65.0001
75.0001
85.0001
Batas Atas
55
65
75
85
100
Nilai Huruf
E
D
C
B
A
Predikat
Very bad
Bad
Fair
Good
Excellent
3. Sebuah perusahaan menerapkan upah harian terhadap beberapa karyawannya dengan
aturan perhitungan gaji berdasarkan jumlah jam
Jika dalam satu hari jumlah jam masuknya lebih dari 8 jam, maka kelebihannya dinilai Rp.
100.000 sedangkan yang standart 8 jam dinilai Rp. 75.000
Buatlah flowchart/pseudocode dan program dalam java untuk menghitung gaji dalam
perusahaan tersebut.
Tugas
Praktikum
Waktu
Praktikum
3/4
Buat program dalam java untuk soal no 1 atau no 2 atau no 3 (di tentukan oleh asisten, setiap
kelompok berbeda)
2 x 50 menit
Praktikum Alpro – Modul 2
Referensi
4/4
1. Java for Dummies, Barry Burd, Wiley Publishing, Inc, 2007
2. Sam Teach Yourselft, Java 6 in 21 Days, Rogers Cadenhead & Laura Lemay, SAMS, 2007
Praktikum Alpro – Modul 2