Tugas Mandiri Algoritma

Mahasiswa Yang Berencana, Dosen Yang Menentukan.

Tugas Mandiri 04C

March28

1. Suppose x = 3 and y = 2, show the output, if any, of the following code.What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2?

if (x > 2) {

  if (y > 2) {

    int z = x + y;

}  }    System.out.println(“z is ” + z);

else

  System.out.println(“x is ” + x);

Output dari x = 3 & y = 2 tidak memberikan output

Hasil Dari Textpad

Output dari x = 3 & y = 4 memberikan output “z is 7”

Hasil Dari Textpad

Output dari x = 2 & y = 2 memberikan output “x is 2”

Hasil Dari Textpad

2. What is after the following switch statement is executed ?

x = 3; y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}

Hasil y adalah  “y = 2”

Hasil Dari Textpad

3. Use a switch statement to rewrite the following if statement:?

if (a ==1)
x +=5;
else if (a == 2)
x +=10;
else if (a == 3)
x +=16;
else if (a == 4)
x += 34;

Hasil dari if statement yang dirubah menjadi switch statement

Hasil Dari Textpad

4. Use a ternary operator to rewrite the following if statement

if(x>65)
System.out.println(“Passed”);
else
System.out.println(“Failed”);

Hasil dari Nilai 100 “Passed”

Hasil Dari Textpad

Hasil dari Nilai 65 “Passed”

Hasil Dari Textpad

Hasil dari Nilai 50 “Failed”

Hasil Dari Textpad

www.binus.ac.id

Tugas Mandiri 04B

March27

1. Assuming that x is 1, show the result of the following Boolean expressions.

  • (true) && (3 > 4)

  • !(x > 0) && (x > 0)

  • (x != 1) == !(x == 1)

  • (x >= 0) || (x < 0)

Output dari soal diatas adalah :

  • (true) && (3 > 4) = False

  • !(x > 0) && (x > 0) = False

  • (x != 1) == !(x == 1) = True

  • (x >= 0) || (x < 0) = True

2. List the precedence order of the Boolean operators. Evaluate the following expressions :

  • 2 * 2 – 3 > 2 && 4 – 2 > 5
  • 2 * 2 – 3 > 2 || 4 – 2 > 5

Output dari soal diatas adalah :

  • 2 * 2 – 3 > 2 && 4 – 2 > 5  = False

  • 2 * 2 – 3 > 2 || 4 – 2 > 5     = False

3. Is (x > 0 && x < 10) the same as ((x > 0) && (x < 10))? Is (x > 0 || x < 10 && y < 0) the same as (x > 0 || (x < 10 && y < 0)) ?

  • Is (x > 0 && x < 10) dengan ((x > 0) && (x < 10)) Adalah Same

  • (x > 0 || x < 10 && y < 0) dengan  (x > 0 || (x < 10 && y < 0)) Adalah Same

www.binus.ac.id

Tugas Mandiri 04A

March27

1. Can different types of numeric values be used together in computation?

Bisa, karena menggunakan type casts. Yaitu untuk menggabungkan berbagai jenis numerik (angka) secara bersama dengan melakukan pemisahan. Hasilnya akan sesuai dengan input, jika ada nilai awal yang diberikan maka bisa menggantikan beberapa tipe yang juga digunakan.

2.  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 = 46 % 9 + 4 * 4 – 2

  • a = 45 + 43 % 5 * (23 * 3 % 2)

  • a %= 3 / a + 3

  • d += 1.5 * 3 + (++a)

Output dari soal diatas adalah :

  • a = 46 % 9 + 4 * 4 – 2 = 15

  • a = 45 + 43 % 5 * (23 * 3 % 2) = 48

  • a %= 3 / a + 3 = 0

  • d += 1.5 * 3 + (++a) = 6.5

Hasil Dari Textpad

3. Are the following statements correct? If so, show the output.

  • System.out.println(“25 / 4 is “ + 25 / 4);
  • System.out.println(“25 / 4.0 is “ + 25 / 4.0);
  • System.out.println(“3 * 2 / 4 is “ + 3 * 2 / 4);
  • System.out.println(“3.0 * 2 / 4 is “ + 3.0 * 2 / 4);

Output dari soal diatas adalah :

  • System.out.println(“25/4 is ” +25/4); = 25/4 is 6

  • System.out.println(“25/4.0 is ” +25/4.0); = 25/4.0 is 6.25

  • System.out.println(“3*2/4 is ” +3*2/4); = 3*2/4 is 1

  • System.out.println(“3.0*2/4 is ” +3.0*2/4); = 3.0*2/4 is 1.5

Hasil Dari Textpad

www.binus.ac.id