Membuat class untuk setiap bentuk geometri yang berisi method luas dan keliling.
Class Main:
Class Persegi:
Class Lingkaran:
Class PersegiPanjang:
Class BelahKetupat:
Class JajarGenjang:
Class Segitiga:
public class Main
{
public static void main(){
//Scanner ps = new Scanner(System.in);
Lingkaran aCircle;
aCircle = new Lingkaran();
aCircle.x = 10; aCircle.y = 10; aCircle.r = 5;
double luas = aCircle.luas();
double keliling = aCircle.keliling();
System.out.println("Lingkaran:");
System.out.println("Titik pusat: ("+aCircle.x+","+aCircle.y+")");
System.out.println("Jari-jari: "+aCircle.r);
System.out.println("Luas: "+luas);
System.out.println("Keliling: "+keliling);
System.out.println("");
Segitiga aSegitiga;
aSegitiga = new Segitiga();
aSegitiga.a = 8; aSegitiga.t = 3;
double s = aSegitiga.slope();
luas = aSegitiga.luas();
keliling = aSegitiga.keliling();
System.out.println("Segitiga:");
System.out.println("Sisi: "+s);
System.out.println("Alas: "+aSegitiga.a);
System.out.println("Tinggi: "+aSegitiga.t);
System.out.println("Luas: "+luas);
System.out.println("Keliling: "+keliling);
System.out.println("");
}
}
Class Persegi:
public class Persegi
{
public double s;
public double keliling(){
return 4*s;
}
public double luas(){
return s*s;
}
}
Class Lingkaran:
public class Lingkaran
{
public double x,y,r;
public double keliling(){
return 2*3.14*r;
}
public double luas(){
return 3.14*r*r;
}
}
Class PersegiPanjang:
public class PersegiPanjang
{
public double p,l;
public double keliling(){
return 2*p+2*l;
}
public double Luas(){
return p*l;
}
}
Class BelahKetupat:
public class BelahKetupat
{
public double d1,d2;
public double s(){
return Math.sqrt((d1/2)*(d1/2) + (d2/2)*(d2/2));
}
public double luas(){
return (d1*d2)/2;
}
public double keliling(){
return 4*s();
}
}
Class JajarGenjang:
public class JajarGenjang
{
public double a,b,t;
public double luas(){
return a*t;
}
public double keliling(){
return 2*(a+b);
}
}
Class Segitiga:
public class Segitiga
{
public double a,t;
public double slope(){
return Math.sqrt((a/2)*(a/2) + (t*t));
}
public double luas(){
return (a*t)/2;
}
public double keliling(){
return 2*slope()+a;
}
}
Komentar
Posting Komentar