Rabu, 23 September 2009

Contoh-Contoh Program Java

Contoh Program Break
Syntax:
class TestBreak{
public static void main(String[] args){
System.out.println("Sebelum for");
for(int x=0;x<10;x++){
if(x==4)
break;
System.out.println("Nilai x : "+x);
}
System.out.println("Setelah For");
}
}

Output:
Sebelum for
Nilai x : 0
Nilai x : 1
Nilai x : 2
Nilai x : 3
Setelah for


Contoh Program Continue
Syntax:
class TestContinue{
public static void main(String[] args){
int x=10;
System.out.println("Sebelum while");
while(x<=20){
x++;
if(x%2==0)
continue;
System.out.println("Nilai x : "+x);
}
System.out.println("Sesudah while");
}
}

Output:
Sebelum while
Nilai x : 11
Nilai x : 13
Nilai x : 15
Nilai x : 17
Nilai x : 19
Nilai x : 21
Sesudah while


Contoh Program Return
Syntax:
class Orang{
public String cetakNama(){
return "Hello Nama Saya Anis!";
}
public static void main(String[] args){
Orang org = new Orang();
System.out.println("Sebelum panggil method");
System.out.println(org.cetakNama());
System.out.println("Sesudah panggil method");
}
}

Output:
Sebelum panggil method
Hello Nama Saya Anis!
Sesudah panggil method


Contoh Program Class
Syntax:
class Manusia{
String nama;
String jenkel;
byte usia;
String alamat;
}
class DemoManusia{
public static void main(String args[]){
Manusia m1,m2;
m1 = new Manusia();
m2 = new Manusia();

m1.nama="Mandala";
m1.jenkel="Laki-laki";
m1.usia=23;
m1.alamat="Jl.Kemiri Sari no.7 Salatiga";

m2.nama="Bella";
m2.jenkel="Perempuan";
m2.usia=23;
m2.alamat="Jl.Dahlia Raya no.5 Bogor";

System.out.println("Nama : "+m1.nama);
System.out.println("Jenkel : "+m1.jenkel);
System.out.println("Usia : "+m1.usia);
System.out.println("Alamat : "+m1.alamat);

System.out.println("Nama : "+m2.nama);
System.out.println("Jenkel : "+m2.jenkel);
System.out.println("Usia : "+m2.usia);
System.out.println("Alamat : "+m2.alamat);
}
}

Output:
Nama : Mandala
Jenkel : Laki-laki
Usia : 23
Alamat : Jl. Kemiri Sari no.7 Salatiga
Nama : Bella
Jenkel : Perempuan
Usia : 23
Alamat : Jl. Dahlia Raya no.5 Bogor


Contoh Program Method
Syntax:
class Manusia {
//Hak akses private
private String Nama;
private int TahunSekarang, TahunLahir, Umur;
//Getter Method untuk nama
public String GetNama()
{
return Nama;
}
//Setter Method untuk nama
public void SetNama(String N) {
Nama = N;
}
//Getter Method untuk tahun Sekarang
public int GetTahunSekarang() {
return TahunSekarang;
}
//Settter Method untuk tahun Sekarang
public void SetTahunSekarang (int TS) {
TahunSekarang = TS;
}
//Getter Method untuk tahun lahir
public int GetTahunLahir() {
return TahunLahir;
}
//Setter Method untuk tahun lahir
public void SetTahunLahir(int Tahun) {
if(Tahun < 1900)
TahunLahir = 1900;
else
TahunLahir = Tahun;
}
void Umur(){
Umur = TahunSekarang - TahunLahir;
System.out.println ("Umur = " + Umur);
}

}
class TesManusia{
public static void main(String[] args) {
// TODO code application logic here

Manusia data1=new Manusia();

data1.SetNama("Rara");
data1.SetTahunSekarang(2009);
data1.SetTahunLahir(1985);

System.out.println ("Nama = "+ data1.GetNama());
System.out.println ("Tahun Lahir = "+ data1.GetTahunLahir());

data1.Umur();
}
}

Output:
Nama = Rara
Tahun Lahir = 1985
Umur = 24
Powered By Blogger