Tugas 2 PBO A - Class dan Object
Tugas 2 PBO A (Class dan Object pada BlueJ)
Nama : Ifta Jihan N
NRP : 05111740000034
Membuat rumus bangun 2 dimensi dengan menggunakan Class dan Object dengan Java pada BlueJ.
Source Code:
1. Main
2. Square
3. Triangle
4. Rectangle
5. Diamond
6. Parallelogram
Output:
Nama : Ifta Jihan N
NRP : 05111740000034
Membuat rumus bangun 2 dimensi dengan menggunakan Class dan Object dengan Java pada BlueJ.
Source Code:
1. Main
/**
* @author Jihan
* @version 10/09/2018
*/
public class MyMain
{
public static void Square()
{
Square aSquare;
aSquare = new Square();
aSquare.x = 10;
double area = aSquare.area();
double circumf = aSquare.circumference();
System.out.println("Persegi");
System.out.println("Sisi="+aSquare.x);
System.out.println("Luas="+area);
System.out.println("Keliling="+circumf+ "\n");
}
public static void Triangle()
{
Triangle aTriangle;
aTriangle = new Triangle();
aTriangle.a = 5;
aTriangle.t = 4;
double area = aTriangle.area();
double circumf = aTriangle.circumference();
System.out.println("Segitiga Sama Sisi");
System.out.println("Alas="+aTriangle.a+ " Tinggi="+aTriangle.t);
System.out.println("Luas="+area);
System.out.println("Keliling="+circumf+ "\n");
}
public static void Rectangle()
{
Rectangle aRectangle;
aRectangle = new Rectangle();
aRectangle.x = 10;
aRectangle.y = 20;
double area = aRectangle.area();
double circumf = aRectangle.circumference();
System.out.println("Persegi Panjang");
System.out.println("Sisi x="+aRectangle.x+ " Sisi y="+aRectangle.y);
System.out.println("Luas="+area);
System.out.println("Keliling="+circumf+ "\n");
}
public static void Diamond()
{
Diamond aDiamond;
aDiamond = new Diamond();
aDiamond.d1 = 10;
aDiamond.d2 = 10;
aDiamond.s = 20;
double area = aDiamond.area();
double circumf = aDiamond.circumference();
System.out.println("Belah Ketupat");
System.out.println("Diagonal 1="+aDiamond.d1+ " Diagonal 2="+aDiamond.d2+ " Sisi="+aDiamond.s);
System.out.println("Luas="+area);
System.out.println("Keliling="+circumf+ "\n");
}
public static void Parallelogram()
{
Parallelogram aParallelogram;
aParallelogram = new Parallelogram();
aParallelogram.a = 10;
aParallelogram.b = 8;
aParallelogram.t = 7;
double area = aParallelogram.area();
double circumf = aParallelogram.circumference();
System.out.println("Jajar Genjang");
System.out.println("Alas="+aParallelogram.a+ " Sisi miring="+aParallelogram.b+ " Tinggi="+aParallelogram.t);
System.out.println("Luas="+area);
System.out.println("Keliling="+circumf+ "\n");
}
}
2. Square
/**
* @author Jihan
* @version 10/09/2018
*/
public class Square
{
public double x;
public double circumference()
{
return 4*x;
}
public double area()
{
return x*x;
}
}
3. Triangle
/**
* @author Jihan
* @version 10/09/2018
*/
public class Triangle
{
public double a,t;
public double circumference()
{
return a*3;
}
public double area()
{
return 0.5*(a*t);
}
}
4. Rectangle
/**
* @author Jihan
* @version 10/09/2018
*/
public class Rectangle
{
public double x, y;
public double circumference()
{
return (2*x)+(2*y);
}
public double area()
{
return x*y;
}
}
5. Diamond
/**
* @author Jihan
* @version 10/09/2018
*/
public class Diamond
{
public double d1, d2, s;
public double circumference()
{
return s*4;
}
public double area()
{
return (d1*d2)*0.5;
}
}
6. Parallelogram
/**
* @author Jihan
* @version 10/09/2018
*/
public class Parallelogram
{
public double a,b,t;
public double circumference()
{
return 2*(a+b);
}
public double area()
{
return a*t;
}
}
Output:
Comments
Post a Comment