Tuesday, February 28, 2012

Encapsulation in Java

Assalamualaikum.
Today i will demonstrate how to use Encapsulation in Java.
EnCAPSULation = Capsule means it is data hiding.

It is 1 of 4 fundamental in OOP concept. The others are Inheritance, Polymorphism and Abstraction.
Google it to know more :)

In this tutorial we will create two java file

Customer.java
public class Customer {
    private String customerID;
    private String customerName;
    private String address;
    private int pinCode;
     
    public String getCustomerID() {
        return customerID;
        }
         
    public void setCustomerID (String myCustomerID) {
        customerID = myCustomerID;
        }

    public String getCustomerName(){
        return customerName;
        }
         
    public void setCustomerName (String myCustomerName){
        customerName = myCustomerName;
        }   

    public String getAddress () {
        return address;
        }
         
    public void setAddress(String myAddress) {
        address = myAddress;
        }
                 
    public int getPinCode(){
        return pinCode;
        }
     
    public void setPinCode(int myPinCode){
        pinCode = myPinCode;
        }
    }


EncapsulationTry.java
 public class EncapsulationTry{
 
    public static void main (String [] args){
    // Creating object of Customer Class
    Customer customer = new Customer();
 
    /* Calling setter methods for setting the values of instance variables */
    customer.setCustomerID ("0123");
    customer.setCustomerName ("MFZ");
    customer.setAddress ("Seremban,Negeri Sembilan, Malaysia");
    customer.setPinCode (70400);
    /* Calling getter methods for printing the values of instance variables */
 
    System.out.println ("Customer ID :" + " " + customer.getCustomerID());
    System.out.println ("Customer Name:" + " " +  customer.getCustomerName());
    System.out.println ("Customer Address:" + " " + customer.getAddress());
    System.out.println ("Customer Pin Code:" + " " + customer.getPinCode());
    }
}



Then compile all the java file and run the EncapsulationTry.class
Analyse the result and code, InsyaAllah you can get the point. 


  

Tuesday, February 21, 2012

Assalamualaikum.java (Versi Linux)


Assalamualaikum ( Program JAVA pertama)

Java merupakan antara bahasa programming. Boleh digunakan dalam mana2 platform kerana menggunakan JVM.

Dalam tutorial ini, kita akan menggunakan sistem Linux Ubuntu 11.10 dan Text Editor. Dalam bahasa JAVA penggunaan huruf amat sensitif. Pastikan anda menaip dengan tepat.

Taip 'code' di bawah dalam Text Editor dan 'save' sebagai 'Assalamualaikum.java'


public class Assalamualaikum {

public static void main (String [] args) {
System.out.println ("Assalamualaikum daripada Galaxy!!!");
}
}
Dalam tutorial ini, semua file akan di 'save' dalam '~/Desktop/Galaxy'


'Compile' dan 'run'
Mulakan terminal dan 'compile' Assalamualaikum.java


mfz@mfz-Galaxy:~/Desktop/galaxy$ javac Assalamualaikum.java
'Assalamualaikum.class' akan di'create'. '.class' yang mampu untuk di 'run' di mane2 platform.
Kemudian 'Run' program tersebuat dan anda akan dapat lihat hasilnya.



mfz@mfz-Galaxy:~/Desktop/galaxy$ java Assalamualaikum
Assalamualaikum daripada Galaxy!!!