Tugas Pemrograman Berbasis Objek (Java)

1. Contoh penggunaan keyword Break..

public class Main {

public static void main(String[] args) {

int i=0;

do {

i++;

System.out.print(i);

System.out.print(” “);

if(i==9) break;             // keyword Break..

} while(i<=10);

}

}

OUTPUT

1 2 3 4 5 6 7 8 9

2. Contoh penggunaan keyword Continue…

public class Main {

public static void main(String[] args) {

int i=0;

int j=1;

do {

i++;

System.out.print(“bilangan ganjil ke ” +(i));

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

j=j+2;

if (j==0) continue;                  // keyword Continue

} while (j<=13);

}

}

OUTPUT

bilangan ganjil ke 1 = 1

bilangan ganjil ke 2 = 3

bilangan ganjil ke 3 = 5

bilangan ganjil ke 4 = 7

bilangan ganjil ke 5 = 9

bilangan ganjil ke 6 = 11

bilangan ganjil ke 7 = 13

3. Contoh penggunaan keyword Return, method Getter, method Setter…

class Hitung {                // penggunaan Class

private int a, b;

public Hitung(){

a = 0;

b = 0;

}

private int Penjumlahan (int a, int b){

return a+b;                        // keyword Return

}

private int Pengurangan (int a, int b){

return a-b;                       // keyword Return

}

private int Perkalian (int a, int b){

return a*b;                      // keyword Return

}

private int Pembagian (int a, int b){

return a/b;                      // keyword Return

}

public void setA (int a) {     // method setter

this.a = a;

}

public void setB (int b) {    // method setter

this.b=b;

}

public int getJumlah (){             //method getter

return Penjumlahan (a,b);        // keyword Return

}

public int getKurang (){             //method getter

return Pengurangan (a,b);        // keyword Return

}

public int getKali (){                    //method getter

return Perkalian (a,b);               // keyword Return

}

public int getBagi (){                 //method getter

return Pembagian (a,b);          // keyword Return

}

public static void main(String[] args) {

Hitung h = new Hitung();

h.setA(25);                              //method setter

h.setB(5);                                //method setter

System.out.println(“A + B = ” + h.getJumlah());    //method getter

System.out.println(“A – B = ” + h.getKurang());     //method getter

System.out.println(“A * B = ” + h.getKali());           //method getter

System.out.println(“A / B = ” + h.getBagi());          //method getter

}

}

OUTPUT

A + B = 30

A – B = 20

A * B = 125

A / B = 5

* Output Printscreen…

Penggunaan keyword Break..

Penggunaan keyword Break..

Penggunaan keywoard Continue..

Penggunaan keywoard Continue..

Penggunaan keywoard Return, method Getter, method Setter..

Penggunaan keyword Return, method Getter, method Setter..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: