Algorithm and Programing assignment 6

1.Describe the Java Throwable class, its subclasses, and the types of exceptions

2.What is the purpose of declaring exceptions? How do you declare an exception, and where? Can you declare multiple exceptions in a method header?

3.What is a checked exception and an unchecked exception?

4.What is the keyword throw and throws used for?

5.Point out the problem in the following code. Does the code throw any exceptions?

Picture1

6.Suppose that statement2 causes an exception in the following try-catch block:

Picture2

Answer the following questions:

−Will statement3 be executed?

−If the exception is not caught, will statement4 be executed?

−If the exception is caught in the catch block, will statement4 be executed?

−If the exception is passed to the caller, will statement4 be executed?

7.What is displayed when the following program is run?Picture3

 

Answer :

 

1. Throwable adalah kelas yang ada di package java.lang yang menangani eksepsi dan merupakan turunan dari kelas objek (kelas tertinggi di JAVA). Fungsi code ini akan akan dijalankan jika terjadi eksepsi saja.Subclasses dari Error adalah LinkageError, VirtualMachineError, and AWTError. subclasses dari Exception ialah RuntimeException, IOException, AWTException, dan InstantiationException.

Tipe-tipe Exception sangat banyak, seperti :

ArithmeticException   : Arithmetic error, such as divide by zero. ArrayIndexOutOfBoundsException : Array index is out-of-bounds. ClassCastException :  Invalid cast. StringIndexOutOfBounds  :  Attempt to index outside the bounds of a string. NullPointerException  : Invalid use of a null reference. IndexOutOfBoundsException : Some type of index is out-of-bounds.

 

2. Tujuan dari declare exception adalah untuk memberitahu sistem runtime Java apa yang bisa terjadi kesalahan. Declare exception  dapat menggunakan throws keyword dalam method declaration.

Untuk melakukkan declare multiple exception dapat dilakukan dengan cara memisahkannya dengan koma.

 

3. Checked exceptions adalah exception yang diperiksa oleh Java compiler. Compiler memeriksa keseluruhan program apakah menangkap atau mendaftar exception yang terjadi dalam sintaxthrows. Apabila checked exception tidak didaftar ataupun ditangkap, maka compiler error akan ditampilkan.
Tidak seperti checked exceptions, unchecked exceptions tidak berupa compile-time checking dalam penanganan exceptions. Pondasi dasar dari unchecked exception classes adalah Error, RuntimeException dan subclass-nya.

 

4. Keyword throw digunakan untuk melempar suatu eksepsi dalam program. keyword throws digunakan pada waktu mendeklarasikan suatu method untuk memberitahu bahwa method yang bersangkutan dapat melempar eksepsi dengan tipe yang dideklarasikan oleh keyword throws tersebut.

 

5. Code tersebut tidak digunakan sebagai pengecualian dalam Java. Jika panjang nilai maksimal yang ditambahkan tidak melebihi panjang nilai itu sendiri.

 

6. Menggunakan pengecualian pada try catch berikut :

–          Statement 3 tidak akan dilakukan eksekusi/ tidak dijalankan.

–          Jika pengecualian tidak ada pada catch, maka statement 4 tidak akan dilakukan eksekusi/tidak dijalankan.

–          Jika pengecualian ada pada catch, maka statement 4 akan dilakukan eksekusi.

–          Jika pengecualian diberikan kepada pemanggil (yang melakukan eksekusi), maka statement4 tidak akan dilalukan eksekusi.

 

7. Ketika fungsi mainnya diubah, maka Output yang keluar akan mengeluarkan exception : “ArithmeticException”

 

 

BINUS University – www.binus.ac.id

Posted in Uncategorized | Leave a comment

Algorithm and Programing Assignment 5

1. What are the differences between a while loop and a do-whileloop? Convert the following while loop into a do-while loop.

soal3

soal2

soal

2. Do the following two loops result in the same value in sum?

for2

for

3.  What does the following statement do?

for( ;  ; ){

     do something;

}

4.  Can you always convert a while loop into a for loop? Convert the following while loop into a for loop.

soal4

5.  After the continue statement is executed in the following loop, which statement is executed? Show the output.

soal5

Answer :
Continue reading

Posted in Uncategorized | Leave a comment

Tugas Algoritma 4C

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);

 

2. What is y after the following switch statement is executed?
x = 3; y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}

 

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;

4. Use a ternary operator to rewrite the following if statement
if(x>65)
System.out.println(“Passed”);
else
System.out.println(“Failed”);

Jawaban :

 

1. Input dari x = 3 and y = 2 tidak memberikan Output, atau dengan kata lain output dilewati sehingga menghasilkan layar yang kosong.

tg1

Input dari if x = 3 and y = 4 memberikan output “z is 7”

tg2

 

Input dari if x = 2 and y = 2 menghasilkan output “x is 2”.

tg3

 

 

2. Hasil akhir y adalah y=2

 

3. switch (a) {
case 1: x+=5;
break;
case 2: x+=10;
break;
case 3: x+=16;
break;
case 4: x+=34;
break;
}

 

4.  System.out.println(x>60 ? “Passed” : “\nFailed” );

 

Binus University – www.binus.ac.id

Posted in Uncategorized | Leave a comment

Tugas Algoritma 4b

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)

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

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))?

 

Jawaban :

1. Jawaban :

  • Bagian kedua false dikarenakan 3 tidak lebih besar dari 4.
  • False, bagian pertama benar, tetapi statement bagian kedua salah.
  • True, statement pertama dan kedua benar.
  • True, statement pertama dan kedua benar.

 

2. Jawaban :

  • True, statement pertama dan kedua benar.
  • True, statement pertama dan kedua benar.

 

3. Jawaban:

(x > 0 && x < 10) the same as ((x > 0) && (x < 10)) = statement ini menghasilkan nilai yang sama.

(x > 0 || x < 10 && y < 0) the same as (x > 0 || (x < 10 && y < 0)) = statement ini  menghasilkan nilai yang sama juga.

Kedua-dua dari statement diatas dapat dijalankan.

 

 

Binus University – www.binus.ac.id

Posted in Uncategorized | Leave a comment

Tugas Pertemuan 4A

Saya akan mencoba menjawab pertanyaan berikut :

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

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)

 

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);

Jawaban :

 

1. Menurut saya bisa ya dan bisa tidak. Ketika kita menggunakkan tipe-tipe nilai numerik, kita bisa menggunakan float dan double bersamaan, tetapi untuk input yang berbeda, mungkin float hanya untuk  input dalam perhitungan dalam bilangan kecil, dan double dalam hitungan besar seperti uang. Dan berlaku untuk beberapa nilai numerik pula.

 

2. Output dari :

  • 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

tugas

 

3. Output dari :

  • 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.5tugas1

 

Binus University – www.binus.ac.id

Posted in Uncategorized | Leave a comment

Algorithm and Programming Assignment 3b

Kali ini kita akan mengulas pertanyaan berikut, mari belajar bersama :

  1. Does any method in the String class change the contents of the string?
  2. Create a program to determine whether a character is alphanumeric?
  3. Create a program to determine whether a character is in lowercase or uppercase?
  4. Describe how to convert hexadecimal to decimal?
  5. Evaluate the following method calls:
  6. Math.pow(2,2)
  7. Math.max(2, Math.min(3,4))
  8. Math.round(2.5F)
  9. Math.ceil(-9.49)
  10. Math.floor(7.5)
  11. Describe and give example for each method of String
  • contains
  • concat
  • compareTo
  • format
  • charAt
  • replace
  • substing
  • trim
  • toCharArray
  • split
  • toLowerCase
  • toUpperCase

Continue reading

Posted in Uncategorized | Leave a comment

Algorithm and Programming Assignment 2

Kali ini saya akan mencoba menjawab pertanyaan algoritma yang kedua ini, mari simak pertanyaan dibawah berikut :

  1. Why does the System class not need to be imported?
  2. What is the differences between the following two import statements:
  • import java.util.Scanner;
  • import java.util.*;

 

3. Explain the output syntax:

  • System.out.println
  • System.out.print
  • System.out.printf

4. If a NoClassDefFoundError occurs when you run a program, what is the cause of the error?

5. If a NoSuchMethodError occurs when you run a program, what is the cause of the error?

6. How to compile and run program in Java using command prompt?

7. Identify and fix the errors in the following code:

public class Sample {
public static void main (String[]args){
System.out,printl(“Welcome to Java!”)
}
}
Continue reading

Posted in Uncategorized | Leave a comment

Algorithm and Programming Assignment

Di artikel pertama saya ini, kali ini saya akan menjelaskan pengertian dari beberapa pertanyaan di bawah ini :

  1. Describe about library in java!
  2. Specify the rules that can be used to create identifier!
  3. What is the function of keywords in java:
    1. static
    2. final
    3. class
    4. package
  4. Explain the usage of “String[] args” in public static void main!
  5. What is JVM, JDK, source program, and compiler?

Continue reading

Posted in Uncategorized | Leave a comment

Hello world!

Welcome to Binusian blog.
This is the first post of any blog.binusian.org member blog. Edit or delete it, then start blogging!
Happy Blogging 🙂

Posted in Uncategorized | 1 Comment