Monday, January 27, 2014

OOP දෙවන පාඩම

Constructor Overloading & Method Overloading  
 OOP දෙවැනි පාඩමෙන් මුලින්ම මං කතාකරන්න බලාපොරොතු වෙන්නේ overloading ගැන.overloading ගැන කතාකරනකොට කොටස් දෙකක් ගැන කතාකරන්න වෙනවා.ඒවා තමයි
         1)constructor overloading
           2) method overloading
අපි මුලින්ම කතාකරමු constructor overloading ගැන.කෙටියෙන්ම කිවොත් constructor overloading කියන්නේ class එකකට constructors ගොඩක් තියෙන්න පුළුවන්.අපි කලින් පාඩමේදී කතාකලා constructor එකක් කියන්නේ class name එකින්ම තියන return type එකක් නැති method එකක් කියල.තව කිව්ව class එකක් හදනකොටම ජාවා වලින් default constructor එකක් automatically class එකකට add කරනවා කියල.නමුත් දැන් ඔයාලට හිතෙන්න ඕනේ එහෙනම් කොහොමද එක class එකකට constructors ගොඩක් යොදන්නේ කියල.එක වෙන්නේ මෙහෙමයි.අපි යොමුවෙමු කලින්ම කතාකරපු Box class එකට.
අපිට මේවර ඕනේ කරන Box එක කලින් එකට ටිකක් වෙනස්.Box එකක හදනකොට එක අවස්ථාවක length,higth ,width තුනම 0 වෙලා තියන්න ඕනේ,නමුත් තවත් වෙලාවක Box එකේ length,higth ,width තුනම object එක හදනකොට pass කරන අගයන් වලට අනුකුලව වෙනස්වෙන්න ඕනේ.එකට class එක ලියන විදිය අපි දැන් බලමු.
class Box{
      int length;
      int width;
      int height;
     
      Box(){// Default constructor method
            length=0;
            width=0;
            height=0;
            System.out.println("Box....**** Constructor ****");  
      }
     
      Box(int length,int width,int height){//overloaded constructor
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }
}
Main class
class demoBox {
      public static void main(String args[]){
            Box b=new Box();
            Box b1=new Box(10,10,10);
      }
}

මේ programme එක run කරලා බලුවම ඔයාලට පනේවි b කියල object එක හදනකොට run වෙන්නේ parameters නැති constructor එක,b1 කියල object එක හදනකොට run වෙන්නේ parameters 3 තියන constructor එක.ඒ අනුව පේනවා ඇති එකනමින් තියන constructors කිහිපයක් වූවත් class එකක include කරන්න පලුවන්.නමුත් ඒ include කරන constructors වල තියන parameters වෙනස් වෙන්න ඕනේ.දැන් පහත තියන constructors දෙක බලන්න.

Box(int length,int width,int height){//overloaded constructor 1
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }
Box(int length,int width,float height){//overloaded constructor 2
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }

මේ දෙකම එක class එකක් ඇතුලේ දැම්මොත් class එක compile වෙනවද?parameters 3 බැලුබැල්මට කියන්නේ නෑ කියල නමුත් අව්ලක් නැතුව compile & run වෙනවා.මොකක්ද දන්නවද හේතුව දෙවනි constructor එකේ අන්තිම parameter එක float එක නිසා parameters සර්වසම නෑ.මෙතනදීත් constructor overloading වෙනවා කිසි අව්ලක් නැතිව.අපි තව පොඩි උදාහරණයක් බලමු.

binaryTree(int left,double right){
            this.left=left;
            this.right=right;
      }

binaryTree(double right, int left){
            this.left=left;
            this.right=right;
      }

ඉහත දක්වල තියන constructors දෙක parameters ගාන වගේම data type ත් සමානයි නමුත් compile error නැතිව run වෙනවා.හේතුව තමයි parameter දෙකේ order එක වෙනස්.මේවගේ අවස්ථාවකදීත් වෙන්නේ constructor overloading තමයි.දැන් අපි පොඩි අලුත් වැඩක් කරමු constructor overloading වලට.අපිට constructor overloading බලාගන්න හොඳම විදිය තමයි ජාවා API එක.
API කීවට හරියටම තේරෙන්නේ නැතිව ඇති.ජාවා වල API එක කියන්නේ jdk එකත් එක්ක එන documentation එක .එක බලන විදිය පොඩ්ඩක් කියලම ඉන්නම්.මුලින්නම කරන්න ඕනේ API එක download කරගන්න එක හරි online API page එකට යනඑක තමයි.ඒ දෙකට ලින්ක් දෙක පහත දක්වල තියනවා.

http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html මේ ලින්ක් එකට ගහින් API එක download කරගන්න.download වෙන්නේ rar file එකකක්.එක extract කරලා එන file එකක් වෙබ් බ්‍රොව්සෙර්ට book mark එකක් තියාගන්න.

නැත්තම් http://docs.oracle.com/javase/7/docs/api/ මේ ලින්ක් එකට ගියාම online API එක බලන්න පලුවන්.
මේ API එකේ තියෙන්නේ ජාවා class ගැන summary එකක්.පහත දැක්වෙන්නේ ජාවා API එකේ Screen Shot එකක්.




දැන් නැවත constructure overloading වලට හැරෙමු.දැන් අපි ජාවා class එකක් API එකින් බලමු.



All classes වල JButton class එක හෝ ඔයාලට කැමති ඕනෑම class එකක් click කරන්න.ඊතලයෙන් පෙන්නලා තියන විදියට.එතකොට class එකේ විස්තර යතේ එන constructor summary යතේ එන්නේ එම class එකට තියන constructor ටික තමයි.ඔයාලට පේනවා ඇති මේකේ same name different signature තියන constructors ගොඩක් තියනවා.එතන කරලත් තියෙන්නේ constructor overloading.ඒ constructors ගැන වැඩිදුර තොරුතුරු ඕනනම් constructor එක උඩ click කරන්න.එතකොට විස්තර පෙන්නාවි.දැන් ඔයාලටම පලුවන් API එක use කරලා constructor overloading කරන්නේ කියල තනියම කරන්න.
දැන් අපි method overloading කරන විදිය ගැන ටිකක් බලමු.හරියටම කිවොත් constructor overloading නුයි method overloading නුයි අතර එචර වෙනසක් නෑ එකවගේම තමයි.මුලින්නම අපි method overloading වලට පොඩි උදාහරණයක් බලමු.

class Calculator {
      int sum;
      double sum1;

   public void add(int i, int j) {
         sum=i+j;
        System.out.println("Integer addition"); 
   } 
    
   public void add(int i, double j) { 
         sum1=i+j;
        System.out.println("Integer and double addition"); 
   } 
  
   public void add(double i, double j) { 
         sum1=i+j;
        System.out.println("Double addition"); 
   } 
  
මේ calculator class එකේ add method 3 තියනවා එකම parameters ගාන තියන.නමුත් parameters වල order එක හා data type වල එකිනෙකට වෙනස්කම් තියනවා.ඒ කියන්නේ එකම නමින් method ගොඩක් එකම class එකේ include කරලා තියනේ.නමුත් different signature වලින් මේ ක්‍රියාවලිය තමයි method overloading කියන්නේ.දැන් ඔයාලට තේරෙනවා ඇති constructor overloading වලදී වගේම තමයි method overloading වලදී වෙන්නෙත් කියල.පහත දක්කවා ඇති සම්පුර්ණ Box class එක බලන්න.
class Box{
      int length;
      int width;
      int height;
     
      Box(){
            length=1;
            width=1;
            height=1;
      }
     
      Box(int l, int w, int h){
            length=l;
            width=w;
            height=h;
      }

      void printVolume(){
            System.out.println("Volume is:"+getVolume());
      }
     
      void setValues(int l, int w, int h){
            length=l;
            width=w;
            height=h ;
      }
     
      void setLength(int l){
            length=l;
      }
     
      void setWidth(int w){
            width=w;
      }
     
      void setHeight(int h){
            height=h;  
      }
     
      int getVolume(){
            int volume;
            volume=length*width*height;
            return volume;
      }
     
      void printArea(){
            int a=getArea();
            System.out.println("Area of the Box is :"+a);
      }
     
      int getArea(){
            int a;
            a=2*(length*width+length*height+width*height);
            return a;
      }
     
      int getLength(){
            return length;
      }
     
      int getWidth(){
            return width;
      }
     
      int getHeight(){
            return height;
      }
     
      void printValues(){
            System.out.println("Length :"+length);
            System.out.println("Width  :"+width);
            System.out.println("Height :"+height);
      }
     
      void setValues(int l){
            length=l;
            width=l;
            height=l;
      }
}
ඒ Box class එකේ constructors මෙන්නම method overloading වෙලාතියනව.set values කියන method එක මුල් එකට parameters 3 යි අනිත් එකට parameters 1 යි.overload කරපු විදිය තේරෙනවා ඇතිනේ.දැන් ඔයාලට පලුවන් API class එකක් අරන් method overloading use කරලා තියනවිදිය බලාගන්න.

මං අන්තිමටම් කියන්න යන්නේ API එක download කරලා browser එකට bookmark එකක් දාගන්න විදිය.

1)මුලින්ම download page එකට ගිහින් liaison agreement එකට agree වෙන්න.
2)ඊ හිසෙන් දක්වා ඇති file එක මත click කරවිට download වීම ඇරඹේ.
3)download වූ file එක extract කරගන්න.
  
4)එතොකොට docs කියල folder එකක් ලැබෙනවා .
5)ඒ folder එක ඇතුලේ මේ වගේ file වගයක් තියනවා.
6)එකේ තියන API folder එකට යන්න.එකේ ඇති index කියල file එකක්,එක වෙබ් browser එකින් open කරන්න.
7)ඊටපස්සේ මේ page එක ලැබෙනවා.
 

8) දෙවෙනි රූපෙ තියන තරු සලකුණ click කරලා book mark එක දාගන්න.

මන් හිතන්නේ overloading ගැන වගේම API එක ගැනත් පූංචි දැනුමක් මේ පොස්ට් එකින් ලැබෙන්න ඇති කියල.අපි ඊළඟ පාඩමේදී inheritance ගැන කතාකරමු............. 

  

Saturday, January 25, 2014

OOP පළවන පාඩම



OOP (Object Oriented Programming)

Object Oriented Programming කියන්නේ මොකක්ද කියල අපි මුලින්ම බලමු.අපි c වගේ language  එකක් use කරනකොට මේ Object Oriented Programming හව්වෙන්නේ නෑ.මේකෙදි අපි programme ලියන්නේ object එකක් මත පදනම්වෙලා තමයි.object එකෙක් කියන්නේ මොකක්ද කියල දැම්ම තෙරන්නේ නෑ.ටිකක් පහලට යනකොට තේරේවි.අපි මුලින්ම පොඩි programme එකක් බලල ඉමු .
class DemoBox{
      public static void main(String args[]){
            int length;
            int width;
            int height;
           
            length=12;
            width=5;
            height=3;
           
            int volume;
            volume=length*width*height;
            System.out.println("Volume is:"+volume);

      }
}
මේ programme එකේ වෙන්නේ පෙට්ට්යක ගණත්වය calculate කරන එක තමයි .නමුත් අපිට තව පෙට්ටියක ගණත්වය හොයන්න ඕන නම් programme එක මෙහෙම ලියන්න වෙනවා.
class DemoBox{
      public static void main(String args[]){
            int length;
            int width;
            int height;
           
            length=12;
            width=5;
            height=3;
           
            int volume;
            volume=length*width*height;
            System.out.println("Volume is:"+volume);

            int length1;
            int width1;
            int height1;
           
            length1=120;
            width1=50;
            height1=30;
           
            int volume1;
            volume1=length1*width1*height1;
            System.out.println("Volume is:"+volume1);
      }
}
බලන්න මෙහෙම ලිව්වම code එක ගොඩක් දික්වනවා නේද?මෙන්න මේ වගේ ප්‍රශ්න වලදී තමයි අපිට oop ඕන වෙන්නේ.oop ගැන කතාකරනකොට එන ප්‍රදානම දේ තමයි class එක.අපි දැන් බලමු class එකක් කියන්නේ මොකක්ද කියල.

ජාවා class

ජාවා class එකක් කියන දේ මුලිකවම කිව්වොත් යම්කිසි දෙයක් කරගන්න හදාගන්න ටෙම්ප්ලේට් එකක් තමයි.ඒ ටෙම්ප්ලේට්ටෙම්ප්ලටෙ එකට තියානවා නමක්,attributes ,methods යන ඒවා.එක මෙන්න මේ වගේ රූපයකින් පෙන්නනන්න පුළුවන්.මේකේ පෙන්නලා තියන්නේ Box class එක.
Class Name:-Box
Attributes
height
Width
length
Methods
Volume
Area

හැම class එකකටම නමක් තියෙන්න ඕන.ඒවගේම Attribute කියන්නේ class එකේ අඩංගු මුලික futers ටික.methods කියන්නේ Attributes හා වනත් දේවල් use කරලා කරන්න විවිද වැඩටික.දැන් අපි බලමු උඩින් ලියපු programme එකට අදාල class එක ලියන්නේ කොහොමද කියල.
class Box{//class Name
      int length;//attributs
      int width;
      int height;
     
}
අපි දැන් බලමු හදාගත්තු class එක use කරන හැටි.
class DemoBox{
      public static void main(String args[]){//main method
            Box b1;//line 1.1
            b1=new Box();//line 1.2 
           
            System.out.println(b1);//line 2
           
            b1.length=12;
            b1.width=5;
            b1.height=3;
           
            int volume;
            volume=b1.length*b1.width*b1.height;
            System.out.println("Volume is:"+volume);
      }
}
මුලින්ම කියන්න ඕනේ programme එකක් run වෙන්න නම් main method එකක් තියන class එකක් ඇතුලේ ඒ code ටික තියෙන්න ඕන.නැතිනම් run වෙන්නේ නෑ.line 1.1 දි කරන්නේ Box කියන data type එකෙන් variable එකක් හදන එක.line 1.2 කින් කරන්නේ එම memory location එක refer කරන එක.අපි line 2 වගේ ප්‍රින්ට් කරලා බැලුවොත් පෙන්නේනේ hexadecimal value එකක්.ඒ පෙන්නේනේ memory address එක ඒ object එක refer කරන.ඊටපස්සේ අපිට පුළුවන් object එක use කරලා volume එක calculate කරන්න.දැන් අපිට ඕනේ කියල හිතමු තව Box එකක volume එක හොයන්න අපි කරන්න ඕනේ තව බොෂ් type එකේ object එකක් හදන එක විතරයි.එක පහත පෙන්නලා තියනවා.
Box b2=new Box();
           
b2.length=15;
b2.width=7;
b2.height=4;
Java methods
අපි දැන් බලමු ජාවා method එකක් කියන්නේ මොකක්ද කියල.method එකක් කියන්නේ ,class එකක් කියන්නේ මිනිසෙක් කියල හිතමු attribute කියන්නේ ම්නිසාගේ අත්,ඇස ,පාද,කන් වගේ ඒවාට method කියන්නේ මිනිසාට දුහන්න පලුවන් එහෙනම් run කියන්නේ method එකක්.මං හිතන්නේ method එකක් කියන්නේ මොකක්ද කියල ඔයාලට මුලික වශයෙන් තේරෙන්න ඇති.දැන් අපි බලමු ජාවා method එකක structure එක මොකක්ද කියල.

dataType method_name(parameters){
//method body
      Return ………
}

dataType එකට අපිට මුලික data type යොදාගන්න පලුවන්.ඒ හැර void use කරන්න පලුවන්.

ඉහත අපි ලියපු Box class එකට volume එක calculate කරන්න අපි ජාවා method එකක් ලියමු.

int volume(){
      int volume=length*width*hight;
      return volume;
}
return කරන්නේ method එකේ data type එකේ ජාතියේ value එකක් තමයි.දැන් අපි එක main method එකේදී call කරන විදිය බලමු.
Box b=new Box();
b.length=10;
b.hight=2;
b.width=5;
b.volume();
මං පහත දීල තියන programme එකින් කරන්නේ slender එකක area එකයි volume එකයි හොයන එක.
class Slinder {
      int hight;
      int radius;
      final double pi=3.14;
     
      double area(){//method without Parameters
            double are=(2*pi*radius*hight)+(pi*radius*radius);
            return are;
      }
     
      double volume(){
            double vol=pi*radius*radius*hight;
            return vol;
      }
     
      double area(double pi,int radius,int hight){//method with Parameters
            double are=(2*pi*radius*hight)+(pi*radius*radius);
            return are;
      }
     
      double volume(double pi,int radius,int hight){
            double vol=pi*radius*radius*hight;
            return vol;
      }
     
}

Main class එක
public class demoSylender {

      /**
       * @param args
       */
      public static void main(String[] args) {
            Slinder s=new Slinder();
            s.hight=15;
            s.radius=10;
            s.area();//Calling without parameters
            s.volume();
            s.area(3.14, 50, 20);//calling with Parameters
            s.volume(3.14, 5, 32);

      }

}
මේකේ මං පෙන්නලා තියනවා method එකක් parameters ඇතිව හා නැතිව හදන්නේ කොහොමද කියල.අපිට method එකක අපිට අවශ ප්‍රමාණයක් parameters යොදන්න පලුවන්.නමුත් හොඳට මතක තියාගන්න ඕන method එක call කරනකොට parameters යොදපු අනුව call කරන්න ඕන. parameters තියන area method එක call කරවිට අගයන් ගන්නේ parameters වලට pass වෙච්ච ඒවා.attributes නෙමේ.නමුත් this key word එක යොදාගත්තොත් යොදාගන්නේ attributes වල අගයන්.පහත method එක බලන්න.

double area(double pi,int radius,int hight){//method with Parameters
 double are;
are=(2*this.pi*this.radius*this.hight)+(this.pi*this.radius*this.radius);
            return are;
      }

තවදුරත් get ,set method වලින් class එකට values set කරන විදිය තේරුම් ගන්න පහත දැක්වෙන class එක උදව් වේවි.

class Counter {
      int counter=0;
     
      void inclement(){
            counter++;
      }
      void declementer(){
            counter--;
      }
      void setCounter(int c){
            counter=c;
      }
      int getCounter(){
            return counter;
      }
}
 Constructor Method
Constructor method එක කියන්නේ return type එකක් නැති class එකේ නමටම සමාන method name එකක් ඇති method එකකට.සාමාන්යෙන් අපි class එකක් හදනකොටම automatically constructor method එකක් ජාවා වලින් input කරනවා.එක තමයි අපි object එකක් හදනකොට මෙහෙම call කරන්නේ.(Box b1=new Box())”new Box()”අන්න ඒක තමයි default constructor එක.අපි දැන් බලමු කොහොමද Box class එකට constructor method එක දන්නේ කියල.

class Box{
      int length;
      int width;
      int height;
     
      Box(){//Constructor method
            length=1;
            width=1;
            height=1;
            System.out.println("Box....");     
      }
     
      void printVolume(){
            System.out.println("Volume is:"+getVolume());
      }
     
      void setValues(int l, int w, int h){
            length=l;
            width=w;
            height=h ;
      }
     
      void setLength(int l){
            length=l;
      }
     
      void setWidth(int w){
            width=w;
      }
     
      void setHeight(int h){
            height=h;  
      }
     
      int getVolume(){
            int volume;
            volume=length*width*height;
            return volume;
      }
     
      void printArea(){
            int a=getArea();
            System.out.println("Area of the Box is :"+a);
      }
     
      int getArea(){
            int a;
            a=2*(length*width+length*height+width*height);
            return a;
      }
     
      int getLength(){
            return length;
      }
     
      int getWidth(){
            return width;
      }
     
      int getHeight(){
            return height;
      }
     
      void printValues(){
            System.out.println("Length :"+length);
            System.out.println("Width  :"+width);
            System.out.println("Height :"+height);
      }
}
මේකේ main class එක පහත දැක්වේ.
public class demoBox {

      /**
       * @param args
       */
      public static void main(String[] args) {
            Box s=new Box();
      }

}
මේකේ output එක තමයි මේ
අපි කරලා තියෙන්නේ object එක හදන එක විතරයි.නමුත් Box....**** Constructor **** කියල ප්‍රින්ට් වනවා.එහම වෙන්නේ එතකොට constructor එක call වන නිසා.එහමනම් දැන් ඔයලට තේරෙන්නවා ඇති අපි object එක හදනවා කියල කරන්නේ class එකේ constructure method එකක් call කරන එක කියල.අපිට ඕනෙනම් pharameeters යොදලත් constructor එකක් හදන්න පලුවන්.ඒගැන වැඩිදුරට overloding වලදී කතාකරමු.