Wednesday, 11 December 2013

Assalamu'alaikum wr. wb.
Berikut ini adalah program Java menghitung luas dan volume beberapa bangun menggunakan Overriding. Dalam hal ini, membuat empat baris program dan salah satunya adalah program tampil.

A. Baris Program Lingkaran
public class Lingkaran
{
protected double jari2;
protected double sudut;
public double luas(){
double ls;
ls=3.14*jari2*jari2;
return ls;
}
public double keliling(){
double kel;
kel= 2*3.14*jari2;
return kel;
}
public double juring(){
double jur;
jur= (sudut/360)*(3.14*jari2*jari2);
return jur;
}
public double setjari2(double r){
this.jari2=r;
return r;}
public double getjari2(){
return jari2;
}
public double setsudut(double s){
this.sudut=s;
return s;}
public double getsudut(){
return sudut;
}
}

B. Baris Program Tabung
public class Tabung extends Lingkaran
{
protected double tinggi;
public void settinggi(double t){
tinggi=t;
}
public double gettinggi(){
return tinggi;}
public double getvoltabung(){
double vol;
vol=super.luas()*tinggi;
return vol;
}
}

C. Baris Program Kue
public class kue extends Lingkaran
{
protected double tinggi;
public void settinggi(double t){
tinggi=t;
}
public double gettinggi(){
return tinggi;}
public double getvolkue(){
double vl;
vl=super.juring()*tinggi;
return vl;
}
}

D. Baris Program Tampil
public class tampil1
{
public static void main( String[] args ){
Tabung A= new Tabung();
kue B= new kue();
A.setjari2(7);
System.out.println("---Siyamto/B/111 111 0007--------------");
System.out.println("Menghitung Luas dan Keliling Lingkaran" );
System.out.println("---------------------------------------");
System.out.println("Jari2 lingkaran="+A.getjari2());
System.out.println("Luas Lingkaran="+A.luas());
System.out.println("Keliling Lingkaran="+A.keliling());
System.out.println(" ");
A.settinggi(2);
System.out.println("-------------------------");
System.out.println("Menghitung Volume Tabung" );
System.out.println("-------------------------");
System.out.println("Tinggi tabung="+A.gettinggi());
System.out.println("Volume tabung="+A.getvoltabung());
System.out.println(" ");
B.setjari2(10);
System.out.println("-------------------------");
System.out.println("Menghitung Luas Juring" );
System.out.println("-------------------------");
System.out.println("Jari2 Juring="+B.getjari2());
B.setsudut(30);
System.out.println("Sudut Juring="+B.getsudut());
System.out.println("Luas Juring="+B.juring());
System.out.println(" ");
B.settinggi(5);
System.out.println("-------------------------");
System.out.println("Menghitung Volume Kue" );
System.out.println("-------------------------");
System.out.println("Tinggi Kue="+B.gettinggi());
System.out.println("Volume Kue="+B.getvolkue());

}
}

Screenshoot

Sekian yang dapat saya sampaikan tentang Java Overriding, semoga bermanfaat. Wassalamu'alaikum wr.wb.

0 comments:

Post a Comment