Program Menghitung Bangun Datar dalam Java
Program menghitung bangun datar dalam java.Dalam posting saya kali ini saya akan membuat program dalam java yang bisa menghitung luas dan keliling dari 4 jenis bangun datar yaitu persegi,persegi panjang,lingkaran dan juga segitiga.Dalam setiap class bangun datar nya akan ada banyak konstruktor untuk setiap case pembuatan object nya.Setiap Class sebenar nya hampir sama namun dirubah sedikit untuk menyesuaikan kebutuhan setiap bangun datar.Dalam method main saya hanya mencoba secara manual untuk membuat dan mencetak setiap object nya.Silahkan berkreasi sendiri jika ingin membuat program ini lebih baik.
Ini adalah code nya :
Ini adalah code nya :
/**
* Write a description of class Circle here.
*
* @author Adhityairvan
* @version 0.1
*/
import static java.lang.Math.*;
public class Circle
{
private double r;
private String color;
public Circle ()
{
r = 1.0;
color = "red";
}
public Circle (double jari)
{
r = jari;
color = "red";
}
public Circle ( double jari, String warna )
{
r = jari;
color = warna;
}
public double getArea(){
System.out.printf("Area of %s Circle is.. %f\n",color,PI * r * r);
return PI * r * r;
}
public double getRound()
{
System.out.printf("Around of %s Circle is.. %s\n",color,PI * 2 * r);
return PI * 2 * r;
}
public double getRadius()
{
return r;
}
public String getColor()
{
return color;
}
public String toString()
{
return String.format("A Circle with %s color has an area of %f cm",this.getColor(),this.getArea());
}
}
public class Triangle
{
private double a;
private double t;
private String color;
public Triangle()
{
a = 1.0;
t = 1.0;
color = "white";
}
public Triangle(double alas)
{
a = alas;
t = 1.0;
color = "white";
}
public Triangle(double alas,String warna)
{
a = alas;
color = warna;
}
public Triangle(double alas,double tinggi,String warna)
{
a = alas;
t = tinggi;
color = warna;
}
public double getA()
{
return a;
}
public double getT()
{
return t;
}
public String getColor()
{
return color;
}
public double getArea()
{
return a*t/2;
}
public String toString()
{
return String.format("A %s colored triangle have an area of %f cm",this.getColor(),this.getArea());
}
}
public class Rectangle
{
private double p;
private double l;
private String color;
public Rectangle()
{
p = 1.0; l = 1.0;
color = "white";
}
public Rectangle(double panjang,double lebar)
{
p = panjang;
l = lebar;
color = "white";
}
public Rectangle(double panjang,double lebar,String warna)
{
p = panjang;
l = lebar;
color = warna;
}
public double getP()
{
return p;
}
public double getL(){ return l; }
public String getColor() {return color; }
public double getArea ()
{
return p * l;
}
public double getRound()
{
return 2*(p+l);
}
public String toString()
{
return String.format("A %s colored rectangle with Lenght of %f and Widht of %f have an area of %f cm"
,this.getColor(),this.getP(),this.getL(),this.getArea());
}
}
public class Square
{
private double s;
private String color;
public Square()
{
s = 1.0;
color = "white";
}
public Square(double sisi)
{
s = sisi;
color = "white";
}
public Square(double sisi,String warna)
{
s = sisi;
color = warna;
}
public double getArea(){
return s * s;
}
public double getRound()
{
return 4 * s;
}
public double getSide()
{
return s;
}
public String getColor()
{
return color;
}
public String toString()
{
return String.format("The %s colored Square has an Area of %f cm",this.getColor(),this.getArea());
}
}
public class MainClass
{
public static void main()
{
Circle circle1 = new Circle(7.0,"Black");
Circle circle2 = new Circle(14.0);
Circle circle3 = new Circle();
System.out.println(circle1);
System.out.printf("A round of %f\n",circle1.getRound());
System.out.println(circle2);
System.out.printf("A round of %f\n",circle2.getRound());
System.out.println(circle3);
System.out.printf("A round of %f\n",circle3.getRound());
System.out.printf("\n\n");
System.out.printf("Now to test the Rectangle\n");
Rectangle persegi1 = new Rectangle(5,4);
Rectangle persegi2 = new Rectangle(5,3,"Merah");
Rectangle persegi3 = new Rectangle();
System.out.println(persegi1);
System.out.printf("A round of %f\n",persegi1.getRound());
System.out.println(persegi2);
System.out.printf("A round of %f\n",persegi2.getRound());
System.out.println(persegi3);
System.out.printf("A round of %f\n",persegi3.getRound());
System.out.printf("\n\n");
System.out.printf("Now to test the square\n");
Square kotak1 = new Square(10);
Square kotak2 = new Square(5,"hijau");
Square kotak3 = new Square();
System.out.println(kotak1);
System.out.printf("A round of %f\n",kotak1.getRound());
System.out.println(kotak2);
System.out.printf("A round of %f\n",kotak2.getRound());
System.out.println(kotak3);
System.out.printf("A round of %f\n",kotak3.getRound());
System.out.printf("\n\n");
System.out.printf("Now to test the triangle\n");
Triangle segitiga1 = new Triangle(2);
Triangle segitiga2 = new Triangle(6.0,3.0,"Kuning");
Triangle segitiga3 = new Triangle ( 10.0, "Kuning");
Triangle segitiga4 = new Triangle();
System.out.println(segitiga1);
System.out.println(segitiga2);
System.out.println(segitiga3);
System.out.println(segitiga4);
System.out.printf("\n\n");
}
}
Sekian Postingan saya kali ini,semoga source code ini berguna untuk anda.see u next time..

0 comments: