Program Java kali ini saya akan membuat game tetris sederhana.Dalam game ini terdapat 3 class yaitu class board untuk papan permainan dan rendering nya,class tetris untuk class main nya, dan juga class shape yg menyimpan bentuk2 dari balok yang jatuh dan juga method2 untuk memutar balok nya.
Class Board

 /**  
  * Write a description of class Board here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 package tetris;  
 import java.awt.Color;  
 import java.awt.Dimension;  
 import java.awt.Graphics;  
 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import java.awt.event.KeyAdapter;  
 import java.awt.event.KeyEvent;  
 import javax.swing.JLabel;  
 import javax.swing.JPanel;  
 import javax.swing.Timer;  
 import tetris.Shape.bentuk;  
 public class Board extends JPanel implements ActionListener {  
   final int BoardWidth = 15;  
   final int BoardHeight = 22;  
   Timer timer;  
   boolean isFallingFinished = false;  
   boolean isStarted = false;  
   boolean isPaused = false;  
   int numLinesRemoved = 0;  
   int curX = 0;  
   int curY = 0;  
   JLabel statusbar;  
   Shape curPiece;  
   bentuk[] board;  
   public Board(Tetris parent) {  
     setFocusable(true);  
     curPiece = new Shape();  
     timer = new Timer(200, this);  
     timer.start();   
     statusbar = parent.getStatusBar();  
     board = new bentuk[BoardWidth * BoardHeight];  
     addKeyListener(new TAdapter());  
     clearBoard();   
   }  
   public void actionPerformed(ActionEvent e) {  
     if (isFallingFinished) {  
       isFallingFinished = false;  
       newPiece();  
       newPiece();  
     } else {  
       oneLineDown();  
     }  
   }  
   int squareWidth() { return (int) getSize().getWidth() / BoardWidth; }  
   int squareHeight() { return (int) getSize().getHeight() / BoardHeight; }  
   bentuk shapeAt(int x, int y) { return board[(y * BoardWidth) + x]; }  
   public void start()  
   {  
     if (isPaused)  
       return;  
     isStarted = true;  
     isFallingFinished = false;  
     numLinesRemoved = 0;  
     clearBoard();  
     newPiece();  
     timer.start();  
   }  
   private void pause()  
   {  
     if (!isStarted)  
       return;  
     isPaused = !isPaused;  
     if (isPaused) {  
       timer.stop();  
       statusbar.setText("paused");  
     } else {  
       timer.start();  
       statusbar.setText(String.valueOf(numLinesRemoved));  
     }  
     repaint();  
   }  
   public void paint(Graphics g)  
   {   
     super.paint(g);  
     Dimension size = getSize();  
     int boardTop = (int) size.getHeight() - BoardHeight * squareHeight();  
     for (int i = 0; i < BoardHeight; ++i) {  
       for (int j = 0; j < BoardWidth; ++j) {  
         bentuk shape = shapeAt(j, BoardHeight - i - 1);  
         if (shape != bentuk.NoShape)  
           drawSquare(g, 0 + j * squareWidth(),  
                 boardTop + i * squareHeight(), shape);  
       }  
     }  
     if (curPiece.getShape() != bentuk.NoShape) {  
       for (int i = 0; i < 4; ++i) {  
         int x = curX + curPiece.x(i);  
         int y = curY - curPiece.y(i);  
         drawSquare(g, 0 + x * squareWidth(),  
               boardTop + (BoardHeight - y - 1) * squareHeight(),  
               curPiece.getShape());  
       }  
     }  
   }  
   private void dropDown()  
   {  
     int newY = curY;  
     while (newY > 0) {  
       if (!tryMove(curPiece, curX, newY - 1))  
         break;  
       --newY;  
     }  
     pieceDropped();  
   }  
   private void oneLineDown()  
   {  
     if (!tryMove(curPiece, curX, curY - 1))  
       pieceDropped();  
   }  
   private void clearBoard()  
   {  
     for (int i = 0; i < BoardHeight * BoardWidth; ++i)  
       board[i] = bentuk.NoShape;  
   }  
   private void pieceDropped()  
   {  
     for (int i = 0; i < 4; ++i) {  
       int x = curX + curPiece.x(i);  
       int y = curY - curPiece.y(i);  
       board[(y * BoardWidth) + x] = curPiece.getShape();  
     }  
     removeFullLines();  
     if (!isFallingFinished)  
       newPiece();  
   }  
   private void newPiece()  
   {  
     curPiece.setRandomShape();  
     curX = BoardWidth / 2 + 1;  
     curY = BoardHeight - 1 + curPiece.minY();  
     if (!tryMove(curPiece, curX, curY)) {  
       curPiece.setShape(bentuk.NoShape);  
       timer.stop();  
       isStarted = false;  
       statusbar.setText("game over");  
     }  
   }  
   private boolean tryMove(Shape newPiece, int newX, int newY)  
   {  
     for (int i = 0; i < 4; ++i) {  
       int x = newX + newPiece.x(i);  
       int y = newY - newPiece.y(i);  
       if (x < 0 || x >= BoardWidth || y < 0 || y >= BoardHeight)  
         return false;  
       if (shapeAt(x, y) != bentuk.NoShape)  
         return false;  
     }  
     curPiece = newPiece;  
     curX = newX;  
     curY = newY;  
     repaint();  
     return true;  
   }  
   private void removeFullLines()  
   {  
     int numFullLines = 0;  
     for (int i = BoardHeight - 1; i >= 0; --i) {  
       boolean lineIsFull = true;  
       for (int j = 0; j < BoardWidth; ++j) {  
         if (shapeAt(j, i) == bentuk.NoShape) {  
           lineIsFull = false;  
           break;  
         }  
       }  
       if (lineIsFull) {  
         numFullLines += 5;  
         for (int k = i; k < BoardHeight - 1; ++k) {  
           for (int j = 0; j < BoardWidth; ++j)  
              board[(k * BoardWidth) + j] = shapeAt(j, k + 1);  
         }  
       }  
     }  
     if (numFullLines > 0) {  
       numLinesRemoved += numFullLines;  
       statusbar.setText(String.valueOf(numLinesRemoved));  
       isFallingFinished = true;  
       curPiece.setShape(bentuk.NoShape);  
       repaint();  
     }  
    }  
   private void drawSquare(Graphics g, int x, int y, bentuk shape)  
   {  
     Color colors[] = { new Color(0, 0, 0), new Color(204, 102, 102),   
       new Color(102, 204, 102), new Color(102, 102, 204),   
       new Color(204, 204, 102), new Color(204, 102, 204),   
       new Color(102, 204, 204), new Color(218, 170, 0)  
     };  
     Color color = colors[shape.ordinal()];  
     g.setColor(color);  
     g.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2);  
     g.setColor(color.brighter());  
     g.drawLine(x, y + squareHeight() - 1, x, y);  
     g.drawLine(x, y, x + squareWidth() - 1, y);  
     g.setColor(color.darker());  
     g.drawLine(x + 1, y + squareHeight() - 1,  
              x + squareWidth() - 1, y + squareHeight() - 1);  
     g.drawLine(x + squareWidth() - 1, y + squareHeight() - 1,  
              x + squareWidth() - 1, y + 1);  
   }  
   class TAdapter extends KeyAdapter {  
      public void keyPressed(KeyEvent e) {  
        if (!isStarted || curPiece.getShape() == bentuk.NoShape) {   
          return;  
        }  
        int keycode = e.getKeyCode();  
        if (keycode == 'p' || keycode == 'P') {  
          pause();  
          return;  
        }  
        if (isPaused)  
          return;  
        switch (keycode) {  
        case KeyEvent.VK_LEFT:  
          tryMove(curPiece, curX - 1, curY);  
          break;  
        case KeyEvent.VK_RIGHT:  
          tryMove(curPiece, curX + 1, curY);  
          break;  
        case KeyEvent.VK_DOWN:  
          tryMove(curPiece.rotateRight(), curX, curY);  
          break;  
        case KeyEvent.VK_UP:  
          tryMove(curPiece.rotateLeft(), curX, curY);  
          break;  
        case KeyEvent.VK_SPACE:  
          dropDown();  
          break;  
        case 'd':  
          oneLineDown();  
          break;  
        case 'D':  
          oneLineDown();  
          break;  
        }  
      }  
    }  
 }  


Class Shape

 package tetris;  
 /**  
  * Write a description of class Shape here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 import java.util.Random;  
 import java.lang.Math;  
 public class Shape  
 {  
   enum bentuk { NoShape, ZShape, SShape, LineShape, TShape, SquareShape, LShape, MirroredLShape };  
   private bentuk bentuktetris;  
   private int coords[][];  
   private int[][][] coordsTable;  
   public Shape(){  
     coords = new int[4][2];  
     setShape(bentuk.NoShape);  
   }  
   public void setShape(bentuk balok){  
     coordsTable = new int[][][] {  
       { { 0, 0 },  { 0, 0 },  { 0, 0 },  { 0, 0 } },  
       { { 0, -1 }, { 0, 0 },  { -1, 0 }, { -1, 1 } },  
       { { 0, -1 }, { 0, 0 },  { 1, 0 },  { 1, 1 } },  
       { { 0, -1 }, { 0, 0 },  { 0, 1 },  { 0, 2 } },  
       { { -1, 0 }, { 0, 0 },  { 1, 0 },  { 0, 1 } },  
       { { 0, 0 },  { 1, 0 },  { 0, 1 },  { 1, 1 } },  
       { { -1, -1 }, { 0, -1 }, { 0, 0 },  { 0, 1 } },  
       { { 1, -1 }, { 0, -1 }, { 0, 0 },  { 0, 1 } }  
     };  
     for( int i = 0; i < 4 ; i++) {  
       for (int j = 0; j < 2; ++j) {  
         coords[i][j] = coordsTable[balok.ordinal()][i][j];  
       }  
     }  
     bentuktetris = balok;  
   }  
   private void setX(int index, int x){  
     coords[index][0] = x ;  
   }  
   private void setY( int index, int y){  
     coords[index][1] = y;  
   }  
   public int x(int index) {   
     return coords[index][0];   
   }  
   public int y(int index) {  
     return coords[index][1];   
   }  
   public bentuk getShape() {  
     return bentuktetris;   
   }  
   public void setRandomShape(){  
     Random r = new Random();  
     int x = Math.abs(r.nextInt()) % 7 + 1;  
     bentuk[] values = bentuk.values();  
     setShape(values[x]);  
   }  
   public int minX(){  
     int m = coords[0][0];  
     for ( int i = 0 ; i < 4 ; i++){  
       m = Math.min(m,coords[i][0]);  
     }  
     return m;  
   }  
   public int minY()   
   {  
    int m = coords[0][1];  
    for (int i=0; i < 4; i++) {  
      m = Math.min(m, coords[i][1]);  
    }  
    return m;  
   }  
   public Shape rotateLeft(){  
     if ( bentuktetris == bentuk.SquareShape) return this;  
     Shape result = new Shape();  
     result.bentuktetris = bentuktetris;  
     for (int i = 0; i < 4; ++i) {  
       result.setX(i, y(i));  
       result.setY(i, -x(i));  
     }  
     return result;  
   }  
   public Shape rotateRight()  
   {  
     if (bentuktetris == bentuk.SquareShape)  
       return this;  
     Shape result = new Shape();  
     result.bentuktetris = bentuktetris;  
     for (int i = 0; i < 4; ++i) {  
       result.setX(i, -y(i));  
       result.setY(i, x(i));  
     }  
     return result;  
   }  
 }  

Dan yang dibawah ini adalah class tetris nya

 package tetris;  
 import java.awt.BorderLayout;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 public class Tetris extends JFrame {  
   JLabel statusbar;  
   public Tetris() {  
     statusbar = new JLabel(" 0");  
     add(statusbar, BorderLayout.SOUTH);  
     Board board = new Board(this);  
     add(board);  
     board.start();  
     setSize(400, 600);  
     setTitle("Tetris Keren");  
     setDefaultCloseOperation(EXIT_ON_CLOSE);  
   }  
   public JLabel getStatusBar() {  
     return statusbar;  
   }  
   public static void main(String[] args) {  
     Tetris game = new Tetris();  
     game.setLocationRelativeTo(null);  
     game.setVisible(true);  
   }   
 }  

Sekian adalah code dari program tetris sederhana nya.
Terimakasih
Bentuk UML nya


Code Program nya

 /**  
  * Class untuk antrian per orang  
  *   
  * @author Adhityairvan   
  * @version 0.1  
  */  
 import javax.swing.*;   
 public class Antrian  
 {  
   private static int totalAntrianTell,totalAntrianCS;  
   private String detail;  
   public Antrian ()  
   {  
     totalAntrianTell = 0;  
     totalAntrianCS = 0;  
   }   
   public void tambahAntrian ( int kode )  
   {  
     if(kode == 1){  
       totalAntrianTell++;  
     }  
     else {  
       totalAntrianCS++;  
     }  
   }  
   public int getNoTell(){  
     return totalAntrianTell;  
   }  
   public int getNoCS(){  
     return totalAntrianCS;  
   }  
   public void Cetak(int kode){  
     JFrame f= new JFrame();  
     JLabel label1 = new JLabel("<html><center>----WAITING LIST----</center></html>");  
     label1.setBounds(75,50,150,10);  
     JLabel label2 = new JLabel();  
     label2.setBounds(75,70,150,100);  
     JLabel label3 = new JLabel("<html><center>----WAITING LIST----</center></html>");  
     label3.setBounds(75,180,150,30);      
     if(kode == 1 ){  
       label2.setText(String.format("<html><center><b>Nomor Antrian <br> <h3>%d</h3> <b><br> Loket Teller <br>Please Wait patienly<br></center></html>",totalAntrianTell));  
     }  
     else {  
       label2.setText(String.format("<html><center><b>Nomor Antrian <br><h3>%d</h3><b><br> Loket Customer Service <br>Please Wait patienly<br></html>",totalAntrianCS,detail));  
     }  
     f.add(label1);f.add(label2);f.add(label3);  
     f.setSize(300,300);  
     f.setLayout(null);   
     f.setVisible(true);   
   }  
 }  
 /**  
  * Write a description of class Pegawai here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 import javax.swing.JOptionPane;  
 public class Pegawai  
 {  
   private String namaPegawai,job;  
   private static int antrian;  
   private int noMeja;  
   public Pegawai(String kerja,String nama,int meja){  
     job = kerja;  
     namaPegawai = nama;  
     noMeja = meja;  
   }  
   public int getNomeja(){  
     return noMeja;  
   }  
   public void panggilAntrian(){  
     antrian++;  
     if(job == "teller"){  
       JOptionPane p1 = new JOptionPane();  
       p1.showMessageDialog(null,String.format("%d\nTeller %d",antrian,noMeja));  
     }  
     else {  
       JOptionPane p1 = new JOptionPane();  
       p1.showMessageDialog(null,String.format("%d\nCustomer Service %d",antrian,noMeja));  
     }  
   }  
 }  
 /**  
  * class yang membentuk GUI utama  
  *   
  * @author Adhityairvan  
  * @version 0.1  
  */  
 import java.util.Scanner;  
 public class MesinAntrian  
 {  
   public static int main(){  
     Scanner scanf = new Scanner(System.in);  
     Antrian queue = new Antrian();  
     System.out.printf("Welcome to Bank A\nPlease enter 1 for Customer page and 2 for Admin page\n");  
     int tampilan = scanf.nextInt();  
     if(tampilan == 1){  
       while(true){  
         System.out.printf("1.Antri Teller.\n2.Antri Customer Service.\n3.Exit\n");  
         int user = scanf.nextInt();  
         if(user == 1){  
           queue.tambahAntrian(1);  
           queue.Cetak(1);  
         }  
         else if (user == 2){  
           queue.tambahAntrian(2);  
           queue.Cetak(2);  
         }  
         else return 0;  
       }  
     }  
     else {  
       String nama = scanf.nextLine();  
       Pegawai pegawai1 = new Pegawai("teller",nama,1);  
       Pegawai pegawai2 = new Pegawai("CS","Adhitya",11);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(1);  
       queue.tambahAntrian(2);  
       queue.tambahAntrian(2);  
       queue.tambahAntrian(2);  
       queue.tambahAntrian(2);  
       while(true){  
         System.out.printf("1.Panggil antrian teller\n2.Penggil antrian cs\n3.exit");  
         int antri = scanf.nextInt();  
         if(antri == 1){  
           pegawai1.panggilAntrian();  
         }  
         else if(antri == 2) pegawai2.panggilAntrian();  
         else return 1;  
       }  
     }  
   }  
 }  

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 :
 /**  
  * 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..
Dalam java terdapat dua tipe saat mendeklarasi kan atribut atau method,yaitu default dan juga Static.
Static dalam java berarti attribut atau method tersebut akan tetap ada di memori dari awal membuka program sampai user menutup program tersebut,walaupun object yang memiliki method static ataupun atribut static belum di buat.Nilai dari tipe static akan tetap sama walaupun ada beberapa object dari kelas yang sama tempat terdapat nya atribut atau method static

Dibawah ini adalah contoh program yang mengimplementasi kan penggunaan tipe static

 /**  
  * Class untuk menghandle inputan tanggal  
  *   
  * @author adhityairvan   
  * @version 0.1  
  */  
 public class Date  
 {  
   private int bulan,tahun,tanggal;//deklarasi private variable for class Date  
   private static final int[] hariBulan =   
   { 0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31 };//jumlah hari dalam setiap bulan  
   public Date ( int day,int month,int year )  
   {  
     bulan = checkMonth(month);  
     tahun = year;  
     tanggal = checkDay( day );  
   }//konstruktor untuk class date,akan di cek apakah sesuai inputan hari dan bulan nya  
   private int checkMonth (int month )  
   {  
     if (month > 0 && month <13 ) return month;  
     else  
       throw new IllegalArgumentException ("Bulan harus berisi 1 - 12" );  
   }  
   private int checkDay ( int day )  
   {  
     if ( day > 0 && day <= hariBulan[bulan] ) return day;  
     if ( bulan == 2 && day == 29 && ( tahun % 400 == 0 || ( tahun % 4 == 0 && tahun % 100 != 0 ) ) )  
       return day;  
     else throw new IllegalArgumentException ("Salah memasukkan tanggal!!");  
   }  
   public String toString ()  
   {  
     return String.format( "%d/%d/%d", tanggal,bulan,tahun );  
   }  
 }  
 /**  
  * Write a description of class Karyawan here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 public class Karyawan  
 {  
   private String namaDepan;  
   private String namaBelakang;  
   private Date tanggalLahir;  
   private Date tanggalBekerja;  
   private static int jumlah = 0;  
   public Karyawan ( String depan, String belakang, Date lahir, Date kerja)  
   {  
     namaDepan = depan;  
     namaBelakang = belakang;  
     tanggalLahir = lahir;  
     tanggalBekerja = kerja;  
     jumlah ++;  
   }//konstruktor class karyawan  
   public String toString()  
   {  
     return String.format( "%s %s \nHired : %s\n Birth : %s\n"  
     ,namaDepan,namaBelakang,tanggalLahir,tanggalBekerja);  
   }  
   public static int getCount()  
   {  
     return jumlah;  
   }  
   public String getFirstName ()  
   {  
     return String.format("%s",namaDepan);  
   }  
   public String getLastName ()  
   {  
     return String.format ("%s",namaBelakang);  
   }  
 }  
 /**  
  * Write a description of class KaryawanTest here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 public class KaryawanTest  
 {  
   public static int main()  
   {  
     System.out.printf( "Employees before instantiation: %d\n",Karyawan.getCount());  
     Date lahir = new Date( 12,02, 1998 );  
     Date kerja = new Date( 15,05,2018 );  
     Karyawan k1 = new Karyawan("Adhitya","Irvansyah",lahir,kerja);  
     Date lahir2 = new Date( 25,10, 1987 );  
     Date kerja2 = new Date( 15,05,2018 );  
     Karyawan k2 = new Karyawan("Muhammad","Adhitya",lahir2,kerja2);  
     System.out.printf( "Jumlah karyawan setelah ditambah %d\n",Karyawan.getCount());  
     System.out.printf( "Jumlah karyawan pada object k1 setelah ditambah %d\n",k1.getCount());  
     System.out.printf( "Jumlah karyawan pada object k2 setelah ditambah %d\n",k2.getCount());  
     System.out.println ( k1 );  
     System.out.println ( k2 );  
     k1 = null;  
     k2 = null;  
     return 0;  
   }  
 }  

Dibawah ini merupakan satu contoh lagi untuk mengimplementasikan tipe static dalam melakukan import dalam java

 /**  
  * Write a description of class StaticImport here.  
  *   
  * @author (your name)   
  * @version (a version number or a date)  
  */  
 import static java.lang.Math.*;  
 public class StaticImport  
 {  
   public static void main()  
   {  
     System.out.printf( "sqrt( 900.0 ) = %.1f\n",sqrt( 900.0 ) );  
     System.out.printf( "ceil( -9.8 ) = %.1f\n",ceil( -9.8 ) );  
     System.out.printf( "E = %f\n", E );  
     System.out.printf( "PI = %f\n", PI );  
   }  
 }  

Sekian contoh tipe static dalam java.Sampai jumpa lagi di postingan saya berikutnya
       Enumeration adalah tipe dari sebuah kelas, makanya, kita tidak harus menuliskan keyword new untuk menginisialisasikan sebuah variable yang bertipe enum. Di pemrograman java, enumeration memiliki kelebihan dari bahasa pemrograman lainya,seperti dapat diberi konstruktor, adanya method dls.
 Enumeration juga adalah sebuah cara untuk mencacah element yang ada pada suatu kumpuan data pada vector.. * vector telah mengimplementasi koleksi interface, tapi kita bisa menemukan koleksi kita sendiri. * ArrayList dan Vectors, keduanya mendukung iterator.
Enumerasi memiliki dua pilihan :
·         nextElement() yang dapat mengembalikan objek selanjutnya pada koleksi
·         hasMoreElements () yang mengembalikan true, sampai objek terakhir dikembalikan oleh nextElement()


 /**  
  * Enumeration class Book - Enumerate and declare all the constant here  
  *   
  * @adhityairvan  
  * @version 0.1  
  */  
 public enum Book  
 {  
   NRT11( "Naruto Comic Volume 11","2008" ),  
   MBCS( "Mudah Belajar C Sharp","2015" ),  
   MTKSD( "Matematika Dasar Untuk Anak SD","2005" ),  
   JVPM( "Java Programming untuk Pemula","2016"),  
   TTDT2( "Tutorial bermain DotA 2","2017" ),  
   CMSKRP( "Cara Mudah Skripsi!","2015" );  
   //Deklarasi Konstanta  
   private final String judul,tahunCetak;  
   Book( String title,String year)  
   {  
     judul = title;  
     tahunCetak = year;  
   }//Konstruktor Class enum book  
   public String getTitle()  
   {  
     return judul;  
   }//mengakses variable private title  
   public String getYear()  
   {  
     return tahunCetak;  
   }   
 }  
 /**  
  * EnumBook - Mencoba class enum yang telah kita buat  
  *   
  * @adhityairvan  
  * @0.1  
  */  
 import java.util.EnumSet;  
 public class EnumBook  
 {  
   public static void main()  
   {  
     System.out.println (" Semua Buku :\n");  
     for ( Book buku : Book.values() )  
     {  
       System.out.printf( "%-10s%-45s%s\n", buku,buku.getTitle(),buku.getYear() );  
     }  
     System.out.println ( "Menampilkan Elemen antara 2 konstanta\n" );  
     for ( Book buku : EnumSet.range(Book.NRT11,Book.JVPM) )  
     {  
       System.out.printf( "%-10s%-45s%s\n", buku,buku.getTitle(),buku.getYear() );  
     }  
   }  
 }  
Composition dalam java.Pada java terdapat fitur untuk menyertakan object dari kelas lain kedalam kelas tertentu sebagai atribut,argument, maupun konstruktor dalam kelas terserbut.
Composition tidaklah terlalu sulit untuk dilakukan.Disini saya akan memberikan contoh kodingan program tentang composition.Dalam program saya terdapat 3 kelas yaitu kelas Date untuk menyimpan tanggal,kelas Karyawan untuk menyimpan data karyawan dan juga kelas KaryawanTest yang berisi method main untuk program yang saya buat

Berikut adalah kodingan nya

 /**  
  * Class untuk menghandle inputan tanggal  
  *   
  * @author adhityairvan   
  * @version 0.1  
  */  
 public class Date  
 {  
   private int bulan,tahun,tanggal;//deklarasi private variable for class Date  
   private static final int[] hariBulan =   
   { 0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31 };//jumlah hari dalam setiap bulan  
   public Date ( int day,int month,int year )  
   {  
     bulan = checkMonth(month);  
     tahun = year;  
     tanggal = checkDay( day );  
   }//konstruktor untuk class date,akan di cek apakah sesuai inputan hari dan bulan nya  
   private int checkMonth (int month )  
   {  
     if (month > 0 && month <13 ) return month;  
     else  
       throw new IllegalArgumentException ("Bulan harus berisi 1 - 12" );  
   }  
   private int checkDay ( int day )  
   {  
     if ( day > 0 && day <= hariBulan[bulan] ) return day;  
     if ( bulan == 2 && day == 29 && ( tahun % 400 == 0 || ( tahun % 4 == 0 && tahun % 100 != 0 ) ) )  
       return day;  
     else throw new IllegalArgumentException ("Salah memasukkan tanggal!!");  
   }  
   public String toString ()  
   {  
     return String.format( "%d/%d/%d", tanggal,bulan,tahun );  
   }  
 }  
 public class Karyawan  
 {  
   private String namaDepan;  
   private String namaBelakang;  
   private Date tanggalLahir;  
   private Date tanggalBekerja;  
   public Karyawan ( String depan, String belakang, Date lahir, Date kerja)  
   {  
     namaDepan = depan;  
     namaBelakang = belakang;  
     tanggalLahir = lahir;  
     tanggalBekerja = kerja;  
   }//konstruktor class karyawan  
   public String toString()  
   {  
     return String.format( "%s %s \nHired : %s\n Birth : %s\n"  
     ,namaDepan,namaBelakang,tanggalLahir,tanggalBekerja);  
   }  
 }  
 public class KaryawanTest  
 {  
   public static int main()  
   {  
     Date lahir = new Date( 12,02, 1998 );  
     Date kerja = new Date( 15,05,2018 );  
     System.out.println (kerja);  
     Karyawan k1 = new Karyawan("Adhitya","Irvansyah",lahir,kerja);  
     System.out.println( k1 );  
     return 0;  
   }  
 }  

Sekian contoh kodingan tentang komposisi dalam java.semoga bermanfaat dan sampai jumpa di post saya berikutnya.
Copyright © 2013 Hackers Community