Tuesday, April 22, 2014

pertama buat java application project. Kemudian berikan nama project seperti contoh beri nama javaapplication4 kemudian buat class.
pertama buat class garis

package javaapplication4;

/**
 *
 * @author Manpits
 */
public class Titik {
    //atribut
    protected int x, y;
   
    //method
    public void setX(int a){
        this.x = a;
    }
   
    public void setY(int b){
        this.y = b;
    }
   
    public void setXY(int a, int b){
        this.x = a;
        this.y = b;
    }
   
    public int getX(){
        return this.x;
    }
   
    public int getY(){
        return this.y;
    }
   
    public void showTitik(){
        System.out.print("("+this.x+","+this.y+")");
    }
   
    //constructor
    public Titik(){
        this.x = 0;
        this.y = 0;
    }
   
    public Titik(int a, int b){
        this.x = a;
        this.y = b;
    }
}






Kemudian buat class garis

package javaapplication4;

/**
 *
 * @author Manpits
 */
public class Garis extends Titik{
    protected Titik tAwal, tAkhir;
    public void setTAwal(Titik t){
        this.tAwal = t;
    }
    public void setTAkhir(Titik t){
        this.tAkhir = t;
    }
    public Titik getTAwal(){
        return this.tAwal;
    }
    public Titik getTAkhir(){
        return this.tAkhir;
    }
    public double getPanjang(){
        int dx = this.tAkhir.getX()-this.tAwal.getX();
        int dy = this.tAkhir.getY()-this.tAwal.getY();
        double p = Math.sqrt(Math.pow(dx, 2)+Math.pow(dy, 2));
        return p;
    }
    public void showGaris(){
        this.tAwal.showTitik();
        System.out.print(" - ");
        this.tAkhir.showTitik();
    }
    public Garis(){
        this.tAwal = new Titik();
        this.tAkhir =  new Titik();
    }
    public Garis(Titik t1, Titik t2){
        this.tAwal = t1;
        this.tAkhir = t2;
    }
   
}


Kemudian selanjutnya buat class persegi

package javaapplication4;

/**
 *
 * @author User
 */
public class Persegi extends Garis {
    protected Garis p, l, t;
   
    public void setP(Garis g){
        this.p = g;
    }
   
    public void setL(Garis g){
        this.l = g;
    }
    public void setT(Garis g){
        this.t = g;
    }
    public Garis getP(){
        return this.p;
    }
   
    public Garis getL(){
        return this.l;
    }
     public Garis getT(){
        return this.t;
    }
    public double getLuas(){
        double luas = this.p.getPanjang()*this.l.getPanjang();
        return luas;
    }
   
  
    public double getkeliling(){
        double keliling = (2*this.p.getPanjang())+(2*this.l.getPanjang());
        return keliling;
    }
   
    public Persegi(){
        this.p = new Garis();
        this.l = new Garis();
    }
   
    public Persegi(Garis G1, Garis G2 ){
        this.p = G1;
        this.l = G2;
    }
   
   
}


Selanjutnya buat class balok

package javaapplication4;

/**
 *
 * @author User
 */
public class Balok extends Persegi{
    protected Persegi q, r;
   
    public void setQ(Persegi q){
        this.q = q;
    }
     public void setR(Persegi r){
        this.r = r;
    }
    public Persegi getQ(){
        return this.q;
    }
    public Persegi getR(){
        return this.r;
    }
  public double getVolume(){
      double v = this.q.getLuas()*this.t.getPanjang();
       return v;      
  }
  public Balok(){
        this.q = new Persegi();
        this.t = new Garis();
    }
   
}


Kemudian terakhir jika sudah selesai membuat semua class kembali mengisi syntax dalam project 
seperti contoh :

package javaapplication4;

/**
 *
 * @author Manpits
 */
public class JavaApplication4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("OOP");
        Titik t1 = new Titik();
        t1.showTitik();
        t1.setX(10);
        t1.showTitik();
        t1.setXY(15, 24);
        t1.showTitik();
       
        Titik t2 = new Titik(20,5);
        t2.showTitik();
       
        Titik t3 = new Titik(7,4);
        t3.showTitik();
       
        Titik t4 = new Titik(5,0);
        t4.showTitik();
       
        Garis g1 = new Garis();
        g1.showGaris();
       
        Garis g2 = new Garis(t1,t2);
        g2.showGaris();
        System.out.println("panjang g2 = "+g2.getPanjang());
       
        Garis g3 = new Garis(t2,t3);
        g3.showGaris();
        System.out.println("panjang g3 = "+g3.getPanjang());
      
       
        TITIK2 s1 = new TITIK2(2,3);
        s1.showT();
       
        TITIK2 s2 = new TITIK2();
        s2.seta(4);
        s2.setb(5);
        s2.showT();
       
        Persegi p1 = new Persegi();
        p1.setP(g3);
        p1.setL(g2);
        System.out.println("luas"+p1.getLuas());
        System.out.println(p1.getkeliling());
       
        Persegi p2 = new Persegi(g3,g2);
        System.out.println(p2.getLuas());
       
        Balok2 b1 = new Balok2();
        b1.setQ(p1);
        b1.setT(g3);
        System.out.println("volume = "+b1.getVolume());
       
    }
   
}






0 comments: