Sunday, September 16, 2018

Tugas 2 - PBO A - Menghitung Luas dan Keliling Bangun Datar



Class-class yang digunakan dalam program ini terbagi menjadi 6 yaitu :
1. Main
2. Circle
3. Square
4. Rectangle
5. Rhombus
6. Triangle
7. Paralellogram

Berikut Source Code untuk menghitung luas dan Keliling Bangun datar diatas dalam Java

Main :

 class Main  
 {  
   public static void main(String args[])  
   {  
     circle aCircle;  
     aCircle = new circle();  
     aCircle.x = 10;  
     aCircle.y = 20;  
     aCircle.r = 5;  
     System.out.println("Circle");  
     System.out.println("Radius="+aCircle.r+" Area="+aCircle.area());  
     System.out.println("Radius="+aCircle.r+" Circumference="+aCircle.circumference());  
     System.out.println();  
     square aSquare;  
     aSquare = new square();  
     aSquare.s = 12;  
     System.out.println("Square");  
     System.out.println("Side="+aSquare.s+" Area="+aSquare.area());  
     System.out.println("Side="+aSquare.s+" Circumference="+aSquare.circumference());  
     System.out.println();  
     rectangle aRectangle;  
     aRectangle = new rectangle();  
     aRectangle.p = 14;  
     aRectangle.l = 9;  
     System.out.println("Rectangle");  
     System.out.println("Length="+aRectangle.p+" Width="+aRectangle.l+" Area="+aRectangle.area());  
     System.out.println("Length="+aRectangle.p+" Width="+aRectangle.l+" Circumference="+aRectangle.circumference());  
     System.out.println();  
     rhombus aRhombus;  
     aRhombus = new rhombus();  
     aRhombus.d1 = 16;  
     aRhombus.d2 = 9;  
     System.out.println("Rhombus");  
     System.out.println("Diagonal1="+aRhombus.d1+" Diagonal2="+aRhombus.d2+" Area="+aRhombus.area());  
     System.out.println("Diagonal1="+aRhombus.d1+" Diagonal2="+aRhombus.d2+" Circumference="+aRectangle.circumference());  
     System.out.println();  
     triangle aTriangle;  
     aTriangle = new triangle();  
     aTriangle.a = 7;  
     aTriangle.b = 10;  
     aTriangle.c = 8;  
     System.out.println("Triangle");  
     System.out.println("Side1="+aTriangle.a+" Side2="+aTriangle.b+" Side3="+aTriangle.c+" Area="+aTriangle.area());  
     System.out.println("Side1="+aTriangle.a+" Side2="+aTriangle.b+" Side3="+aTriangle.c+" Circumference="+aTriangle.circumference());  
     System.out.println();  
     parallelogram aParallel;  
     aParallel = new parallelogram();  
     aParallel.alas = 12;  
     aParallel.tinggi = 4;  
     aParallel.miring = 5;  
     System.out.println("Parallelogram");  
     System.out.println("Base="+aParallel.alas+" Height="+aParallel.tinggi+" Hypotenuse="+aParallel.miring+" Area="+aParallel.area());  
     System.out.println("Base="+aParallel.alas+" Height="+aParallel.tinggi+" Hypotenuse="+aParallel.miring+" Circumference="+aParallel.circumference());  
     System.out.println();  
   }  
 }  

Circle :

 public class circle  
 {  
   public double x, y;  
   public double r;  
   public double circumference()  
   {  
     return 2*3.14*r;  
   }  
   public double area()  
   {  
     return 3.14*r*r;  
   }  
 }  

Square :

 public class square   
 {  
   public double s;  
   public double circumference()  
   {  
     return 4.0*s;  
   }  
   public double area()  
   {  
     return s*s;  
   }  
 }  

Rectangle :

 public class rectangle  
 {  
   public double p, l;  
   public double circumference()  
   {  
     return 2.0*p+2.0*l;  
   }  
   public double area()  
   {  
     return p*l;  
   }  
 }  

Rhombus :

 public class rhombus  
 {  
   public double d1, d2;  
   public double sisi()  
   {  
     return Math.sqrt((0.25*d1*d1)+(0.25*d2*d2));  
   }  
   public double circumference()  
   {  
     return 4.0*sisi();  
   }  
   public double area()  
   {  
     return 0.5*d1*d2;  
   }  
 }  

Triangle :

 public class triangle  
 {  
   public double a, b, c;  
   public double circumference()  
   {  
     return a+b+c;  
   }  
   public double s()  
   {  
     return 0.5*(a+b+c);  
   }  
   public double area()  
   {  
     return Math.sqrt(s()*(s()-a)*(s()-b)*(s()-c));  
   }  
 }  

Paralellogram :

 public class parallelogram  
 {  
   public double alas, miring, tinggi;  
   public double circumference()  
   {  
     return 2.0*alas+2.0*miring;  
   }  
   public double area()  
   {  
     return alas*tinggi;  
   }  
 }  

No comments:

Post a Comment