LATIHAN PEMOGRAMAN JAVA

(1)

public class Hello

{

                /**

                 * Program Java My first

                 */

                public static void main( String[] args ){

                                                //menampilkan String Hello world pada layar

                                                System.out.println(“Hello world”);

                }

}

[/sourcecode language]

 

Output Program :

Hello world



 

(2)

public class ArithmeticDemo  {

     public static void main(String[] args){

          //beberapa angka

          int i = 37;

          int j = 42;

          double x = 27.475;

          double y = 7.22;

          System.out.println(“Nilai Variabel…”);

          System.out.println(”    i = ” + i);

          System.out.println(”    j = ” + j);

          System.out.println(”    x = ” + x);

          System.out.println(”    y = ” + y);
System.out.println(“Penjumlahan…”);

          System.out.println(”    i + j = ” + (i + j));

          System.out.println(”    x + y = ” + (x + y));

//pengurangan angka

          System.out.println(“Pengurangan…”);

          System.out.println(”    i – j = ” + (i – j));

          System.out.println(”    x – y = ” + (x – y));

          //pengalian angka

          System.out.println(“Pengalian…”);

          System.out.println(”    i * j = ” + (i * j));

          System.out.println(”    x * y = ” + (x * y));

           //pembagian angka

          System.out.println(“Pembagian…”);

          System.out.println(”    i / j = ” + (i / j));

          System.out.println(”    x / y = ” + (x / y));

//Penghitungan modulus (sisa pembagian)

       System.out.println(“Penghitungan modulus…”);

       System.out.println(”    i % j = ” + (i % j));

       System.out.println(”    x % y = ” + (x % y));

       //Penghitungan kombinasi variabel

       System.out.println(“kombinasi variabel…”);

       System.out.println(”    j + y = ” + (j + y));

       System.out.println(”    i * x = ” + (i * x));

    } 

Output Program:

Nilai variabel…

      i = 37
j = 42
x = 27.475

      y = 7.22
Penjumlahan…
i + j = 79
x + y = 34.695
Pengurangan…
i – j = -5
x – y = 20.255
Pengalian…
i * j = 1554
x * y = 198.37

Pembagian…
i / j = 0
x / y = 3.8054  Computing Nilai Modulus…
i % j = 37
x % y = 5.815 

 

 


Kombinasi variabel…
j + y = 49.22
i * x = 1016.58

 

(3)

public class RelationalDemo{

    public static void main(String[] args){

        //beberapa angka

        int i = 37;

        int j = 42;

        int k = 42;

        System.out.println(“Nilai variabel…”);

        System.out.println(”    i = ” +i);

        System.out.println(”    j = ” +j);

        System.out.println(”    k = ” +k);

        //lebih besar dari

        System.out.println(“Lebih besar…”);

        System.out.println(”    i > j = “+(i>j));//false

        System.out.println(”    j > i = “+(j>i));//true

        System.out.println(”    k > j = “+(k>j));//false

        //lebih besar atau sama dengan

       System.out.println(“lebih besar atau sama dengan…”);

       System.out.println(”    i >= j = “+(i>=j));//false

       System.out.println(”    j >= i = “+(j>=i));//true

       System.out.println(”    k >= j = “+(k>=j));//true

                //lebih kecil

       System.out.println(“lebih kecil…”);

       System.out.println(”    i < j = “+(i<j));//true

       System.out.println(”    j < i = “+(j<i));//false

       System.out.println(”    k < j = “+(k<j));//false

       //lebih kecil atau sama dengan

       System.out.println(“lebih kecil atau sama dengan…”);

       System.out.println(”    i <= j = “+(i<=j));//true

       System.out.println(”    j <= i = “+(j<=i));//false

       System.out.println(”    k <= j = “+(k<=j));//true 

       //sama dengan

     System.out.println(“sama dengan…”);

     System.out.println(”    i == j = ” + (i==j));//false

     System.out.println(”    k == j = ” + (k==j));//true

  //tidak sama dengan

     System.out.println(“tidak sama dengan…”);

     System.out.println(”    i != j = ” + (i!=j));//true

     System.out.println(”    k != j = ” + (k!=j));//false

    }

  } 

 

Output Program

Nilai variabel…

      i = 37

      j = 42

      k = 42

  Lebih besar…

      i > j = false

      j > i = true

      k > j = false

  Lebih besar atau sama dengan…

      i >= j = false

      j >= i = true

      k >= j = true

  Lebih kecil…

      i < j = true

      j < i = false

      k < j = false 

Lebih kecil atau sama dengan…

      i <= j = true

      j <= i = false

      k <= j = true

  Sama dengan…

      i == j = false

      k == j = true

  Tidak sama dengan…

      i != j = true

      k != j = false 

 

(4)

public class TestOR {

                 public static void main( String[] args ){

                                int  i  = 0;

                                int  j  = 10;

                                boolean test= false;

                                //contoh ||

                                test = (i < 10) || (j++ > 9);

                                System.out.println(i);

                                System.out.println(j);

                                System.out.println(test);

                                //contoh |

                                test = (i < 10) | (j++ > 9);

                                System.out.println(i);

                                System.out.println(j);

                                System.out.println(test);

                 }

}

 

Output program
 0
10
true
0
11
true

 

(5)

public class TestXOR {

                public static void main( String[] args ){

                                 boolean val1 = true;

                                 boolean val2 = true;

                                 System.out.println(val1 ^ val2);

                                 val1 = false; val2 = true;

                                 System.out.println(val1 ^ val2);

                                 val1 = false; val2 = false;

                                 System.out.println(val1 ^ val2);

                                 val1 = true; val2 = false;

                                 System.out.println(val1 ^ val2);

                }

}

 

Output Program :

false
true
false
true

 

(6)

public class ConditionalOperator {

                public static void main( String[] args ){

                                String  status = “”;

                                int grade = 80;

                                //Status dari siswa

                                status = (grade >= 60)?”Passed”:”Fail”;

                                //Cetak status

                                System.out.println( status );

                }

}

Output program:
Passed

 

 

Advertisements