Skip to main content

2D Class OOP

Source code terdiri dari :
- Main
- Circle
- Rectangle
- Square
- Triangle
- Rhombus
- Parallelogram

1) Source code Main

import static java.lang.Math.sqrt;
import java.awt.*;
/**
 * Write a description of class Main here.
 *
 * @author Satria Ade Veda Karuniawan
 * @version 09/10/2018
 */
public class Main
{
    public static void main(String args[])
    {
        //Circle definition
        Circle testCircle;
        testCircle = new Circle();
        testCircle.SetRadius(3.0);
        testCircle.SetCenter(2.0,2.0);
     
        double LuasLingkaran = testCircle.Area();
        double KelilingLingkaran = testCircle.Circumference();
     
        //Rectangle definition
        Rectangle testRectangle;
        testRectangle = new Rectangle();
        testRectangle.SetSide(4.0,3.0);
        testRectangle.SetCenter(2.0,2.0);
     
        double LuasPersegiPanjang = testRectangle.Area();
        double KelilingPersegiPanjang = testRectangle.Circumference();
     
        //Square definition
        Square testSquare;
        testSquare = new Square();
        testSquare.SetSide(4.0);
        testSquare.SetCenter(2.0,2.0);
     
        double LuasPersegi = testSquare.Area();
        double KelilingPersegi = testSquare.Circumference();
     
        //Triangle definition
        Triangle testTriangle;
        testTriangle = new Triangle();
        testTriangle.SetBottom(4.0);
        testTriangle.SetCenter(2.0,2.0);
     
        double LuasSegitiga = testTriangle.Area();
        double KelilingSegitiga = testTriangle.Circumference();
     
        //Rhombus definition
        Rhombus testRhombus;
        testRhombus = new Rhombus();
        testRhombus.SetDiagonal(2.0,1.5);
        testRhombus.SetCenter(2.0,2.0);
     
        double LuasBelahKetupat = testRhombus.Area();
        double KelilingBelahKetupat = testRhombus.Circumference();
     
        //Parallelogram definition
        Parallelogram testParallelogram;
        testParallelogram = new Parallelogram();
        testParallelogram.SetLongSide(4.0);
        testParallelogram.SetHeight(2.0);
        testParallelogram.SetHypo(3.0);
     
        double LuasJajarGenjang = testParallelogram.Area();
        double KelilingJajarGenjang = testParallelogram.Circumference();
     
     
        System.out.println("Luas lingkaran adalah " + LuasLingkaran + " cm^2");
        System.out.println("Keliling lingkaran adalah " + KelilingLingkaran + " cm");
     
        System.out.println("Luas persegi-panjang adalah " + LuasPersegiPanjang + " cm^2");
        System.out.println("Keliling persegi-panjang adalah " + KelilingPersegiPanjang + " cm");
     
        System.out.println("Luas persegi adalah " + LuasPersegi + " cm^2");
        System.out.println("Keliling persegi adalah " + KelilingPersegi + " cm");
     
        System.out.println("Luas segitiga adalah " + LuasSegitiga + " cm^2");
        System.out.println("Keliling segitiga adalah " + KelilingSegitiga + " cm");
     
        System.out.println("Luas belah-ketupat adalah " + LuasBelahKetupat + " cm^2");
        System.out.println("Keliling belah-ketupat adalah " + KelilingBelahKetupat + " cm");
     
        System.out.println("Luas lingkaran adalah " + LuasJajarGenjang + " cm^2");
        System.out.println("Keliling lingkaran adalah " + KelilingJajarGenjang + " cm");

    }
}


2) Source code Circle


/**
 * Write a description of class Circle here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Circle
{
    private double radius;
    private double centerx;
    private double centery;
 
    public Circle()
    {
        radius = 1;
        centerx = 0;
        centery = 0;

    }
 
    public void SetRadius(double r)
    {
        radius = r;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        return 2*3.14*radius;
    }
 
    public double Area()
    {
        return 3.14*radius*radius;
    }
 
}


3) Source code Rectangle

import java.awt.*;
import java.awt.Canvas;
/**
 * Write a description of class Rectangle here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Rectangle
{
    private double aside;
    private double bside;
    private double centerx;
    private double centery;
    private boolean isVisible;
    private String color;
 
    public Rectangle()
    {
        aside = 2;
        bside = 1;
        centerx = 0;
        centery = 0;
        isVisible = true;
        color = "black";

    }
 
    public void SetSide(double l, double s)
    {
        aside = l;
        bside = s;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        return 2*aside + 2*bside;
    }
 
    public double Area()
    {
        return aside * bside;
    }
 

}


4) Source code Square


/**
 * Write a description of class Square here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Square
{
    private double side;
    private double centerx;
    private double centery;
 
    public Square()
    {
        side = 1;
        centerx = 0;
        centery = 0;

    }
 
    public void SetSide(double s)
    {
        side = s;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        return 4 * side;
    }
 
    public double Area()
    {
        return side * side;
    }
}


5) Source code Triangle

import static java.lang.Math.sqrt;
/**
 * Write a description of class Triangle here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Triangle
{
    private double bottom;
    private double height;
    private double hypo;
    private double centerx;
    private double centery;
 
    public Triangle()
    {
        bottom = 1;
        height = 1;
        centerx = 0;
        centery = 0;

    }
 
    public void SetBottom(double s)
    {
        bottom = s;
    }
 
    public void SetHeight(double h)
    {
        height = h;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        hypo = Math.sqrt(0.5*bottom*0.5*bottom + height*height);
        return 2*hypo + bottom;
    }
 
    public double Area()
    {
        return 0.5 * bottom * height;
    }
}


6) Source code Rhombus

import static java.lang.Math.sqrt;
/**
 * Write a description of class Rhombus here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Rhombus
{
    private double firstdiagonal;
    private double seconddiagonal;
    private double side;
    private double centerx;
    private double centery;
 
    public Rhombus()
    {
        firstdiagonal = 1;
        seconddiagonal = 1;
        centerx = 0;
        centery = 0;

    }
 
    public void SetDiagonal(double d1, double d2)
    {
        firstdiagonal = d1;
        seconddiagonal = d2;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        side = Math.sqrt(0.5 * firstdiagonal * 0.5 * firstdiagonal + 0.5 * seconddiagonal * 0.5 * seconddiagonal);
        return 4 * side;
    }
 
    public double Area()
    {
        return 0.5 * firstdiagonal * seconddiagonal;
    }
}


7) Source code Parallelogram


/**
 * Write a description of class Parallelogram here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Parallelogram
{
    private double longside;
    private double height;
    private double hypo;
    private double centerx;
    private double centery;
 
    public Parallelogram()
    {
        longside = 4.0;
        height = 2.0;
        hypo = 3.0;
        centerx = 0;
        centery = 0;

    }
 
    public void SetLongSide(double l)
    {
        longside = l;
    }
 
    public void SetHeight(double h)
    {
        height = h;
    }
 
    public void SetHypo(double hy)
    {
        hypo = hy;
    }
 
    public void SetCenter(double x, double y)
    {
        centerx = x;
        centery = y;
    }
 
 
    public double Circumference()
    {
        return 2 * longside + 2 * hypo;
    }
 
    public double Area()
    {
        return 0.5 * longside * height;
    }
}


Workspace


Comments

Popular posts from this blog

Ticketing Machine

Pembuatan simulasi Ticketing Machine menggunakan OOP Class yang digunakan di dalam Project terdiri atas : Main class TicketMachine class Destination class Library yang digunakan di dalam Project adalah java.util.Scanner Source code Destination class /**  * A class which describes Destination  *  * @author Satria Ade Veda Karuniawan  * @version version 1.0  */ public class Destination {     private String Station;     private int PriceAdult;     private int PriceChild;     //Struct destination       public Destination(String name)     {         Station = name;         PriceAdult = 0;         PriceChild = 0;     }       public void SetInfo(int pa, int pc)     {         PriceAdult = pa;         PriceChild = pc;   ...

Image Viewer GUI

Implementasi Object-Oriented Programming dari Image Viewer yang berfungsi untuk melakukan load sebuah gambar dan memungkinkan dilakukannya manipulasi gambar. Library Java yang digunakan dalam project ini antara lain : Awt (Event, Image) Swing File IO Class yang diimplementasikan antara lain : ImageViewer (Sebagai main class yang melakukan display image pada aplikasi GUI) ImagePanel (Sebagai class yang menampilkan OFImage serta menambah functionality) ImageFileManager (Sebagai class utility untuk melakukan load image) OFImage (Sebagai class yang mendefinisikan image sebagai object) Source Code ImageViewer import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.io.File; /**  *  * @author Satria Ade Veda Karuniawan  * @version 1.0  */ public class ImageViewer {     // static fields:     private static final String VERSION = "Version 1.0";     private st...

Clock

Pembuatan Clock menggunakan BlueJ Class yang digunakan sebagai berikut : Main Clock : digunakan sebagai penghubung antara NumberDisplay dan ClockMechanics NumberDisplay : digunakan sebagai format output number pada jam ClockMechanics : digunakan sebagai pengatur mekanisme jam Main Clock Source Code import java.util.concurrent.TimeUnit; /**  * MainClock class as connector NumberDisplay and ClockMechanics class  *  * @author Satria Ade Veda Karuniawan  * @version Version 1.0  */ public class MainClock {     NumberDisplay numberobj = new NumberDisplay();     ClockMechanics clockobj = new ClockMechanics();     int Marker = 0;     private String[] time_now = {"","","","","","",""};     private String[] converter = new String[7];     public MainClock()     {         while(true)         {            ...