Monday, August 10, 2015

Lesson 11


ඉතින් යාලුවනේ කොහොමද ගොඩ කාලෙකින් නේද.... හිතනවා දැන් ජාවා අමතක වෙලා නැතුව ඇති කියල අපේ කස්ටියට හික් හික්....

අද මම කියල දෙන්න යන්නේ ඔයාලට method එකක් overloading කරන විදිය. ඉතාම සරල දෙයක් මෙතන තියෙන්නේ මම උදාහරණයකින් ම පැහැදිලි කරන්නම් කෝ ඔයලට,


public class ExampleMethodOverloading{

   public static void main(String[] args) {
       
      int a = 10;
      
      int b = 5;
      
      double c = 7.5;
      
      double d = 9.3;
      
      int result1 = minFunction(a, b);
    
      double result2 = minFunction(c, d);
      
      System.out.println("Minimum Value = " + result1);
      
      System.out.println("Minimum Value = " + result2);
      
   }



   public static int minFunction(int n1, int n2) {
       
      int min;
      
      if (n1 > n2)
          
         min = n2;
      
      else
          
         min = n1;

      return min; 
      
   }
   
   public static double minFunction(double n1, double n2) {
       
     double min;
     
      if (n1 > n2)
          
         min = n2;
      
      else
          
         min = n1;

      return min; 
      
   }
   
}



බලන්නකෝ ඔයාලට රෙසුල්ට් එක විදියට එන්න ඕනේ 

Minimum Value = 5
Minimum Value = 7.5


අපි දැන් බලමු කොහොමද මේ උත්තරය අපිට එන්නේ කියලා.

මුලින් ම කරලා තියෙන්නේ මම variables ටික define කරලා තියෙන එකයි.
 int variable දෙකකුයි double variable දෙකකුයි.


 ඊටපස්සේ බලන්න මම හදල තියනවා methods දෙකක් එකම නමින් parameters දෙකක් දල හැබැයි එකේ තියන parameters දෙක එකට එකක් වෙනස්.

බලන්නකෝ මම methods දෙකේම if  statement එකකින් check කරලා තියනවා මොකක්ද මින් value එක කියල.

ඊටපස්සේ ඉතින් methods දෙක ප්‍රින්ට් කරලා තියනවා තව variable දෙකකට assign කරලා. ඔන්න ඕක තමයි method overloading කියල තියන කතාව.


ඊටපස්සේ මම කියල දෙන්න යන්නේ ජවා වල command line arguments ගැන පොඩි කතාවක්. මේක හරිම සරල දෙයක්

command line arguments ඕනේ වෙන්නේ ඔයාලට ජාවා program එක run වෙනකොට මොනවා හරි arguments pass කරන්න ඕනේ නම් pass කරන්නේ command line arguments හරහා තමයි.

බලන්නකෝ මේ උදාහරණයත් එහෙනම්,


public class CommandLine {

   public static void main(String args[]){ 
       
      for(int i=0; i<args.length; i++){
          
         System.out.println("args[" + i + "]: " + args[i]);
         
      }
      
   }
   
}





ඔයාල මේක compile කරලා run කරනකොට දෙන්න ඕනේ මෙන්න මෙහෙම commands සෙට් එකක්

 this is a java command line arguments 200 -100



ඔයාලට රෙසුල්ට් එක විදියට එතකොට මෙන්න මේවගේ එකක් එන්න ඕනේ.


args[0]: this
args[1]: is
args[2]: a
args[3]: java
args[4]: command
args[5]: line
args[6]: arguments
args[7]: 200
args[8]: -100





කොහොමද එල කිරි එකට වැඩ නේද :D ;) 

අපි දැන් බලමු ජාවා වල එන constructors ගැන පොඩි විස්තරයක්.
constructors initialize වෙන්නේ object එකක් හැදුනම විතරයි.
constructors උත් හරියට නිකන් අපි ලියන class වගේම තමයි. විශේෂ ලක්ෂණය තමයි constructor එකකට විශේෂ return type එකක් නෑ

අපි constructors හැදුවේ  නැති උනත් හැම class එකටම default
constructor එකක් තියනවා.


 බලන්නකෝ මේ උදාහරණය එහෙනම්,




class MyClass {
    
    int x;

    MyClass(int i) {
        
        x = i;
        
    }
    
}

public class TestClass {

    public static void main(String args[]) {
        
        MyClass t1 = new MyClass(30);
        
        MyClass t2 = new MyClass(50);
        
        System.out.println(t1.x + " " + t2.x);
        
    }
    
}





Result එක විදියට ඔයාලට එන්න ඕනේ 30 50 කියල. 




ඔයාලට තවත් වැදගත් වෙන දෙයක් තමයි දැන් මම කිය්ටල දෙන්න යන්නේ ඒ තමයි finalize method එකමේ method එක ඔයාලගේ code එකේ කොහේ හරි තිබුනොත් අනිවා run වෙන method එකක් තමයි මේක. ගොඩක් වෙලාවට මේක use කරන්නේ database connection එකක් වගේ close කරන්න වගේ වැඩකට තමයි.

බලන්නකෝ මෙන්න මෙහෙම තමයි මේක අපි ලියන්නේ.

protected void finalize( )
 
{
 
   // finalization code here
 
}


 එහෙනම් ඉතින් අදට මට යන්න වෙලාව හරි අයෙත් අලුත් පොස්ටුවක් අරගෙන නැවතත් හමුවෙමු.  හැමෝටම ජයවේවා.... 








 

Friday, June 19, 2015

Lesson 10


ඉතින් යාලුවනේ ඔන්න අදත් මම ඔයාලට තවත් අලුත් පාඩමක් අරගෙන ආවා. ටිකක් පරක්කු උනාට sorry වෙන්න ඕනේ මේ දවස් වල අම්බානකට වැඩ වලට හිරවෙලා ඉන්නේ හික් හික්.... ;)

ඉතින් අද මම කියල දෙන්න යන්නේ java වල අපිට හම්බවෙන Methods ගැන පොඩි විස්තරයක් ඔයාලට මතක ඇති කලින් පාඩම් වලදී අපි කරපු access modifiers අපිට ඒවා මෙතැනදී ඕනේ වෙනවා. methods වලටම අපි කියනවා Procedures or Functions කියලත් Procedures කියන්නේ value එකක් return කරන්නේ නෑ Functions කියල කියන්නේ අපි value එකක් return කරන ඒවාට, 

අපි එහෙනම් බලමු කොහොමද method එකක් create කරගන්නේ කියල

බලන්නකෝ syntax එක එහෙනම්,



modifier returnType nameOfMethod (Parameter List) {

 // method body

}




  • modifier: It defines the access type of the method and it is optional to use. 
  • returnType: Method may return a value. 
  • nameOfMethod: This is the method name. The method signature consists of the method name and the parameter list. 
  • Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters. 
  • method body: The method body defines what the method does with statements.


මම ඔයාලට මේ syntax එකේ විදියට හදපු මේ method එක විස්තර කරලා දෙන්නම් කෝ


public static int funcTest(int a, int b){
 //Method Body
}
බලන්නකෝ මේ method එකේ 


  •     public static : modifier
  •     int : return type 
  •     funcTest : function name 
  •     a, b : formal parameters 
  •     int a, int b : list of parameters


NOTE:- මතක තියාගන්න method එකක name එක ලියනකොට ලියන්න ඕනේ පලවෙනි word එකේ පලවෙනි අකුර simple දෙවැනි word එකේ ඉදන් capital වලින්.

අපි දැන් එහෙනම් බලමු කොහොමද method එකක් call කරන්නේ කියල එහෙනම් හදාගතට වැඩක් න නේ call කරගන්න බැරි නම් :P හික් හික්.....


method එකක් call වෙන විදි දෙකක් තියනවා එකක් තමයි 
  • method returns a value
  • returning nothing (no return value)

method එකක් call කරහම වෙන දේවල් දෙකක් තියනවා ඒ තමයි
  • return statement is executed.
  • reaches the method ending closing brace.
  වැඩේ ගොඩ නේද එහෙනම් එහෙනම් අපි දැන් උදාහරණයක් කරලා බලමු.




public class ExampleMaxNumber{
   
   public static void main(String[] args) {
       
      int a = 50;
      
      int b = 25;
      
      int c = maxFunction(a, b);
      
      System.out.println("Max Value = " + c);
      
   }
   
   public static int maxFunction(int n1, int n2) {
       
      int min;
      
      if (n1 < n2)
          
         min = n2;
      
      else
          
         min = n1;

      return min; 
      
   }
   
}




ඔයාලට මේකේ output එක එන්න ඕනේ 
Max Value = 50 කියල.


කොහොමද එළකිරි වගේ වැඩ නේද ;) 


ඊටපස්සේ මම ඔයාලට කියල දෙන්න යන්නේ void keyword එක ගැන පොඩි විස්තරයක් මේ keyword එක use කරහම වෙන්නේ method එකෙන් value එකක් return වෙන්නේ නෑ.
 
බලන්නකෝ එහෙනම් මේ example එක



public class ExampleVoid {

    public static void main(String[] args) {
        
        methodRankPoints(155.5);
        
    }

    public static void methodRankPoints(double points) {
        
        if (points >= 100.2) {
            
            System.out.println("Rank:A1");
            
        } else if (points >= 50.3) {
            
            System.out.println("Rank:A2");
            
        } else {
            
            System.out.println("Rank:A3");
            
        }
        
    }
    
}



 

මේකේ result එක විදියට එන්න ඕනේ 
Rank:A1 කොහොමද වැඩේ එළකිරි එකට වැඩ නේද  හික් හික්...... ;)
 
එහෙනම් ඉතින් අදට යන්න වෙලාව හරි methods වල ඉතුරු ටික ඉදිරි පාඩම් වලදී කියල දෙන්නම් එහෙනම් ඉතින් මම ගියා..... 


මතක ඇතුව කොමෙන්ටුවකුහ් දාලම යන්න හොදේ.........





Tuesday, May 26, 2015

Lesson 09


ඉතින් යාලුවනේ ඔන්න අදත් මම තවත් අලුත් පාඩමක් අරගෙන අවා අද මම ඔයාලට කියල දෙන්න යන්නේ java වල එන arrays ගැන පොඩි හැදින්වීමක්. මේකෙත් ඉතින් මහලොකු දෙයක් නෑ අපි කලින් කරපු variable define කරපු එකටම අගට කොටු වරහන් දෙකක් දාන එක විතරයි කරන්නේ හික් හික්.... එහෙම නම් ඉතින් හා හා පුරා කියල වැඩේ පටන්ගමුකෝ කොහොමද කියල බලන්න එක්කම.

Array එකක් කියන්නේ එකමජතියේ variables සෙට් එකක් තියාගෙන ඉන්න object එකක් කිවුවත් වරදක් නෑ.

Array එකක තියන variable එකකට අපි කියන්නේ element එකක් කියලයි.

වැදගත් ම කරුණ තමයි arrays have zero based index :D හැබැටම මොන හරුපයක් ද ඒ හික් හික් :P 

ඒ කියන්නේ array එකක් පටන් ගන්නේ 0 කියන ස්ථානයෙන් කියල ඊටපස්සේ එක, දෙක, තුන ඔහොම යනවා යනවා යනවා ඉතින් හික් හික් :P 

අපි බලමුකෝ එහෙනම් කොහොමද මේකේ syntax එක තියෙන්නේ කියල 



dataType[] arrayRefVar;   // preferred way.

      or

dataType arrayRefVar[];  //  works but not preferred way.




Programmer කෙනෙක් විදියට මම නම් recommend කරන්නේ පලවෙනි විදිය use කරනවානම් ගොඩක් හොදයි කියල. 

syntax එක දිහා බලාගෙන කන්නද එහෙනම් අපි ඒ විදියට එකක්හදල බලමු.




int[] myList;         // preferred way.

or

int myList[];         //  works but not preferred way.




ඔන්න ඔහොමයි වැඩේ වෙන්නේ හික් හික් වැඩේ ඉතා සරලයි නේද ;) 


එහෙනම් ඉතින්  දැන් අපි බලමු අපි ඔය කියපු එක දල array එක කොහොමද හරියට හදාගන්නේ කියල.

වැඩි දෙයක් නෑ හික් හික් එහෙටයි මෙහෙටයි අර data type එක දල මැදට  new කියල කැල්ලක් එකතු කරනවා :P (ගොඩක් තේරුණා නේ..... හික්ස්)
 
එහෙනම් තේරුනේ නැති උදවිය දැන් බලාගන්නකෝ කොහොමද වැඩේ වෙන්නේ කියල.



int marks[]=new int[10]; දැන් තේරුණා නේ එහෙනම් නේද එහෙටයි මෙහෙටයි data type එක දැම්ම මැදට new කියල කැල්ලක් දැම්ම වැඩේ ගොඩ දැන් අපි array එක හදාගෙන ඉවරයි.
ඔයාලට ඕනේ නම් මෙහෙමත් ඕක කරගන්න පුළුවන්


String[] names;
names=new String[10];

එහෙනම් වැඩේ තේරුණා නේ කස්ටියටම 
අපි එහෙනම් දැන් බලමු මොනවද මේ array එකේ තියන ඒවා කියල මේක බලන්නකෝ එතකොට තේරෙයි සේරම ටික 


 

ඒ වැඩෙත් ගොඩ එහෙනම්

දැන් ඉතින් කරන්න තියෙන්නේ එකින් එක values add කරගෙන යන එකයි. 





public class TestArray {
    
    public static void main(String[] args) {
        
        String[] names;
        
        names = new String[5];
        
        names[0] = "Danna";
        
        names[1] = "Sinhalen";
        
        names[2] = "Java";
        
        names[3] = "Igena";
        
        names[4] = "Gamu";
        
        System.out.print(names[0] + " ");
        
        System.out.print(names[1] + " ");
        
        System.out.print(names[2] + " ");
        
        System.out.print(names[3] + " ");
        
        System.out.print(names[4] + ".");
        
    }
    
}




ඔයාලට result එක විදියට එන්න ඕනේ Danna Sinhalen Java Igena Gamu. කියල කොහොමද ලස්සනට වැඩේ වෙනවා නේද ;)

ඒ වගේම ඔයාලට මෙහෙමත් කරන්න පුළුවන් 




public class TestArray {

    public static void main(String[] args) {

        String[] names = { "DANNA", "SINHALEN", "JAVA", "IGENA", "GAMU" };

        System.out.print(names[0] + " ");

        System.out.print(names[1] + " ");

        System.out.print(names[2] + " ");

        System.out.print(names[3] + " ");

        System.out.print(names[4] + ".");

    }

}




ඔයාලට result එක විදියට එන්න ඕනේ DANNA SINHALEN JAVA IGENA GAMU.

මේ දෙවිදියටම ඔයාලට ලස්සනට වැඩේ කරන්න පුළුවන්
ඒ වගේම ඔයාල දැනගන්න ඕනේ තව වැදගත් දෙයක් තියනවා ඒ තමයි array එකක size එක දැනගන්නේ කොහොමද කියල 
බලන්නකෝ එහෙනම් එකත් කරන විදිය



public class TestArray {

    public static void main(String[] args) {

        String[] names = { "DANNA", "SINHALEN", "JAVA", "IGENA", "GAMU" };

        System.out.println(names.length);

    }

}



මේකෙදි ඔයාලට result එක විදියට එන්න ඕනේ 5 කියල එක එන්නේ මම හදල තියන array එකේ values තියෙන්නේ 5 නිසා ඔයාල තව values add කරලා බලන්න මොකද වෙන්නේ කියල.
ඊටපස්සේ ඉතින් මම කියල දෙන්න යන්නේ 2D Arrays ගැන
හැබැටම මොනවද මේ 2D arrays කියල කියන්නේ 


මේකෙදි කරන්නේ arrays ඇතුලේ තව arrays දාන එකයි 


එචර අමාරු නෑ කරලා බලන්නකෝ එහෙනම්,



public class Test2DArray {

    public static void main(String[] args) {

        int rows = 6;
        
        int columns = 5;

        int i, j;

        int[][] aryNumbers = new int[6][5];

        aryNumbers[0][0] = 10;
        
        aryNumbers[0][1] = 50;
        
        aryNumbers[0][2] = 20;
        
        aryNumbers[0][3] = 12;
        
        aryNumbers[0][4] = 11;
        
        
        aryNumbers[1][0] = 20;
        
        aryNumbers[1][1] = 10;
        
        aryNumbers[1][2] = 40;
        
        aryNumbers[1][3] = 24;
        
        aryNumbers[1][4] = 22;
        
        
        
        
        aryNumbers[2][0] = 30;
        
        aryNumbers[2][1] = 15;
        
        aryNumbers[2][2] = 60;
        
        aryNumbers[2][3] = 36;
        
        aryNumbers[2][4] = 33;
        
        
        
        
        aryNumbers[3][0] = 40;
        
        aryNumbers[3][1] = 20;
        
        aryNumbers[3][2] = 80;
        
        aryNumbers[3][3] = 48;
        
        aryNumbers[3][4] = 44;
        
        
        
        
        aryNumbers[4][0] = 50;
        
        aryNumbers[4][1] = 25;
        
        aryNumbers[4][2] = 10;
        
        aryNumbers[4][3] = 60;
        
        aryNumbers[4][4] = 55;

        
        
        
        aryNumbers[5][0] = 60;
        
        aryNumbers[5][1] = 30;
        
        aryNumbers[5][2] = 12;
        
        aryNumbers[5][3] = 72;
        
        aryNumbers[5][4] = 66;

        
        for (i = 0; i < rows; i++) {
            
            for (j = 0; j < columns; j++) {
                
                System.out.print(aryNumbers[i][j] + " ");
                
            }
            
            System.out.println("");
            
        }
        

    }
    

}




මේකේ result එක ඔයාලට මෙන්න මෙහෙම ලස්සනට එන්න ඕනේ
10 50 20 12 11
20 10 40 24 22
30 15 60 36 33
40 20 80 48 44
50 25 10 60 55
60 30 12 72 66


කොහොමද වැඩේ :D
එහෙනම් ඉතින් දැන් යන්න වෙලාව හරි ඔයාලට පුළුවන් මේ වගේම 3D array එකකුත් කරලම බලන්න එකට values 3ක් එනවා

int[][][] aryNumbers = new int[6][5][4];

මෙන්න මේ වගේ එකක් තමයි එතකොට එන්නේ එහෙනම් එක ට්‍රය් කරලම බලන්නකෝ කොහොමද කියල 

එහෙනම් ඉතින් මම යනවා අයෙත් තවත් අලුත් පාඩමක් අරගෙන එන්නම් එතකම් හැමෝටම ජයවේවා!!!

 




Thursday, May 21, 2015

Lesson 08


ඔන්න ඉතින් යාලුවනේ අදත් මම ඔයාලට තවත් වැදගත් වෙන පාඩමක් අරගෙන අවා අද මම ඔයාලට කියල දෙන්න යන්නේ java වල අපිට හම්බවෙන if ststements ගැන පොඩි විස්තරයක්.

එහෙමනම් ඉතින් අපි බලමු මොනවාද මේ if ststements කියල.

 if statements ගත්තහම අපිට වර්ග කීපයකට බෙදන්න පුළුවන් ඒවා තමයි,


  • The if Statement 
  • The if...else Statement 
  • The if...else if...else Statement 
  • Nested if...else Statement


 අපි මුලින් ම බලමු The if Statement එක ගැන,

මේකේ ඉතාමත් සරල එකක් මේකෙදි වෙන්නේ Boolean expression එක true වෙනවනම් statement එක run වෙන එකයි.

බලන්නකෝ syntax එක,



if(Boolean_expression)

{

   //Statements will execute if the Boolean expression is true

}




එහෙනම් ඉතින් කස්ටියත් එක්ක මේ example එක කරලම බලන්නකෝ.



public class Test {

    public static void main(String args[]) {
        
        int x = 20;

        if (x < 30) {
            
            System.out.print("This is if statement");
            
        }
        
    }
    
}



මේකේ result එක විදියට එන්න ඕනේ ඔයාලට This is if statement කියල. එක එන්නේ අපි ලියල තියන Boolean expression  එක true නිසා if එක ඇතුලේ තියන statement එක run වෙනවා.



අපි එහෙනම් දැන් බලමු The if...else Statement  එක ගැන,

මේකෙදි වෙන්නෙත් if එකේ උන දේම තමයි අලුත් දෙකට තියෙන්නේ අපි ලියන Boolean expression එක false උනොත් එක මොකද වෙන්නේ කියල else statement එකේ තියන එක run වෙන එකයි.


බලන්නකෝ syntax එක,




if(Boolean_expression){

   //Executes when the Boolean expression is true

}else{

   //Executes when the Boolean expression is false

}



මේකත් කරලම බලන්නකෝ එහෙනම්,




public class Test {

    public static void main(String args[]) {
        
        int x = 50;

        if (x < 30) {
            
            System.out.print("This is if statement");
            
        } else {
            
            System.out.print("This is else statement");
            
        }
        
    }
    
}




මේකේ result එක විදියට එන්න ඕනේ ඔයාලට This is else statement කියල. එක එන්නේ අපි ලියල තියන Boolean expression  එක false නිසා else එක ඇතුලේ තියන statement එක run වෙනවා.

ඊටපස්සේ අපිට තියෙන්නේ The if...else if...else Statement  එක,

  මේකෙදි වෙන්නේ multiple if statements මේකෙදි අපිට conditions ගොඩක් check කරන්න පුළුවන්. මේක කරනකොට ඔයාල දැනගන්න ඕනේ කරුණු ටිකකුත් තියනවා මේ ටිකත් ඔලුවට දාගන්නකෝ 


  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.


බලන්නකෝ syntax එක



if(Boolean_expression 1){

   //Executes when the Boolean expression 1 is true

}else if(Boolean_expression 2){

   //Executes when the Boolean expression 2 is true

}else if(Boolean_expression 3){

   //Executes when the Boolean expression 3 is true

}else {

   //Executes when the none of the above condition is true.

}



මේක කරලම බලන්නකෝ එහෙනම් තවත් හොදට තේරෙයි එතකොට ඔයාලට




public class Test {

    public static void main(String args[]) {
        
        int x = 30;

        if (x == 10) {
            
            System.out.print("Value of X is 10");
            
        } else if (x == 20) {
            
            System.out.print("Value of X is 20");
            
        } else if (x == 30) {
            
            System.out.print("Value of X is 30");
            
        } else {
            
            System.out.print("This is else statement");
            
        }
        
    }
    
}




මේකේ result එක විදියට එන්න ඕනේ ඔයාලට Value of X is 30 කියල. එක එන්නේ අපි ලියල තියන Boolean expression  එක true වෙන්නේ 3 වෙනි if  statement එකේ නිසා එකේ තියන ස්ට්ස්ටෙමෙන්ට් එක run වෙනවා.


ඊටපස්සේ අපිට කරන්න තියෙන්නේ Nested if...else Statement එක ගැන,

මේකෙදි වෙන්නේ එක if statement එකක් ඇතුලේ තවත් if statement ලියන එකයි.

බලන්නකෝ syntax එක 


if(Boolean_expression 1){

   //Executes when the Boolean expression 1 is true
   
if(Boolean_expression 2){

      //Executes when the Boolean expression 2 is true
   
    }

}





මේක කරලම බලන්නකෝ එහෙනම්





public class Test {

    public static void main(String args[]) {
        
        int x = 50;
        
        int y = 20;

        if (x == 50) {
            
            if (y == 20) {
                
                System.out.print("X = 50 and Y = 20");
                
            }
            
        }
        
    }
    
}





ඔයාලට result එක විදියට එන්න ඕනේ X = 50 and Y = 20 කියල.




මම තව වැදගත් වෙන දෙයක් ඔයාලට කියල දෙන්නයි මේ හදන්නේ ඒ තමයි java වල අපිට හම්බවෙන switch statemets ගැන.


මේකෙදි කරන්නේත් if එකෙන් කරන දේම තමයි ඔයාලට ලේසියෙන් කරගන්න පුළුවන් අපි if else if කියා කියා ගොඩාක් දෙනවා වෙනුවට case කියල දෙන එක විතරයි කරන්න තියෙන්නේ බලාගන්නකෝ syntax එක



switch(expression){

    case value :

       //Statements

       break; //optional

    case value :

       //Statements

       break; //optional

    //You can have any number of case statements.

    default : //Optional

       //Statements

}



ඒ වගේම ඔයාල switch එක use කරනකොට දැනගන්න ඕනේ වැදගත්ම කාරුණු ටිකක් තියනවා මේ ටික හොදට ඔලුවට දාගන්න.

 

  •     The variable used in a switch statement can only be a byte, short, int, or char. 
  •     You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. 
  •     The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. 
  •     When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. 
  •     When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 
  •     Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. 
  •     A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


එහෙනම් මේක කරලම බලන්නකෝ,



public class Test {

    public static void main(String args[]) {

        char grade = 'C';

        switch (grade) {
        
        case 'A':
            
            System.out.println("Distinction Pass!");
            
            break;
            
        case 'B':
            
            System.out.println("Very Good Pass");
            
        case 'C':
            
            System.out.println("Credit Pass");
            
            break;
            
        case 'S':
            
            System.out.println("Ordinary Pass");
            
        case 'F':
            
            System.out.println("Fail, Better try again");
            
            break;
            
        default:
            
            System.out.println("Invalid grade");
            
        }
        
        System.out.println("Your grade is " + grade);
        
    }
    
}







result එක එන්නේ,
Credit Pass
Your grade is C



එහෙනම් ඉතින් අදට යන්න වෙලාව හරි අද අපි මේ කරපු දේවල් ඉස්සරහට ගොඩාක් වැදගත් වෙන්වා. මේවා හොදට බලාගන්න 




එහෙනම් ඉතින් මම ගියා.... ටටා :D හික් හික් 


මතක ඇතුව කොමෙන්ටුවකුත් දාලම යන්න යාලුවනේ :) 











Thursday, May 14, 2015

Lesson 07


ඉතින් යාලුවනේ කොහොමද ඔන්න අද මම කියල දෙන්න යන්නේ අපිට java වල තවත් ගොඩාක් වැදගත් දේවල් ටිකක්  ඒ තමයි java වල Loops.

අපිට java වල loops ගත්තහම වර්ග කීපයල් තියනවා අපි එහෙනම් බලමු ඒ මොනවද කියලා.


  •     while Loop 
  •     do...while Loop 
  •     for Loop
  •     foreach loop (enhanced for loop)
  
 එහෙනම් ඉතින් වැඩ පටාන් ගමු කස්ටියත් එක්ක.
අපි මුලින් ම බලමු එහෙනම් while Loop  එක ගැන,

මේකෙදි වෙන්නේ අපිට ඕනේ task එකක් කිසියම් වාරගානක් repeat කරන එකයි.

බලන්නකෝ syntax එක එහෙනම් ,



while(Boolean_expression)

{  
 
   //Statements   

}






එහෙනම් ඉතින් කස්ටියත් එක්ක මේක කරලා බලන්නකෝ



public class Test {

    public static void main(String args[]) {
        
        int x = 20;

        while (x <= 30) {
            
            System.out.println("Value of x is : " + x);
            
            x++;
            
        }
        
    }
    
}





ඔයාලට result එක විදියට එන්න ඕනේ මෙන්න මෙහෙම,

Value of x is : 20
Value of x is : 21
Value of x is : 22
Value of x is : 23
Value of x is : 24
Value of x is : 25
Value of x is : 26
Value of x is : 27
Value of x is : 28
Value of x is : 29
Value of x is : 30


මේකෙදි වෙන්නේ අපි while condition එකෙන් චෙච්ක් කරනවා x විශාලයි හෝ සමානයි ද 30 ට කියල 

ඊටපස්සේ කරන්නේ අපි එක print කරන එකයි. ඊටපස්සේ x එකකින් increment කරනවා ඊටපස්සේ x කියන එකක් 30 ට සමාන වෙනකම් loop එක run වෙනවා.


අපි එහෙනම් ඊලගට බලමු  do...while Loop  එක ගැන,

මේකෙදි කරන්නේ while loop එකේ උන වැඩේම තමයි ඒ උනත් මේකේ තියන විශේෂත්වය තමයි do කියන code block එකේ තියන එක ඉස්සල්ලම එක පාරක් run වෙනවා.

බලන්නකෝ එහෙනම් syntax  එක 



do

{
   //Statements

} while(Boolean_expression);



එහෙනම් ඉතින් කස්ටිය මේක කරලා බලන්නකෝ,




public class Test {

    public static void main(String args[]) {
        
        int x = 20;

        do {
            
            System.out.println("Value of x is : " + x);
            
            x++;
            
        } while (x <= 30);
        
    }
    
}




මේකේ result එක එන්නේ ඔයාලට,


Value of x is : 20
Value of x is : 21
Value of x is : 22
Value of x is : 23
Value of x is : 24
Value of x is : 25
Value of x is : 26
Value of x is : 27
Value of x is : 28
Value of x is : 29
Value of x is : 30


එහෙනම් ඉතින් ඊටපස්සේ අපි බලමු for Loop එක ගැන,

බලන්නකෝ එහෙනම් syntax එක එහෙම




for(initialization; Boolean_expression; update)

{

   //Statements

}




එහෙනම් ඉතින් මේක කරලාම බලන්නකෝ,




public class Test {

    public static void main(String args[]) {

        for (int x = 20; x <= 30; x++) {

            System.out.println("Value of x is : " + x);

        }

    }

}



Result එක එන්නේ ,




 
Value of x is : 20
Value of x is : 21
Value of x is : 22
Value of x is : 23
Value of x is : 24
Value of x is : 25
Value of x is : 26
Value of x is : 27
Value of x is : 28
Value of x is : 29
Value of x is : 30

එහෙනම් ඉතින් අපි දැන් බලමු foreach loop (enhanced for loop)  එක ගැන මේක java වලට අලුතෙන් හදුන්වාදීපු  එකක් මේක අවේ java 1.5 සංස්කරණය යටතේ තමයි. මේක අපි ගොඩක් ම use කරන්නේ Arrays වලට තමයි ඔයාලට මේක අපි Arrays කරනකොටත් හම්බවෙනවා එක නිසා බයවෙන්න දෙයක් නෑ. 

බලන්නකෝ එහෙනම් syntax එක 



for(declaration : expression)

{

   //Statements

}



එහෙනම් ඉතින් මේක කරලා බලන්නකෝ,




public class EnhancedForLoop {
    
    public static void main(String[] args) {
        
    int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
 
    for (int t: primes) {
        
      System.out.println(t); 
      
    }
    
  }
    
}




result එක එන්නේ,

2
3
5
7
11
13
17
19
23
29
මේකේ තේරුනේ නැත්තම් එච්චර ගනන්ගන්නෙපා අපි ඉස්සරහට Arrays කරනකොට හොදටම තේරෙයි.



ඊටපස්සේ ඔයාලට කියල දෙන්න තවත් පොඩි දෙයක් තියනවා ඒ තමයි අපි loop එකක් use කරනකොට දැනගන්න ඕනේ keywords දෙකක් තියනවා ඒ තමයි break කියන එකකයි continue  කියන එකයි.

අපි මුලින් ම බලමු break කියන keyword එක ගැන එහෙනම්

අපි මේක use කරන්නේ loop එක නවත්තන්න මේ keyword එක use කරහම මුළු loop එකම නවතිනවා.





public class Test {

    public static void main(String args[]) {
        
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            
            if (x == 40) {
                
                break;
                
            }
            
            System.out.println(x);
            
        }
        
    }
    
}



Result එක එන්නේ 

10
20
30
x == 40 වෙනකොට loop එක break වෙනවා එක තමයි වෙන්නේ
ඊටපස්සේ තියෙන්නේ continue keyword එක අපි බලමු,


මේකෙදි කරන්නේ flow එකේ condition එක true උනහම එක skip කරලා loop එක continue කරන එකයි.




public class Test {

    public static void main(String args[]) {
        
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            
            if (x == 40) {
                
                continue;
            }
            
            System.out.println(x);
            
        }
        
    }
    
}





Result එක එන්නේ 
10
20
30
50
Condition එක true උනහම එක skip කරලා loop එක continue කරනවා



එහෙනම් ඉතින් අදට මට යන්න වෙලාව හරි එහෙනම් තවත් අලුත් පාඩමක් අරගෙන එනකම් මේ ටික හොදට බලාගන්න මොකද මේ කරන ටික අපි ගොඩාක් ඉස්සරහට use කරනවා. 



එහෙනම් ඉතින් මම  යනවෝ...... හික්  හික් ;) 


මතක ඇතුව කොමෙන්ටුවකුත් දාලම යන්න යාලුවනේ :)









Thursday, May 7, 2015

Lesson 06


ඉතින් කොහොමද යාලුවනේ ඔන්න අදත් මම ඔයාලට තවත් අලුත් පොස්ටුවක් අරගෙන අවා අද අපිට කතා කරන්න තියෙන්නේ අපි එදා අන්තිමට කතා කරලා ඉවර කරපු ඔපෙරටෝර්ස් වලම කොටසක් තමයි.

 Assignment Operators සහ Misc Operators ගැන තමි මම අද කියල දෙන්න යන්නේ.එහෙනම් අපි මුලින් ම බලමු මොනවද මේ assignment Operators කියල අපිට මේ යටතේ ඔපෙරේටර්ස්  කිහිපයක් ම හම්බෙනවා අපි බලමු එහෙනම් ඒ මොනවද කියල.



Operator
Description
Example

=
 Simple assignment operator, Assigns values from right side operands to left side operand

C = X + Y will assign
 value of X + Y into C


+=
 Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

C += A is equivalent to C = C + A


-=
 Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

C -= A is equivalent to C = C - A


*=
 Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

C *= A is equivalent to C = C * A


/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

C /= A is equivalent to C = C / A


%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

C %= A is equivalent to
 C = C % A

<<=
Left shift AND assignment operator

C <<= 2 is same as C = C << 2

>>=
Right shift AND assignment operator

C >>= 2 is same as C = C >> 2

&=
Bitwise AND assignment operator

C &= 2 is same as C = C & 2

^=
bitwise exclusive OR and assignment operator

C ^= 2 is same as C = C ^ 2

|=
bitwise inclusive OR and assignment operator

C |= 2 is same as C = C | 2


















































 මේ ටික ඉතාමත් සරල operators ටිඅකක් හින්දා මම වැඩිය විස්තර කරන්න යන්නේ නෑ මේක කියෙව්වහමත් ඔයාලට තේරෙයි කොහොමද කරන්නේ කියල එක නිසා මම මේ ඔක්කොටම example එකක් කරන්නම්.



public class AssignmentOpr {

    public static void main(String args[]) {

        int a = 50;
        
        int b = 100;
        
        int c = 0;

        c = a + b;
        
        System.out.println("c = a + b = " + c);

        c += a;
        
        System.out.println("c += a  = " + c);

        c -= a;
        
        System.out.println("c -= a = " + c);

        c *= a;
        
        System.out.println("c *= a = " + c);

        a = 100;
        
        c = 50;
        
        c /= a;
        
        System.out.println("c /= a = " + c);

        a = 20;
        
        c = 15;
        
        c %= a;
        
        System.out.println("c %= a  = " + c);

        c <<= 2;
        
        System.out.println("c <<= 2 = " + c);

        c >>= 2;
        
        System.out.println("c >>= 2 = " + c);

        c >>= 2;
        
        System.out.println("c >>= a = " + c);

        c &= a;
        
        System.out.println("c &= 2  = " + c);

        c ^= a;
        
        System.out.println("c ^= a   = " + c);

        c |= a;
        
        System.out.println("c |= a   = " + c);
        
    }

}




 ඔයාලට result එක එන්න ඕනේ මෙන්න මේ විදියට තමයි

c = a + b = 150

c += a  = 200


c -= a = 150


c *= a = 7500


c /= a = 0


c %= a  = 15


c <<= 2 = 60


c >>= 2 = 15


c >>= a = 3


c &= 2  = 0


c ^= a   = 20


c |= a   = 20


 ඊටපස්සේ එහෙනම් අපි බලමු මොනවද මේ Misc Operators කියන්නේ කියල

මේ යටතට අපිට operators දෙකක් ගන්න පුළුවන් ඒ තමයි

  • Conditional Operator ( ? : )

  • instanceof Operator
 මුලින්ම බලමු එහෙනම් Conditional Operator එක ගැන,

 මේකට අපි ternary operator කියලත් කියනවා මේකෙදි  boolean expression එකක් evaluate කරන්න operands 3ක් use කරනවා.

බලන්නකෝ syntax එක,

variable x = (expression) ? value if true : value if false

 එහෙනම් අපි මේකට example එකක් කරලා බලමු 




public class ConditionalOpr {

   public static void main(String args[]){
       
      int x , y;
      
      x = 10;
      
      y = (x == 1) ? 50: 100;
      
      System.out.println( "Value of y is : " +  y );

      y = (x == 10) ? 50: 100;
      
      System.out.println( "Value of y is : " + y );
      
   }
   
}






  • Value of y is : 100
  • Value of y is : 50


 මේකෙදි වෙන්නේ පලවෙනි statement එකේදී x ගේ value එක එකට සමානද කියල බලනවා  එක false නිසා අපිට එන්නේ 100 කියන value එකයි ඊලග  statement එකේදී අපි බලන්නේ value එක 10 ට සමානද කියලයි එක true වෙන නිසා අපිට 50 කියන උත්තරය ලැබෙනවා


ඊලගට අපි බලමු එහෙනම් instanceof Operator එක ගැන,
මේ operator එක අපි use කරන්නේ object reference variables වලට විතරයි. මේ operator එකෙන් චෙක් කරනවා object එක class type or interface type ද කියල,

Syntax එක බලාගන්නකෝ එහෙනම්,

( Object reference variable ) instanceof  (class/interface type)



public class InstanceofOpr {

   public static void main(String args[]){
       
      String name = "Amal";
      
      // following will return true since name is type of String
      
      boolean result = name instanceof String;  
      
      System.out.println( result );
      
   }
   
}




මේකේ result එක විදියට එන්නේ 

True කියල

එක වෙන්නේ instanceof operator එකෙන් චෙච්ක් කරනවා name කියන variable එකේ තියෙන්නේ string type එකේ value එකක් ද කියල.


මෙතනින් අපි කරපු operators පාඩම ඉවර වෙනවා එහෙනම් ඉතින් මේ ටික කස්ටිය එක්ක හොදට බලාගන්න අපිට ඉස්සරහට මේ කරපු ඒවා ගොඩක් වැදගත් වෙනවා.

එහෙනම් ඉතින් අදට යන්න වෙලාව හරි අයෙත් තවත් අලුත් පොස්ටුවකුත් අරගෙන එන්නම් එතකම් හැමෝටම ජයවේවා!!!


 මතක ඇතුව කොමෙන්ටුවකුත් දාල යන්න යාලුවනේ.....