public class Circle
{
public double x, y; // centre of the cirlce
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2 * 3.14 * r;
}
public double area() {
return 3.14 * r * r;
}
}
Source Code Class Bujursangkar (Square)
public class Square
{
public double s;
//Methods to return circumference and area
public double circumference() {
return 4*s;
}
public double area() {
return s*s;
}
}
Source Code Class Persegi Panjang (Rectangle)
public class Rectangle
{
public double p;
public double l;
public double circumference() {
return 2*(p+l);
}
public double area () {
return p*l;
}
}
Source Code Class Segitiga (Triangle)
public class Triangle
{
public double a, b, c, d;
public double circumference() {
return a+b+c;
}
public double d() {
return circumference() / 2;
}
public double area() {
return Math.sqrt(d()*(d()-a)*(d()-b)*(d()-c));
}
}
Source Code Class Belah Ketupat (Rhombus)
public class Rhombus
{
public double d1, d2, s;
public double circumference() {
return 4*s;
}
public double area() {
return d1*d2*0.5;
}
}
Source Code Class Jajar Genjang (Parallelogram)
public class Parallelogram
{
public double a,t,h;
public double circumference()
{
return 2*(a+h);
}
public double area()
{
return a*t;
}
}
Source Code Class MyMain
public class MyMain
{
public static void main(String args[])
{
Circle aCircle; //creating reference
aCircle = new Circle(); //creating object
aCircle.x = 10; //assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); //involving method
double circumf = aCircle.circumference();
System.out.println("Lingkaran");
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference="+circumf);
System.out.println();
Square aSquare; //creating reference
aSquare = new Square(); //creating object
aSquare.s = 10; //assigning value to data field
double Area = aSquare.area(); //involving method
double Circumf = aSquare.circumference();
System.out.println("Bujursangkar");
System.out.println("Sisi="+aSquare.s+" Area="+Area);
System.out.println("Sisi="+aSquare.s+" Circumference="+Circumf);
System.out.println();
Rectangle aRectangle; //creating reference
aRectangle = new Rectangle(); //creating object
aRectangle.p = 10; //assigning value to data field
aRectangle.l = 5;
double luas = aRectangle.area(); //involving method
double keliling = aRectangle.circumference();
System.out.println("Persegi Panjang");
System.out.println("Panjang="+aRectangle.p+" Lebar="+aRectangle.l+" Area="+luas);
System.out.println("Panjang="+aRectangle.p+" Lebar="+aRectangle.l+" Circumference="+keliling);
System.out.println();
Triangle aTriangle; //creating reference
aTriangle = new Triangle(); //creating object
aTriangle.a = 3; //assigning value to data field
aTriangle.b = 4;
aTriangle.c = 5;
double Luas = aTriangle.area(); //involving method
double Keliling = aTriangle.circumference();
System.out.println("Segitiga");
System.out.println("Sisi 1="+aTriangle.a+" Sisi 2="+aTriangle.b+" Sisi 3="+aTriangle.c+" Area="+Luas);
System.out.println("Sisi 1="+aTriangle.a+" Sisi 2="+aTriangle.b+" Sisi 3="+aTriangle.c+" Circumference="+Keliling);
System.out.println();
Rhombus aRhombus; //creating reference
aRhombus = new Rhombus(); //creating object
aRhombus.d1 = 16; //assigning value to data field
aRhombus.d2 = 12;
aRhombus.s = 10;
double areaa = aRhombus.area(); //involving method
double circum = aRhombus.circumference();
System.out.println("Belah Ketupat");
System.out.println("Daigonal 1="+aRhombus.d1+" Diagonal 2="+aRhombus.d2+" Sisi="+aRhombus.s+" Area="+areaa);
System.out.println("Daigonal 1="+aRhombus.d1+" Diagonal 2="+aRhombus.d2+" Sisi="+aRhombus.s+" Circumference="+circum);
System.out.println();
Parallelogram aParallelogram; //creating reference
aParallelogram = new Parallelogram(); //creating object
aParallelogram.a = 20; //assigning value to data field
aParallelogram.h = 15;
aParallelogram.t = 12;
double areaaa = aParallelogram.area(); //involving method
double circumm = aParallelogram.circumference();
System.out.println("Jajar Genjang");
System.out.println("Alas="+aParallelogram.a+" Sisi Miring="+aParallelogram.h+" Tinggi="+aParallelogram.t+" Area="+areaaa);
System.out.println("Alas="+aParallelogram.a+" Sisi Miring="+aParallelogram.h+" Tinggi="+aParallelogram.t+" Circumference="+circumm);
}
}
Tampilan program dan hasil setelah dieksekusi
kiri : hasil, kanan : tampilan di BlueJ |
0 comments:
Post a Comment