Anda di halaman 1dari 10

TugasPersonal ke-2

Minggu 4
Review Questions:
1. Assume that int a=1 and double d=1.0, and that each expression is independent. What are
the results of the following expressions?
a. a = 46 / 9
a=5
b. a = 46 % 9 + 4 * 4 2
a = 15
c. a = 45 + 43 % 5 * (23 * 3 % 2)
a = 48
d. a %= 3 / a + 3
a=1
e. a = 4 + d * d + 4
error! karena integer tidak bisa menampung perhitungan angka yang terdiri dari type
double.
Jika mengesampingkan error, hasilnya menjadi a = 9
2. Evaluate the following method calls:
a. Math.pow(2,2)
4
b. Math.max(2, Math.min(3,4))
3
c. Math.round(2.5F)
3
d. Math.ceil(-9.49)
-9
e. Math.floor(7.5)
7
3. Are the following two statements equivalent? Give your explanation!

if (income <= 10000)


Tax = income * 0.1;
else if (income <= 20000)
Tax = 1000 +
(income 10000) * 0.15;

if (income <= 10000)


Tax = income * 0.1;
else
if(income > 10000 && income <=20000)
Tax = 1000 +
(income 10000) * 0.15;

Setara, dari statement terlihat perbedaan pada kondisi else if yang di bagian kanan
menyebutkan nilai minimal yaitu harus lebih dari 10000 sedangkan yang kiri tidak
menyebutkan minimal. Tetapi karena kondisi if yang pertama sudah melakukan kondisi
jika lebih kecil atau sama dengan 10000 maka tax = income * 0.1. Jika if tersebut tidak
terpenuhi sudah pasti income melebihi 10000 atau dengan kata lain, kondisi minimal yang di
tulis else if yaitu income > 10000 tidak akan berpengaruh sama sekali.
4. What is wrong in the following code?
if(score >= 60.0)
Grade = D;
else if(score >= 70.0)
Grade = C;
else if (score >= 80.0)
Grade = B;
else if(score >= 90.0)
Grade = A;
else
Grade = F;
Kesalahannya adalah argoritma perhitungannya di mulai dari kondisi paling rendah yaitu
score lebih besar di 60. Jika kondisi ini diterapkan, nilai di atas 90 yang harusnya
mendapat grade A menjadi D karena pengkondisian yang salah, akan terkondisi di if
pertama yaitu (90 >= 60 is true), if akan berhenti jika kondisi nya telah terpenuhi dan
mengabaikan else. Kurang lebih seperti itu.
5. Answer the following questions:
a. Write a Boolean expression that evaluates to true if a number stored in variable num
is between 1 and 100.
import java.util.Scanner;

class Boolean
{
static boolean hasil;
static int num;
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.print("Masukkan angka yang anda suka = ");
num = input.nextInt();
if(( num == 1 ) || ( num == 100 )){
hasil = true;
}
else{
hasil = false;
}
System.out.println(hasil);
}
}
b. Write a Boolean expression that evaluates to true if a number stored in variable num
is between 1 and 100 or the number is negative.
import java.util.Scanner;
class Boolean
{
static boolean hasil;
static int num;
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.print("Masukkan angka yang anda suka = ");
num = input.nextInt();
if(( num == 1 ) || ( num == 100 ) || ( num < 0 )){
hasil = true;

}
else{
hasil = false;
}
System.out.println(hasil);
}
}

Programming Exercise:
Please create a small program follow these criteria:
1. The program will show the menus, which are Mathematic Test for the first menu option
and Boolean Operator Test for the second menu option.
2. If the user choose menu 1:
a. The program will generate two random single-digit integers into number 1 and
number 2.
b. The program will swap the number if number 2 is bigger than number 1.
c. Prompt the user to answer what is number 1 number 2?
d. After the program received the users answer, display whether the answer is correct.
What is 6 6? 0(enter)
You are correct!
What is 9 2? 5(enter)
Your answer is wrong
9 2 should be 7
3. If the user choose menu 2:
a. A user is required to insert an integer.
b. The program will check and show the result whether the inserted number is divisible
by both 3 and 4, or neither of them, or just one of them. Here are some sample runs:
Enter an integer: 16
16 is divisible by 3 or 4, but not both.
Enter an integer: 24
24 is divisible by both 3 or 4.
Enter an integer: 7
7 is not divisible by either 3 or 4.

Please run the EXE file to see the sample of the program.
Script

import java.util.Scanner;
class tugas3
{
static int menu, number1, number2, hasil, jawab, numbTemp;
static boolean test1, test2;
public static void main (String args[]){
System.out.println("Select menu = ");
System.out.println("1. Mathematic Test");
System.out.println("2. Boolean Operator Test");
Scanner input = new Scanner (System.in);
System.out.print("Chhose number = ");
menu = input.nextInt();
// MENU PILIHAN 1 //
if( menu == 1){
System.out.println("== Mathematic Test ==");
System.out.println("* The program will generate two random singledigit");
System.out.println("* Your task is to answer the reduction of these two
numbers");
number1 = 0 + (int)(Math.random() * 10);
number2 = 0 + (int)(Math.random() * 10);
if (number2 > number1){
numbTemp = number2;
number2 = number1;

number1 = numbTemp;
}
System.out.println("What is "+ number1 +" - "+ number2 +" = ");
jawab = input.nextInt();
hasil = number1 - number2;
if(jawab == hasil){
System.out.println("You are correct");
}
else{
System.out.println(number1 +" - "+ number2 +" should be
"+ hasil);
}
}
// MENU PILIHAN 2 //
else if( menu == 2 ){
System.out.println("== Boolean Operator Test ==");
System.out.println("* you are required to enter a number");
System.out.println("* the program will determine whether the numbers
given can be divisible by the number 3 or 4 ");
System.out.print("Please input your number = ");
jawab = input.nextInt();
number1 = jawab % 3;
number2 = jawab % 4;
if( number1 == 0 ){
test1 = true;
}else{
test1 = false;
}

if( number2 == 0 ){
test2 = true;
}else{
test2 = false;
}
if((test1 == true) && (test2 == true)){
System.out.println(jawab +" is divisible by both 3 or 4.");
}else if((test1 == false) && (test2 == false)){
System.out.println(jawab +" is not divisible by either 3 or
4.");
}else{
System.out.println(jawab +" is divisible by 3 or 4, but not
both.");
}
}
// MENU PILIHAN SELAIN 1 DAN 2 //
else{
System.out.println("Menu doesn't exist");
}
}
}
Hasilnya
Tampilan pertama program

Tampilan, jika user memilih menu 1

Hasil nya jika benar

Hasilnya jika salah

Tampilan kedua jika user memilih menu 2

Hasilnya jika inputan dari user jika dapat di bagi dengan 3 dan 4

Hasilnya jika inputan dari user jika dapat di bagi dengan 3 atau 4

Hasilnya jika inputan dari user jika tidak dapat di bagi dengan 3 atau 4

Anda mungkin juga menyukai