public class Driver{
public static void main(String[]args){
Balok b= new Balok("BALOK",10,5,2);
Tabung t= new Tabung("TABUNG",7,10);
BolaPejal bp= new BolaPejal("BOLA",14);
b.display();
t.display();
bp.display();
b.ubahTinggi(3);
t.ubahTinggi(5);
bp.ubahTinggi(28);
b.display();
t.display();
bp.display();
}
}
public class Tabung extends TigaDimensi{
private double r;
private String nama;
public Tabung(String nama, double r, double t){
this.r=r;
this.t=t;
this.nama=nama;
}
@Override
public double luasPermukaan(){
double tot;
tot=((2*(phi*r*r))+(t*(phi*2*r)));
return tot;
}
@Override
public double volume(){
double tot;
tot=(phi*r*r*t);
return tot;
}
@Override
public String jumlahSisi(){
return "Tiga";
}
@Override
public void display(){
System.out.println("Tabung "+this.nama);
System.out.println("Jari-jari :"+this.r);
System.out.println("Tinggi :"+this.t);
System.out.println("Luas Permukaan :"+this.luasPermukaan());
System.out.println("Volume :"+this.volume());
System.out.println();
}
}
public class BolaPejal extends TigaDimensi{
private double r;
private String nama;
public BolaPejal(String nama, double t){
this.t=t;
this.r=t/2;
this.nama=nama;
}
@Override
public double luasPermukaan(){
double tot;
tot=(4*phi*r*r);
return tot;
}
@Override
public double volume(){
double tot;
tot=(4/3*phi*r*r*r);
return tot;
}
@Override
public String jumlahSisi(){
return "~";
}
@Override
public void ubahTinggi(double t){
this.t=t;
this.r=t/2;
System.out.println("Ubah tinggi/diameter berhasil");
}
@Override
public void display(){
System.out.println("Bola Pejal "+this.nama);
System.out.println("Jari-jari :"+this.r);
System.out.println("Luas Permukaan :"+this.luasPermukaan());
System.out.println("Volume :"+this.volume());
System.out.println();
}
}
public class Balok extends TigaDimensi{
private double p;
private double l;
private String nama;
public Balok(String nama, double p, double l, double t){
this.p=p;
this.l=l;
this.t=t;
this.nama=nama;
}
@Override
public double luasPermukaan(){
double tot;
tot=(((p*t)+(p*l)+(t*l))*2);
return tot;
}
@Override
public double volume(){
double tot;
tot=(p*t*l);
return tot;
}
@Override
public String jumlahSisi(){
return "Enam";
}
@Override
public void display(){
System.out.println("Balok "+this.nama);
System.out.println("Panjang :"+this.p);
System.out.println("Lebar :"+this.l);
System.out.println("Tinggi :"+this.t);
System.out.println("Luas Permukaan :"+this.luasPermukaan());
System.out.println("Volume :"+this.volume());
System.out.println();
}
}
public abstract class TigaDimensi{
protected double t;
protected final double phi = 3.14;
public abstract double luasPermukaan();
public abstract double volume();
public abstract String jumlahSisi();
public abstract void display();
public void ubahTinggi(double t){
this.t=t;
System.out.println("Ubah tinggi/diameter berhasil");
}
}
0 komentar:
Post a Comment