Tugas #PBOA-tugas2
Berikut ini adalah program untuk menghitung luas dan keliling dari beberapa bangun datar.
- Persegi
Program untuk class persegi :
public class BujurSangkar
{
public double s; //sisi persegi
public double circumference()
{
return 4*s;
}
public double area()
{
return s*s;
}
}
- Segitiga
Program untuk class segitiga :
public class Segitiga
{
public double a, t, m; //alas, tinggi, sisi miring
public double circumference()
{
return a*t*m;
}
public double area()
{
return a*t*0.5;
}
}
- Persegi Panjang
Program untuk class persegi panjang :
public class PersegiPanjang
{
public double p, l; //panjang, lebar
public double circumference()
{
return 2*p+2*l;
}
public double area()
{
return p*l;
}
}
- Belah Ketupat
Program untuk class belah ketupat :
public class BelahKetupat
{
public double d1, d2, s;
public double circumference()
{
return s*4;
}
public double area()
{
return d1*d2*0.5;
}
}
- Jajargenjang
Program untuk class jajargenjang :
public class JajarGenjang
{
public double a, t, s;
public double circumference()
{
return 2*a+2*s;
}
public double area()
{
return a * t;
}
}
- Test Main
Program untuk class jajargenjang :
public class MyMain
{
public static void main(String args[])
{
//Bujur sangkar
BujurSangkar persegi;
persegi = new BujurSangkar();
persegi.s = 5;
double area1 = persegi.area();
double circumf1 = persegi.circumference();
System.out.println("Sisi="+persegi.s+" Area="+area1);
System.out.println("Sisi="+persegi.s+" Circumference="+circumf1+"\n");
//Segitiga
Segitiga triangle;
triangle = new Segitiga();
triangle.a = 4;
triangle.t = 3;
triangle.m = 5;
double area2 = triangle.area();
double circumf2 = triangle.circumference();
System.out.println("Alas="+triangle.a+" Tinggi="+triangle.t+" Miring="+triangle.m+" Area="+area2);
System.out.println("Alas="+triangle.a+" Tinggi="+triangle.t+" Miring="+triangle.m+" Circumference="+circumf2+"\n");
//Persegi Panjang
PersegiPanjang rectangle;
rectangle = new PersegiPanjang();
rectangle.p = 10;
rectangle.l = 5;
double area3 = rectangle.area();
double circumf3 = rectangle.circumference();
System.out.println("Panjang="+rectangle.p+" Lebar="+rectangle.l+" Area="+area3);
System.out.println("Panjang="+rectangle.p+" Lebar="+rectangle.l+" Circumference="+circumf3+"\n");
//Belah Ketupat
BelahKetupat rhombus;
rhombus = new BelahKetupat();
rhombus.d1 = 8;
rhombus.d2 = 6;
rhombus.s = 5;
double area4 = rhombus.area();
double circumf4 = rhombus.circumference();
System.out.println("Diagonal 1="+rhombus.d1+" Diagonal 2="+rhombus.d2+" Sisi="+rhombus.s+" Area="+area4);
System.out.println("Diagonal 1="+rhombus.d1+" Diagonal 2="+rhombus.d2+" Sisi="+rhombus.s+" Circumference="+circumf4+"\n");
//Jajar Genjang
JajarGenjang parallelogram;
parallelogram = new JajarGenjang();
parallelogram.a = 14;
parallelogram.t = 12;
parallelogram.s = 9;
double area5 = parallelogram.area();
double circumf5 = parallelogram.circumference();
System.out.println("Alas="+parallelogram.a+" Tinggi="+parallelogram.t+" Sisi="+parallelogram.s+" Area="+area5);
System.out.println("Alas="+parallelogram.a+" Tinggi="+parallelogram.t+" Sisi="+parallelogram.s+" Circumference="+circumf5+"\n");
}
}
Berikut adalah output dari program di atas :
Program untuk class persegi :
public class BujurSangkar
{
public double s; //sisi persegi
public double circumference()
{
return 4*s;
}
public double area()
{
return s*s;
}
}
Program untuk class segitiga :
public class Segitiga
{
public double a, t, m; //alas, tinggi, sisi miring
public double circumference()
{
return a*t*m;
}
public double area()
{
return a*t*0.5;
}
}
Program untuk class persegi panjang :
public class PersegiPanjang
{
public double p, l; //panjang, lebar
public double circumference()
{
return 2*p+2*l;
}
public double area()
{
return p*l;
}
}
Program untuk class belah ketupat :
public class BelahKetupat
{
public double d1, d2, s;
public double circumference()
{
return s*4;
}
public double area()
{
return d1*d2*0.5;
}
}
Program untuk class jajargenjang :
public class JajarGenjang
{
public double a, t, s;
public double circumference()
{
return 2*a+2*s;
}
public double area()
{
return a * t;
}
}
Program untuk class jajargenjang :
public class MyMain
{
public static void main(String args[])
{
//Bujur sangkar
BujurSangkar persegi;
persegi = new BujurSangkar();
persegi.s = 5;
double area1 = persegi.area();
double circumf1 = persegi.circumference();
System.out.println("Sisi="+persegi.s+" Area="+area1);
System.out.println("Sisi="+persegi.s+" Circumference="+circumf1+"\n");
//Segitiga
Segitiga triangle;
triangle = new Segitiga();
triangle.a = 4;
triangle.t = 3;
triangle.m = 5;
double area2 = triangle.area();
double circumf2 = triangle.circumference();
System.out.println("Alas="+triangle.a+" Tinggi="+triangle.t+" Miring="+triangle.m+" Area="+area2);
System.out.println("Alas="+triangle.a+" Tinggi="+triangle.t+" Miring="+triangle.m+" Circumference="+circumf2+"\n");
//Persegi Panjang
PersegiPanjang rectangle;
rectangle = new PersegiPanjang();
rectangle.p = 10;
rectangle.l = 5;
double area3 = rectangle.area();
double circumf3 = rectangle.circumference();
System.out.println("Panjang="+rectangle.p+" Lebar="+rectangle.l+" Area="+area3);
System.out.println("Panjang="+rectangle.p+" Lebar="+rectangle.l+" Circumference="+circumf3+"\n");
//Belah Ketupat
BelahKetupat rhombus;
rhombus = new BelahKetupat();
rhombus.d1 = 8;
rhombus.d2 = 6;
rhombus.s = 5;
double area4 = rhombus.area();
double circumf4 = rhombus.circumference();
System.out.println("Diagonal 1="+rhombus.d1+" Diagonal 2="+rhombus.d2+" Sisi="+rhombus.s+" Area="+area4);
System.out.println("Diagonal 1="+rhombus.d1+" Diagonal 2="+rhombus.d2+" Sisi="+rhombus.s+" Circumference="+circumf4+"\n");
//Jajar Genjang
JajarGenjang parallelogram;
parallelogram = new JajarGenjang();
parallelogram.a = 14;
parallelogram.t = 12;
parallelogram.s = 9;
double area5 = parallelogram.area();
double circumf5 = parallelogram.circumference();
System.out.println("Alas="+parallelogram.a+" Tinggi="+parallelogram.t+" Sisi="+parallelogram.s+" Area="+area5);
System.out.println("Alas="+parallelogram.a+" Tinggi="+parallelogram.t+" Sisi="+parallelogram.s+" Circumference="+circumf5+"\n");
}
}
Komentar
Posting Komentar