Saturday, June 25, 2011

[contoh] Interface

0 comments
Ya, seperti biasa,..
ini adalah contoh-contoh dari interface.




import javax.swing.JOptionPane;
interface mycomparableee{
boolean greaterThan(Object obj);
boolean lessThan(Object obj);
boolean equal(Object obj);
}
interface Constants{
int min=10;
int max=100;
}
class FourDigitsNumber implements Constants, mycomparableee {
  private int value;
  public FourDigitsNumber(int value) {
  /*atur agar nilai yang diinputkan dalam
                 constructor hanya berada di antara min - max
    */
    this.value=value;
}
  /*tambahkan method get untuk mengakses value*/

  /*overriding method greaterThan*/
  public int getValue(){
  return value;
  }
  public boolean greaterThan(Object obj) {
    /*casting from superclass to subclass*/
    FourDigitsNumber number = (FourDigitsNumber)obj;
    return ( value > number.getValue() );
  }
  public boolean lessThan(Object obj) {
    /*casting from superclass to subclass*/
    FourDigitsNumber number = (FourDigitsNumber)obj;
    return ( value < number.getValue() );
  }
  public boolean equal(Object obj) {
    /*casting from superclass to subclass*/
    FourDigitsNumber number = (FourDigitsNumber)obj;
    return ( value == number.getValue() );
  }
  /*lengkapi overriding method interface*/
  }
 class ExInterface {
  public static void main(String [] args) {
        int value;
        String str=JOptionPane.showInputDialog("Masukkan nilai : ");
        value=Integer.parseInt(str);
        System.out.println("nilai anda: "+value);
        if ((value > 10) & (value < 100))
            System.out.println("true");
        else
            System.out.println("false");
  }
  }









import javax.swing.*;
interface Induk {
  double LuasSegi3(double a, double t);
  double LuasSegi4(double s);
  double Lingkaran(double r);
}
interface Constants{
  double phi = 3.14;
}
class Luas implements Induk,Constants{   
  double a, t, s, r, LuasSegi3, LuasSegi4, Lingkaran;
  Luas(double a,double t,double s,double r) {
    this.a = a;
    this.t = t;
        this.s = s;
    this.r = r;
  }
  public double LuasSegi3(double a, double t) {
    return (0.5 * a * t);
  }
  public double LuasSegi4(double s) {
    return (s * s);
  }
  public double Lingkaran(double r) {
    return (phi * r * r);
  }
  public void CetakHasil() {
    System.out.println("LuasSegi3 = "+LuasSegi3(a,t));
        System.out.println("LuasSegi4 = "+LuasSegi4(s));
        System.out.println("Lingkaran = "+Lingkaran(r));
  }
}
class LuasBidang{
  public static void main(String[] args)  { 
         double a, t, s, r, LuasSegi3, LuasSegi4, Lingkaran;
     System.out.println("Masukkan nilai alas : ");
     a=Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai alasnya ","Input",JOptionPane.QUESTION_MESSAGE));
     System.out.println(+a);
     System.out.println("Masukkan nilai tinggi : ");
     t=Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai tingginya ","Input",JOptionPane.QUESTION_MESSAGE));
     System.out.println(+t);
         System.out.println("Masukkan nilai sisinya : ");
     s=Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai sisinya ","Input",JOptionPane.QUESTION_MESSAGE));
     System.out.println(+s);
     System.out.println("Masukkan nilai jari2 : ");
     r=Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai jarinya ","Input",JOptionPane.QUESTION_MESSAGE));
     System.out.println(+r);
    Luas Luas1 = new Luas(a,t,s,r);
    Luas1.CetakHasil();
  }
}





Nah ini yang terakhir.


import javax.swing.*;
interface MtkDasar {
  double tambah(int a, int b);
  double kurang(int a, int b);
}
class Pecahan implements MtkDasar{      
  private int a;
  private int b;
  Pecahan(int a,int b) {
    this.a = a;
    this.b = b;
  }
  public double tambah(int a, int b) {
    return (a + b);
  }
  public double kurang(int a, int b) {
    return (a - b);
  }
  public void cetakHasil() {
    System.out.println("a + b = " + tambah(a, b));
    System.out.println("a - b = " + kurang(a, b));
  }
}
class CobaPecahan { 
  public static void main(String[] args) {
    int a;
  int b;
    System.out.println("Masukkan nilai a : ");
    a = Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai a ","Input",JOptionPane.QUESTION_MESSAGE));
    System.out.println(+a);
    System.out.println("Masukkan nilai b : ");
    b = Integer.parseInt(JOptionPane.showInputDialog(null,"Input nilai b ","Input",JOptionPane.QUESTION_MESSAGE));
    System.out.println(+b);
  Pecahan HasilHitung = new Pecahan(a,b);         
    HasilHitung.cetakHasil();       
  }          
}


Leave a Reply