Class, Field, dan Method

November 2, 2009

Biasanya, tiap Class memiliki Field dan Method. Field kadang disebut sebagai properties, sedangkan Method disebut sebagai behaviour. Sedangkan konstruktor adalah deklarasi Class yang memiliki Method dan nama  yang sama dengan Class.

Misalkan suatu Class memiliki diagram UML seperti ini:

umlclass-low

Maka, dapat dibentuk menjadi source code java kurang lebih seperti ini:

//Source Filename: CharStack.java
public class CharStack{     //Class name
    //Class Declarations:

    //Fields:
    private char[] stackArray;  //The array implementing the stack.
    private int    topOfStack;  //The top of the stack;

    //Constructor:
    public CharStack(int capacity){
        stackArray = new char[capacity];
        topOfStack = -1;
    }

    //Methods:
    public void push(char element)  { stackArray[++topOfStack] = element;}
    public char pop()               { return stackArray[topOfStack--];}
    public char peek()              { return stackArray[topOfStack];}
    public boolean isEmpty()        { return topOfStack < 0;}
    public boolean isFull()         { return topOfStack == stackArray.length = 1;}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: