09 April 2010

Method dan Constructor di Pemrograman Java

File 1: Persegi.java

public class Persegi {
int sisi;
public Persegi(int s) {
sisi = s;
}
//methode
public int getKelPersegi() {
return (4*sisi);
}
public int getLuasPersegi() {
return (sisi*sisi);
}
public void cetak (){
System.out.println("Keliling Persegi : "+getKelPersegi());
System.out.println("Luas Persegi : "+getLuasPersegi());

}
}


File 2: PersegiBeraksi.java

public class PersegiBeraksi {
public static void main (String[] args){
//inisiasi Object
Persegi objP = new Persegi(9);
objP.cetak();
}

}


Tampilan Outputnya :



1 comment: