Object and Classes
OBJECT AND CLASSES
Kali ini saya akan membuat class beberapa bangun 2D beserta testMain nya. Lihat beberapa contoh berikut ini :
1. Bujur Sangkar
public class BujurSangkar
{
public double s; //sisi dari persegi
//Methods to return circumference and area
public double circumference(){
return 4*s;
}
public double area () {
return s*s;
}
}
2. Segitiga Siku-Siku
public class SegitigaSikuSiku
{
public double a; //alas dari segitiga siku-siku
public double t; //tinggi dari segitiga siku-siku
public double sm; //sisi miring dari segitiga siku-siku
//Methods to return circumference and area
public double circumference(){
return a+t+sm;
}
public double area () {
return 0.5*a*t;
}
}
3. Persegi Panjang
public class PersegiPanjang
{
public double p; //panjang dari persegi panjang
public double l; //lebar dari persegi panjang
//Methods to return circumference and area
public double circumference(){
return (2*p)+(2*l);
}
public double area () {
return p*l;
}
}
4. Belah Ketupat
public class BelahKetupat
{
public double d; //Diameter dari belah ketpat
public double s; //Sisi dari belah ketupat
//Methods to return circumference and area
public double circumference(){
return 4*s;
}
public double area () {
return 0.5*d*d;
}
}
5. Jajaran Genjang
public class JajaranGenjang
{
public double a; //alas dari jajaran genjang
public double t; //tinggi dari jajaran genjang
public double sm; //sisi miring dari jajaran genjang
//Methods to return circumference and area
public double circumference(){
return (2*a)+(2*sm);
}
public double area () {
return a*t;
}
}
6. TestMain
class MyMain
{
public static void main(String args[])
{
BujurSangkar aBujur; //creating reference
aBujur = new BujurSangkar(); //creating object
aBujur.s = 5; //assigning value to data field
double area = aBujur.area(); //invoking method
double circumf = aBujur.circumference();
System.out.println("Square");
System.out.println("Sisi="+aBujur.s+" Area="+area);
System.out.println("Sisi="+aBujur.s+" Circumference="+circumf);
System.out.println("\n");
SegitigaSikuSiku aSegitiga; //creating reference
aSegitiga = new SegitigaSikuSiku(); //creating object
aSegitiga.a = 5; //assigning value to data field
aSegitiga.t = 12;
aSegitiga.sm = 13;
double area1 = aSegitiga.area(); //invoking method
double circumf1 = aSegitiga.circumference();
System.out.println("Right Triangle");
System.out.println("Alas="+aSegitiga.a+" Tinggi ="+aSegitiga.t+" Area="+area1);
System.out.println("Alas="+aSegitiga.a+" Tinggi ="+aSegitiga.t+" Sisi Miring ="+aSegitiga.sm+" Circumference="+circumf1);
System.out.println("\n");
PersegiPanjang aPerpan; //creating reference
aPerpan = new PersegiPanjang(); //creating object
aPerpan.p = 3; //assigning value to data field
aPerpan.l = 10;
double area2 = aPerpan.area(); //invoking method
double circumf2 = aPerpan.circumference();
System.out.println("Rectangle");
System.out.println("Panjang="+aPerpan.p+" Lebar="+aPerpan.l+" Area="+area2);
System.out.println("Panjang="+aPerpan.p+" Lebar="+aPerpan.l+" Circumference="+circumf2);
System.out.println("\n");
BelahKetupat aBelah; //creating reference
aBelah = new BelahKetupat(); //creating object
aBelah.d = 10; //assigning value to data field
aBelah.s = 7.07;
double area3 = aBelah.area(); //invoking method
double circumf3 = aBelah.circumference();
System.out.println("Rhombus");
System.out.println("Diagonal="+aBelah.d+" Area="+area3);
System.out.println("Diagonal="+aBelah.d+" Sisi="+aBelah.s+" Circumference="+circumf3);
System.out.println("\n");
JajaranGenjang aJajar; //creating reference
aJajar = new JajaranGenjang(); //creating object
aJajar.a = 9; //assigning value to data field
aJajar.t = 4;
aJajar.sm = 5;
double area4 = aJajar.area(); //invoking method
double circumf4 = aBelah.circumference();
System.out.println("Parallelogram");
System.out.println("Alas="+aJajar.a+" Tinggi="+aJajar.t+" Area="+area4);
System.out.println("Alas="+aJajar.a+" Sisi miring="+aJajar.sm+" Circumference="+circumf4);
System.out.println("\n");
}
}
Begitulah prosesnya, semoga bermanfaat!
Komentar
Posting Komentar