Wednesday, December 29, 2010

ජාවා වැඩසටහනක් සහ PHP වැඩසටහනක් අතර දත්ත හුවමාරු කරන අන්දම

එක දිගටම ජාවා සම්බන්ද මූලධර්ම සාකච්ඡා කරපු නිසා අපි මේ පාඩමෙන් ප්‍රායෝගික පැත්තට බර කරුණු ටිකක් අධ්‍යයනය කරමු. මේ වන විට පරිගණක වැඩසටහනක් යනු හුදෙක්ම එක් පරිගණකයකට සීමාවූ දෙයක් නොවන බව අපි කවුරුත් දන්න දෙයක්නේ. බොහොමයක් පරිගණක වැඩසටහන් අන්තර්ජාලයට සම්බන්දව පවතිනවා. උදාහරණ විදියට internet messenger වැඩසටහන් , virus guards, වැනි වැඩසටහන් දක්වන්න පුලුවන්. වයිරස් ගාඩ් එකක් සැලකුවොත් එය අන්තර්ජාලය හා සම්බන්ද වන අවස්ථා තමයි අලුත් virus definition තිබේ දැයි පරීක්‍ෂා කිරීම, අලුත් virus definition තිබේනම් ඒවා ලබා ගැනීම, සැක සහිත ගොණුවක් අදාල ආයතනයට upload කිරීම වැනි අවස්ථා. මෙහිදී මූලිකවම සිදුවන්නේ දුරස්ථ සර්වරයක ඇති වැඩසටහනක් හා කිසියම් සන්නිවේදනයක් කිරීමයි. 

අපි මෙම පාඩමෙන් අධ්‍යයනය කිරීමට යන්නේ ජාවා වැඩසටහනකින් දුරස්ථ වැඩසටහනක් සමග සම්බන්ද වන ආකාරයයි. මාතෘකාවට අනුව අපි දුරස්ථ වැඩසටහන ලෙස යොදාගන්නේ PHP script එකක්. නමුත් මෙම දුරස්ථ වැඩසටහන Python හෝ Perl script එකක් හෝ වෙනත් ඕනෑම server side script එකක් වීමට පුලුවන්. 

Saturday, December 18, 2010

ජාවා Data Streams භාවිතා කරන අයුරු

මේ වනවිට අපි ඉතා වැදගත් stream වර්ග කිහිපයක් ගැන කතාකලා. byte stream, character stream, buffered stream එම stream පිලිබඳ ඔබට මේ වනවිට හොඳ අවබෝධයක් ඇති බව උපකල්පනය කරමින් අපි මෙම පාඩමෙන් සාකච්ඡා කිරීමට යන්නේ data streams පිලිබඳවයි. 

Data stream බාවිතයෙන් stream එකකට  primitive data ලිවීමට සහ data stream එකකින් primitive data කියවීමට පුලුවන්. එනම් int, float, double, long, boolean, char, byte මෙන්ම primitive data type එකක් නොවුවද String සඳහා data streams සහය දක්වනවා.

DataInputStream සහ DataOutputStream නම් class වර්ග 2 යොදාගෙන data streams නිරූපණය කෙරෙනවා. එවන් stream එකකින් දත්ත කියවීමට DataInputStream ද ඒවාට ලිවීමට DataOutputStream class එකද යොදාගැනෙනවා.
හොඳින් මතක තබාගත යුතු කාරණාවක් තමයි සෑම විටම data stream object එකක් construct කල යුත්තේ byte stream එකක් wrap කිරීමෙන් බව.

DataInputStream භාවිතාකරන අන්දම...
මෙහිදී readInt(), readLong(), readUTF()... යනාදී වශයෙන්  primitive data කියවීමට වැදගත් methods ගණනාවක් යොදාගන්න පුලුවන්. Java Documentation එක බැලිමෙන් මෙම methods සවිස්තරාත්මකව දැනගැනීමට පුලුවන්. 

කලින් අධ්‍යයනය කල stream වලදී මෙන් නොව data stream වලදී File එකේ අවසානය (EOF) හඳුනාගැනීමට සෘජු ක්‍රමයක් නැහැ. එම නිසා EOFException එක catch කිරීමෙන් file එකක අවසානය හඳුනාගතයුතු වනවා. 

DataOutputStream භාවිතාකරන අන්දම...
මෙහිදී writeInt(), writeLong(), writeUTF()... යනාදී වශ්යෙන් primitive data ලිවීමට වැදගත් වන methods ගනණාවක් තිබෙනවා. මීට පෙර stream වලට පොදුවූ flush(), close() යනාදී methods ද මෙම data stream විසින් inherit කරගෙන සිටිනවා.

අපි දැන් බලමු data stream එකක ක්‍රියාකාරීත්වය වටහාගැනීමට පහසුවන අන්දමේ සරල ජාවා වැඩසටහනක්.

/**       
*class  : Test 
*Author : Kanishka Dilshan       
*Purpose: Demonstrate how to use data streams  
*Blog   : http://javaxclass.blogspot.com       
*/ 

import java.io.*;

class Test {
 public static void main(String args[]){
  try{
   DataOutputStream dout=new DataOutputStream(
     new BufferedOutputStream(
     new FileOutputStream("myDataFile.dat")));
   
   dout.writeUTF("Kanishka");
   dout.writeFloat(83.23f);
   dout.writeBoolean(true);   
   
   dout.writeUTF("Ravindu");
   dout.writeFloat(98.12f);
   dout.writeBoolean(false); 
   
   dout.writeUTF("Madhawa");
   dout.writeFloat(84.65f);
   dout.writeBoolean(false);
   
   dout.close();
   
   DataInputStream din=new DataInputStream(
     new BufferedInputStream(
     new FileInputStream("myDatafile.dat")));
   try{
    while(true){
     String name=din.readUTF();
     float marks=din.readFloat();
     boolean val=din.readBoolean();
     System.out.println(name+"   "+marks+"   "+val);
    }
   }catch(EOFException e){
    System.out.println("End of the data file");
   }finally{
    try{
    
    }catch(Exception e){
     din.close();
    }
   }
  }catch(Exception e){
   System.err.println(e.getMessage());
  }
 }
}
Output :

ඉහත code එක මගින් සිදුවන ක්‍රියාවලිය වටහා ගැනීමට උත්සාහ කරමු.
Line  13,14,15 මගින් සුදුසු byte stream එකක් output data stream එකක් මගින් wrap කිරීම සිදුකර ඇත.

Line 17,18,19 මෙහිදී කිසියම් පිලිවෙලකට අනුව data stream එකට දත්ත ලිවීම සිදුකර ඇත. එනම් මුලින් String data ද දෙවනුව float data ද අවසන boolean data ද stream එකට එනම් myDataFile.dat file එකට ලියා ඇත.

Line 31,31,33 මෙහිදී byte stream එකක් input data stream එකක් මගින් wrap කිරීම සිදුකර ඇත. මෙහිදී සාදාගන්නා din object එක හරහා data කියවීම සිදුකෙරේ.

Line 34 - Line 48 මෙහිදී අදාල data file එක අවසන් වනතෙක් කියවීම සිදුකර එම දත්ත console එකෙහි දර්ශනය කරයි.  line 35 න් ආරම්භ වන while loop එක infinite loop එකක් ලෙස පෙනුනද file එකේ අවසානයේ EOFException එකක් throw කරන නිසා file එක අවසානය වනතෙක් පමණක් loop එක ක්‍රියාත්මක වේ.

Streaming වල තවත් වැදගත් කොටසක් වන Standard streams පිලිබඳ අපි මීලඟ පාඩමෙන් සාකච්ඡා කරමු.

Friday, December 17, 2010

Buffered Stream යොදාගෙන File Copy කරන අන්දම

අපි මීට පෙර පාඩමෙන් Buffered stream පිලිබඳව අධ්‍යයනය කලා. අපි මෙම පාඩමෙන් එම සිද්ධාන්ත යොදාගනිමින් File copy කිරීම සඳහා වැඩසටහනක් ලිවීමට උත්සාහ කරමු. මේ සඳහා අපි එම buffered stream classes තුල ඇති read() ,write() , flush(), close() යන methods භාවිතයට ගනිමු. පහත දැක්වෙන්නේ ඒ සඳහා වන කේත කොටසයි.
FileOperations.java
/**       
*class  : FileOperations 
*Author : Kanishka Dilshan       
*Purpose: Using buffered Streams to copy a file  
*Blog   : http://javaxclass.blogspot.com       
*/ 

import java.io.*;

class FileOperations {
 public static final int BUFF_SIZE=8*1024*1024;//8 MB
 
 public boolean fileCopy(String source,String dest){
  boolean isCopied=false;
  BufferedInputStream buffIn=null;
  BufferedOutputStream buffOut=null;
  byte buffer[]=new byte[BUFF_SIZE];
  try{
   buffIn=new BufferedInputStream(new FileInputStream(source),8*1024*1024);
   buffOut=new BufferedOutputStream(new FileOutputStream(dest),8*1024*1024);
   
   int len;
   while((len=buffIn.read(buffer))>0){
    buffOut.write(buffer,0,len);
    buffOut.flush();
   }
   isCopied=true; //file copied successfully
  }catch(FileNotFoundException e){
   System.err.println("Cannot find the file : " + e.getMessage());
  }catch(IOException e){
   System.err.println("IO Error : " + e.getMessage());
  }finally{
   if(buffIn!=null){
    try{
     buffIn.close();
    }catch(IOException e){}
   }   
   if(buffOut!=null){
    try{
     buffOut.close();
    }catch(IOException e){}
   }
  }
  return isCopied;
 }
}  
FileCopy.java
class FileCopy{
 public static void main(String args[]){
  if(args.length!=2){
   System.out.println("Incorrect syntax!");
   System.out.println("Use Following Syntax");
   System.out.println("java FileCopy #source_file# #estination_file#");
   System.exit(0);
  }
  
  FileOperations fo=new FileOperations();
  boolean status=fo.fileCopy(args[0],args[1]);
  if(status==true){
   System.out.println("File was copied successfully!");
  }else{
   System.out.println("File was not copied");
  }
  
 }
}

අපි දැන් ඉහත වැඩසටහනේ ක්‍රියාකාරීත්වය අධ්‍යයනය කරමු.
Line 11 : මෙහිදී BUFF_SIZE ලෙස 8MB constant අගයක් declare කර ගැනීම සිදුකෙරෙනවා. පසුව  buffer එකේ ඇතිදෑ කියවීමට සහ buffer එකට ලිවීමට මෙම constant අගයට අනුව සකසන byte array එකක් යොදාගනු ලබනවා.

Line 12 : boolean isCopied=false; මෙය යොදාගන්නේ file එක සාර්ථකව කොපි වූවාද නැද්ද යන්න return කිරීම සඳහායි.

Line 17 : byte buffer[]=new byte[BUFF_SIZE]; මෙහිදී 8MB ප්‍රමාණයකින් යුතු "buffer" නම් byte array එකක් සකසාගනු ලැබේ.

Line 19, 20 : buffIn සහ buffOut යනුවෙන් buffered stream object 2ක් සකසාගනු ලැබේ(class type සලකා බලන්න) මින් buffIn object එක යොදාගනුයේ source file එක කියවීමටය. එමෙන්ම buffOut යොදාගනුයේ destination file එකට ලිවීමටය. මෙම object සැදිමේදී ඒවාට අදාල buffer size එකද විශේෂයෙන් සඳහන් කර ඇත.

Line 23 : මෙම while loop එක තුලදී මූලිකවම කාර්‍යයන් 2 ක් සිදුකෙරේ len=buffIn.read(buffer) මගින් සිදුකරන්නේ input stream එකෙන් කියවන ලද අගයන් buffer නම් byte array එකේ store කර len නම් int විචල්‍යය තුල කියවන ලද ඩේටා ප්‍රමාණය assign කිරීමයි. 

එහිදී සිදුකරන අනෙකුත් කාර්‍යය නම් එම කියවන ලද ඩේටා ප්‍රමාණය 0 ට වඩා වැඩිද යන්න පරීක්ෂා කර බැලීමයි. කියවන ලද ඩේටා ප්‍රමාණය -1 ලෙස ලැබේ නම් ඉන් ගම්‍ය වනුයේ stream එකෙහි අවසානයට ලඟාවී ඇති බවයි නැතහොත් end of file(EOF) බවයි මෙවිට output stream එකට ලිවීමට අවශ්‍ය නැත. කියවන ලද ඩේටා ප්‍රමාණය 0 නම්ද output stream එකට ලිවීමට අවශ්‍ය නැත.  එම නිසා (len=buffIn.read(buffer))>0 නම් condition එක true නම් පමණක් while loop එක ක්‍රියාත්මකවේ.

Line 24 : buffOut.write(buffer,0,len);   මෙහිදී සිදුකරනුයේ කියවන ලද ඩේටා (byte array එක) output stream එකට ලිවීමයි. 0 සහ len යන parameters වලින් කියවෙනුයේ එම byte array එකේ 0 සිට len යන අගය දක්වා byte ප්‍රමාණය output stream එකට ලිවිය යුතු බවයි.

Line 25 : buffOut.flush(); මෙහිදී output stream එක clear කිරීම සිදුකරයි. 

Line 27 :  while loop එක අවසන් වී මෙම line එකට පැමිණියා යනු file එක සාර්ථකව ලියා අවසන් බවට සහතික විය හැකිය මන්දයත් IO Operations සිදුකර ඇත්තේ exception handle කල හැකි පරිදි try-catch block තුලය. එම නිසා මෙහිදී isCopied නම් විචල්‍යයේ අගය true ලෙස සටහන් කරනු ලැබේ.

Line 32: catch block එක තුලදී සිදුකර ඇති දෑ පහසුවෙන් තේරුම් ගත හැකි නිසා finally block තුලදී කුමක් සිදුවන්නේද යන්න සලකා බලමු. මෙහිදී ඉහත open කරගන්නා ලද input සහ output stream 2ම close කිරීම සිදුකරයි මෙය සිදුකරන්නේ තවත් try-catch block එකක් තුලදීය. 

වැඩසටහන මගින් source file එකේ binary pattern එක ඒ ආකාරයෙන්ම destination file එකේ තිබේ දැයි බැලීමට hash calculator එකක් යොදාගත හැක.

මෙම ක්‍රියාදාමය වටහාගැනීම සඳහා ඉහත පැහැදිලි කිරීම ප්‍රමාණවත් යයි සිතමි. කෙසේ වෙතත් තවදුරටත් පැහැදිලි කල යුතු යමක් ඇත්නම් ඉදිරිපත් කරන මෙන් ඉල්ලා සිටින්න කැමතියි.
ඉහත දක්වා ඇති source code එක සහ byte code files ලබා ගැනීමට පහත දක්වා ඇති link එක භාවිතාකරන්න. 

Thursday, December 16, 2010

ජාවා වැඩසටහන් තුල Buffered Stream යොදාගන්නා අයුරු

මීට පෙර පාඩම් වලදී අපි Stream පිලිබඳ කතාකලානෙ අපි මෙම පාඩමෙන් තවත් වැදගත් Stream වර්ගයක් වන Buffered Stream පිලිබඳ අධ්‍යයනය කරමු. 

අපි මීට පෙර සලකා බලන ලද FileInputStream , FileReader යන Stream වර්ග සැලකූ විට කෙලින්ම ඒවා යොදාගෙන කිසියම් resource එකක් භාවිතා කිරීමේදී ඒවායෙන් ලබාදෙන සෑම  read, write request එකක් සඳහාම අදාල resource එක access කිරීම සිදුවනවා. උදාහරණයක් ලෙසට අපි හිතමු  අපිට access කිරීමට අවශ්‍ය resource එක disk file එකක් කියල. එහිදී සෑම read, write request එකකදීම hard disk එකේ අදාල file එක access කිරීම සිදුවනවා. මෙහිදී JVM (Java Virtual Machine) එක විසින් native calls(operating system එකට අදාල low level methods) භාවිතා කිරීම සිදුකෙරෙනවා. එනම් සෑම read, write request එකකටම native calls invoke කිරීමට සිදුවනවා.  තවත් උදාහරණයක් ලෙස අපි සලකමු මෙම resource එක client Socket එකක් කියල. එවිට ඉහත ආකාරයේ buffered නොවන Stream එකක් යොදාගත්තොත් සෑම read, write request එකක් සඳහාම network resources භාවිතාවීම සිදුවනවා. කාර්‍යක්ෂමතාව අතින් සැලකීමේදී මෙය හොඳ විසඳුමක් වන්නේ නැහැ.

Wednesday, December 8, 2010

ජාවා Byte Streams භාවිතා කරන අයුරු

කලින් පාඩමෙන් අපි කතාකලා ජාවා stream පිළිබඳව.  දැන් අපි බලමු byte stream භාවිතා කරන්නේ කුමනාකාරයෙන්ද කියල. Byte stream එකක් input / output operations සිදු කරනුයේ 8-bit අකාරයටයි.  එනම් character streams වලදී මෙන් නොව මෙහිදී characters වෙනුවට raw bytes කියවීම සහ ලිවීම සිදුකෙරේ. එම නිසා data file copy කිරීම යනාදී වැඩ වලට යොදාගත යුත්තේ byte stream වේ. නමුත් භාවිතාවන data, character වලින් යුක්ත නම් character streams වලට යෑම වඩාත් ඵලදායීවේ.

අහත දැක්වෙන ආකාරයට ජාවා වැඩසටහනක් තුලදී byte streams යොදාගත හැකිය. මෙහිදී I/O Stream නිරූපණය කිරීම සඳහාjava.io පැකේජයේ ඇති FileInputStream සහ FileOutputStream class යොදාගැනේ.


/**      
*class  : ByteOperations
*Author : Kanishka Dilshan      
*Purpose: Showing how to use byte streams
*Blog   : http://javaxclass.blogspot.com      
*/ 


import java.io.*;

public class ByteOperations{
 public static void main(String args[]){
  FileInputStream fileInpStrm=null;
  FileOutputStream fileOutStrm=null;
  try{
   fileInpStrm=new FileInputStream("myDataFile.dat");
   fileOutStrm=new FileOutputStream("newDataFile.dat");
   int tmp;
   while((tmp=fileInpStrm.read())!=-1){
    fileOutStrm.write(tmp);
   }

  }catch(FileNotFoundException e){
   System.err.println("File NOt Found : " + e.getMessage());
  }catch(IOException e){
   System.err.println("IO Exception : " + e.getMessage());
  }finally {
   try{
    if(fileInpStrm!=null){
     fileInpStrm.close();
    }
      }catch(IOException e){}
   try{   
    if(fileOutStrm!=null){
     fileOutStrm.close();
    }
   }catch(IOException e){}
  }
 }
}

මෙහිදී සිදුවනුයේ myDataFile.dat file එකේ clone එකක් newDataFile.dat ලෙසින් සෑදීමයි. පෙර පරිදිම මෙහිදීද open කරන ලද ස්ට්‍රීම් close කර තිබේ.  

FileInputStream යනු InputStream යන abstract class එකෙහි child class එකක් වේ.  ඒම class අතර සම්බන්දය පහත පරිදි වේ.

තවත් ස්ට්‍රීම් වර්ග හා ඒවා යොදාගන්නා ආකරයන් පිලිබඳව අපි ඉදිරි පාඩම් වලදී සාකච්ඡා කරමු.

Tuesday, December 7, 2010

ජාවා තුල යෙදෙන I/O Streams යනු මොනවාද | Java I/O Streams

සියලුම පරිගණක භාෂාවන් හි Input සහ Output ක්‍රියාවලීන් සිදුකර ගැනීමට කිසියම් යාන්ත්‍රණයක් පවතිනවා. උදාහරණයක් වශයෙන් කිසියම් calculation එකක් සඳහා ලියන ලද පරිගණක  වැඩසටහනකට දත්ත input කිරීමට සහ එහි ප්‍රතිඵලය පිටතට ලබාදීම සඳහා යොදාගන්නා ක්‍රම සැලකිය හැකිය.

ජාවා වැඩසටහන් තුලදී Input සහ Output ක්‍රියාවලීන් සිදුකරගැනීමට විවිධ ක්‍රම පවතී ඒවා නම් Streams, Files, Channels යනාදී වශයෙනි. අපි මෙහිදී වැඩිදුරටත් සාකච්ඡා කරනුයේ ජාවා තුල යෙදෙන Streams පිලිබඳවයි.

Friday, October 8, 2010

Java සමග Software Design Patterns (4 කොටස | Proxy Pattern)

බොහෝ පරිගණක වැඩසටහන් සැලසුම් කිරීමේදී අපට පද්ධතියෙන් පරිභාහිර සම්පත්(resources)  අප සැලසුම් කරන වැඩසටහන තුල භාවිතයට ගැනීමට සිදුවේ. උදාහරණ වශයෙන් ජාල සම්බන්දතා(network connections), files, පරිගණකයේ සසම්භාවී මතකය තුල පවතින විශාල වස්තුවක්(a large object in memory) හෝ සම්පත් අපතේ යෑම නිසා duplicate  කල නොහැකි අනෙකුත් ඕනෑම resource එකක් සැලකිය හැකියි.

මෙවැනි අවස්ථාවලදී අප සකස්කරන සැලසුම අසාර්ථක එකක් වුවහොත් එය කෙලවර වන්නේ අධික ලෙස bandwidth එක භාවිතා කරන හෝ පරිගණක මතකය අපතේ යවන හෝ කාර්‍යක්ෂමතාව අතින් ඉතාම අඩු මට්ටමක පරිගණක වැඩසටහනකින්. ප්‍රොක්සි (proxy design pattern) යනු මෙවැනි අවස්ථාවන්හිදී සාර්ථකව apply කල හැකි design pattern එකකි.

Thursday, October 7, 2010

Java සමග Software Design Patterns (3 කොටස | Facade Pattern)

ෆැෂාඩ් | Facade යනු structural ආකාරයේ design pattern එකකි. මෙමගින් කිසියම් සංකීර්ණ උප පද්ධතියක(sub system) lower level classes invoke කිරීම මගින් යම් කාර්‍යයක් සිදුකිරගැනීම සඳහා higher level interface එකක් සපයනු ලබයි. මෙලෙය facade pattern එක යොදමින් කිසියම් පද්ධතියක් සැලසුම් කිරීම නිසා එම පද්ධතියේ coupling යන ගුණය අඩුවීම සිදුවේ එනම් එම පද්ධතියේ ස්ථායීතාවය කෙරෙහි එය සුභදායක ලෙස බලපායි. මීට අමතරව coupling ගුණය අඩුවීම යම් පද්ධතියක කාර්‍යක්ෂමතාවය වැඩිවීම පිනිසද හේතුවේ. යම් system එකකට facade pattern එක apply කල පසු එතුල පවතින සංකීර්ණ sub system එකක් කිසියම් client class කිහිපයක් මගින් access කරනුයේ facade එක හරහාවේ. එමගින් ඉතා විශාල වාසි එම පද්ධතියට අත්වේ. ඉන් එක් වාසියක් නම් client ගේ වෙනසක් නොවන පරිදි සංකීර්ණ sub system එක තුල වෙනස්කම් සිදුලහැකි වීමයි. එමගින් අප විසින් සැලසුම් කරන පද්ධතිය අවුල් ජාලයක් වීම වලකනු ලබයි.

Wednesday, October 6, 2010

Java සමග Software Design Patterns (2 කොටස | Adapter Pattern)

Adapter නම් design pattern එක structural ආකාරයේ pattern එකකි. ලියන ලද කිසියම් program code එකක් දැනට පවතින වෙනත් නොගැලපෙන(incompatible) code-base එකකට(උදා: google maps API, twitter API) සම්බන්ද කිරීමේදී මෙම design pattern එක apply කල හැකිය. මෙහිදී සිදුකරනුයේ විධිමත් ක්‍රමයකට අපේ program එකට පරිභාහිර code-base එක call කිරීමට class structure එකක් සෑදීමයි. සාමාන්‍යයෙන් අප විසින් සකසන මෙම class තුලදී process එකක් නොපවතී ඒ වෙනුවට එම class මගින් සිදුකරනුයේ අදාලා කාර්‍යය සිදුකරගැනීම පිනිස පවතින code-base එක call කිරීමයි.

 අපි දැන් බලමු adapter pattern එක සඳහා අදාල class diagram එක
ඔබට UML පිළිබඳ යම් අවබෝධයක් ඇත්නම් ඉහත class diagram එක මගින් adapter pattern එකෙහි ව්‍යුහය පිලිබඳ මනා වැටහීමක් ලැබෙනු ඇත. ඔබට UML(Unified Modeling Language) පිළිබඳ අවබෝධයක් නොමැතිනම් මෙම tutorial එක භාගත කරගෙන හොඳින් අධ්‍යයනය කරන්න. ඉන්පසු ඔබට ඉතා පහසුවෙන් මෙම UML diagram එක සහ ඉදිරි පාඩම් වලදී ඉදිරිපත් කෙරෙන UML diagrams වටහාගත හැකිවනු ඇත.

Java සමග Software Design Patterns (1 කොටස)

කිසියම් පරිගණක භාෂාවක් යොදාගෙන මෘදුකාංග නිපදවීමේදී අප විසින් කිසියම් සැලසුමක් මුලින් සකස් කල යුතු වනවා. මෙහිදී අදාල සැලැස්ම ඉතා හොඳ එකක් වීම ඉතා වැදගත්. හොඳ මෘදුකාංග සැලසුමක් නිසා කේතකරණය මෙන්ම මෘදුකාංගයේ නව යාවත් කාලීන කිරීම් පුලුල් කිරීම් ආදිය පහසු වීම සිදුවනවා.
හොඳ design එකක් සාදාගැනීම සඳහා භාවිතාකරන එක තාක්ෂණයක් තමයි software design patterns කියල කියන්නෙ. කිසියම් මෘදුකාංගයක් සැලකීමේදී එහි design pattern 1කට වැඩි සංබ්‍යාවක් තිබීමට පුලුවන්.
software design pattern සඳහා වන Wikipedia හි අර්ථ දැක්වීම

"A general reusable solution    to a commonly occurring problem    in software design"

සරළව විස්තර කරන්නේ නම්. design pattern එකක් යනු විවිධ අවස්ථාවන් සඳහා යොදාගත හැකිවන පටිදි කිසියම් ගැටලුවක් විසඳන ආකාරයයි. එම නිසා ඕනෑම software pattern එකක් සැලකූ විට ඒය design pattern එකක් නොවිය හැකියි.
උදා: algorithm(sorting,encrypting,compressing..etc) සහ data structures(queues, linked lists, stacks...etc) යන ඒවා design patterns ලෙස නොසැලකේ.


Saturday, September 11, 2010

ජාවා යොදාගෙන බහුකාර්‍ය වැඩසටහන් ලිවීම (Java Threads) II

අපි මීට පෙර පාඩමේදී සාකච්ඡා කලා Threads හා සම්බන්ද මූලිකම මූලධර්ම පිලිබඳව. අපි මෙම පාඩමෙන් බලමු Threads යොදාගෙන වැඩසටහන් ලියන ආකාරය.
Threads යොදාගෙන ජාවා වැඩසටහන් ලිවීමේදී ඉතා වැදගත් වන methods කිහිපයක් තිබෙනවා අපි මුලින්ම එම methds පිලිබඳ සලකා බලමු.
ඕනෑම Thread එකක් සැලකීමේදී එය පවත්නා තත්වයන් කිහිපයක් ඇත. පහත Thread state diagram එක මගින් එම අවස්ථා පිලිබඳ සහ thread එකක හැසිරීම පිලිබඳ අධ්‍යයනය කරමු.
 Fig 1.0 Thread State Diagram

Friday, September 10, 2010

ජාවා යොදාගෙන බහුකාර්‍ය වැඩසටහන් ලිවීම (Java Threads) I

පරිගණක වැඩසටහනක් සැලසුම් කිරීමේදී එය මගින් යම් යම් ක්‍රියාවන්(methods) එකවිට(parallel) සිදුකලයුතු අවස්ථා අපට මුණගැසේ. Multitasking සඳහා උදාහරණයක් ලෙස web browser එකක tabs කිහිපයක් එකවිට load වීම ගත හැකිය.  එවැනි අවස්ථාවන් ක්‍රියාවට නැංවීම (implement) සඳහා multitasking සංකල්පය භාවිතයට ගත හැකිය.

Multitasking වැඩසටහන්හි පහත අයුරින් ප්‍රධාන ආකාර 2ක් දැකිය හැකිය.

    * Process Based

මෙහිදී සිදුවනුයේ පරිගණකයේ වැඩසටහන් කීපයක්  සමගාමීව ක්‍රියාකිරීමෙන් අදාල ප්‍රතිඵලය ලබාදීමයි. process අතර සන්නිවේදනය(දත්ත හුවමාරුව) පහසු නොවේ. ඒ සඳහා pipes, files, sockets, shared memory ආදී විවිධ උපක්‍රම යොදාගැනීමට සිදුවේ.

Monday, August 23, 2010

Errors සහ Exceptions කලමනාකරණය කිරීම (Exception Handling)

කිසියම් වැඩසටහනක් 100% ක්ම දෝශ රහිතව ලිවීම ඉතාම අසීරු කාරණයකි. වැඩසටහනේ විශාලත්වය වැඩිවත්ම මෙහි අසීරුතාව සීඝ්‍රයෙන් වැඩිවේ. Program එක සාර්ථකව compile වීම යනු එම වැඩසටහන දෝශරහිත යයි සැලකීමට සාධකයක් නොවේ. Program එකෙහි logical errors තිබිය හැක. logical errors යනු වැඩසටහන නියමාකාර ලෙස ක්‍රියානොකිරීමට(අපේක්ෂා කල පරිදි ක්‍රියා නොකිරීමට) හේතුපාදක වන කාරණයකි.

ජාවා වැඩසටහනක ඇතිවිය හැකි errors ආකාර දෙකකි.
  • Compile-time errors
  • Run-time errors
සියලුම syntax errors හෙවත් භාශාවේ කාරක රීති වලට පටහැනි අවස්ථා Compile-time errors ලෙස සැලකේ. මෙහිදී ජාවා compiler එක .java file එක මගින් .class file එක නොසාදයි. එනම් වැඩසටහන compile නොකෙරේ.

*Compile-time errors සඳහා උදාහරණ.

ඉහත වැඩසටහනේ 3 වන line එකෙහි ඇති statement එක semicolon ";" එකකින් අවසන් කර නැත එය syntax error එකකි. එමනිසා එය compile කිරීමේදී පහත දෝශය පෙන්වයි.
* Run-time errors සඳහා උදාහරණ
මෙහිදී program එක සාර්ථකව compile වුවද එය run කිරීමට යාමේදී විවිධ ගැටලු ඇතිවේ.
  • නිඛිල සංඛ්‍යා 0න් බෙදිම
  • Array එකක සීමාවෙන් පිට element එකක් access කිරීමට යාම
  • කිසියම් array එකක් තුලට නොගැලපෙන data type එකකින් යුතු අගයක් ඇතුලු කිරීම
  • null object එකක attribute හෝ methods භාවිතා කිරීම(access)
මේ සඳහා තවත් උදාහරණ රාශියක් ඇත.
දැන් අපි Run-time erors සඳහා උදාහරණ කිරිපයක් බලමු.
*Division by zero
class Runtime1{
 public static void main(String args[]){
  int a=5;
  int b=6;
  int c=10;
  int res=c/(b-a-1);
  System.out.println(res);
 }
}
මෙම වැඩසටහන සාර්ථකව compile වුවද run වීමේදී පහත දෝශය පෙන්වයි. එයට හේතුව line 6 දී (b-a-1) කොටසෙහි අගය 0 වීමයි.
* Array index out of bounds
class Runtime2{
 public static void main(String args[]){
  int arr[]=new int[5];
  int i=arr[10];
 }
}
මෙය run වීමේදී පහත දෝශය පැන නගී


දැන් අපි Run-time errors පිලිබඳ පුලුල්ව සාකච්ඡා කරමු.
Run-time errors වර්ග 2කි
  1. Errors
  2. Exceptions
අපි මුල්ම පාඩම් වලදී ඉගෙනගත්තා ජාවා තුල සියලුම දේවල් class ලෙස සලකන බව. ඒ අනුව Errors හා Exception සඳහාද ජාවා තුල class 2ක් තිබෙනවා.

Error class
මෙම ආකාරයේ errors හඳුන්වන්නේ abnormal errors හෙවත් අස්වාභාවික errors ලෙසින්. වැඩසටහනක් ලිවීමේදී අප එම වැඩසටහන තුලින් සාමාන්‍යයෙන් මෙම ආකාරයේ errors අපේක්ෂා කරන්නේ නෑ එනම් ඒවා handle කිරීමට යන්නේ නෑ.
මේ සඳහා උදාහරණ.
  • Virtual machine එකේ දෝශ
  • ThreadDeath errors

Exception class
ජාවා වැඩසටහන් තුල Error handle කරනවා යනු මෙම exceptions නියමාකාරයෙන් handle කිරීමයි. මෙහි වැදගත්කම වනුයේ run-time එකේදී කලින් අපේක්ෂා කල errors ඇතිවුවහොත් program එක terminate වීම හෝ වැරදි ප්‍රතිඵලයක් ලබාදීමෙන් වලක්වා ගැනීමයි.

බහුල වශයෙන්  හටහන්නා Exception කිහිපයකට උදාහරණ
  •  ArithmeticException
  •  ArrayIndexOutOfBoundsException
  •  IOException
  •  FileNotFoundException
  •  NumberFormatException
  •  StringOutOfBoundsException
දැන් අපි බලමු ක්‍රමලේඛයක් තුලදී Exception handle කරන්නේ කියලා.
මේ සඳහා විධි 2ක් තිබෙනවා. 

1) Throw Out (Ignore)
    මෙහිදී යම් අපේක්ෂා කල Exception එකක් හටගතහොත් එය ඉවත දැමීම නොහොත් නොසලකා හැරීම සිදුකෙරෙනවා. ඇතැම් අවස්ථා වලදී මෙම ක්‍රමය යොදාගත්තත් මෙය සුදුසුම ක්‍රමය නොවේ.
    දැන් අපි බලමු මෙම ක්‍රමය භාවිතා කරන අයුරු.
    Syntax:
        methodName throws ExceptionName{
            ..method body
        }
    උදා:
    import java.io.*;
    
    class ExceptionEx1{
     public static void main(String args[]) throws IOException{
      InputStreamReader isr=new InputStreamReader(System.in);
      BufferedReader br=new BufferedReader(isr);
      System.out.print("Enter your name : ");
      String name=br.readLine();
     }
    }
    

    2) Catch and handle
    සලකනු ලබන code segment එකක් තුල යම් අපේක්ෂිත Exception එකක් පැනනැගුනහොත් සිදුකලයුතු ක්‍රියාමාර්ගය පිලිබඳ මෙහිදී සඳහන් කෙරේ ජාවා වැඩසටහනක Exception handle කිරීම සඳහා මෙම ක්‍රමය ඉතාම සුදුසු වේ. 
    Syntax:
            try{
                ..................
                ...code segment...
                ..................
            }catch(ExceptionName1 exObj){
                ..................
                ..handling error1..
                ..................
            }
    catch(ExceptionName2 exObj){
                ..................
                ..handling error2..
                ..................
            }
    catch(ExceptionNamen exObj){
                ..................
                ..handling errorn.
                ..................
            }finally
    {
                ..................
                ..final action....
                ..................
            }

    උදා:
    import java.io.*;
    
    class ExceptionEx2{
     public static void main(String args[]) {
      int age=0;
      InputStreamReader isr=new InputStreamReader(System.in);
      BufferedReader br=new BufferedReader(isr);
      System.out.print("Enter your age : ");
      try{
       String ageStr=br.readLine(); 
       age=Integer.parseInt(ageStr);
      }catch(IOException ex1){
       System.out.println("I/O Error! "+ ex1.getMessage());
      }catch(NumberFormatException ex2){
       System.out.println("Invalid number format! "+ ex2.getMessage());
      }
     }
    }
    
    output:
    Errors catch කිරීමේදී ඒවා අපේක්ශිත අනුපිලිවෙලට සිදුකලයුතු බව මතකතබා ගත යුතුය. මෙහිදී exceptions ඇතිවිය හැකි අනුපිළිවෙල මෙන්ම class hierarchy එකද වැදගත් වේ. class hierarchy එකේ පහලින් ඇති errors මුලින් catch කර ඉහල ඇති ඒවා පසුව catch කිරීම සිදුකලයුතුය. class hierarcy එක බලාගැනීමට java documentation එක පරිශීලනය කලයුතුවේ.

    finally  block එක පිලිබඳව
    මෙතුල අන්තර්ගත කරන code segments අනිවාර්‍යයෙන්ම execute කිරීම සිදුවේ(Exception එකක් හටගත්තත් නැතත්). finally statement එක යෙදෙන අවස්ථා සඳහා උදාහරණ ලෙස open කරන ලද stream එකක් close කිරීම වැනි අවස්ථා දැක්විය හැකිය.

    Exception class එකෙහි ඇති getMessage() method එක පිලිබඳව
    මෙමගින් Exception එකට අදාල වැඩිදුර තොරතුරු String එකක් ලෙස return කරයි. 
     
    අපි දැන් බලමු අපිවිසින් අපේම(our own) Exception සාදන ආකාරය සහ ඒවා throw කරන්නේ කවර ආකාරයෙන්ද කියලා. 
    මෙහිදී මූලිකවම සිදුකරන්නේ Exception නම් predefined class එක extend කරමින් නව class එකක් සෑදීමයි.
    උදා: OwnExceptionDemo.java

    /**     
    *class  : OwnExceptionDemo     
    *Author : Kanishka Dilshan     
    *Purpose: Demonstrate howto write custom exceptions   
    *Blog   : http://javaxclass.blogspot.com     
    */ 
    
    class Printer{
     /*assumption: 
      *printing 1 page will decrease the ink level by 1
      */
     int inkLevel;//minimum=0
     int pages;
     
     public Printer(int p,int inkLevel){
      pages=p;
      this.inkLevel=inkLevel;
     }
    
     public void print(){
      try{
       if(inkLevel==0){
        throw new EmptyCartridgeException();
       }else if(pages==0){
        throw new NoPagesException();
       }else{
        pages-=1;
        inkLevel-=1;
        System.out.println("Page completed.");
       }
      }catch(EmptyCartridgeException ecEx){
       System.err.println(ecEx.getMessage());
      }catch(NoPagesException npEx){
       System.err.println(npEx.getMessage());
      }
     }
    }
    
    class NoPagesException extends Exception{
     NoPagesException(){
      super("No pages in the paper tray!");
     }
    } 
    
    class EmptyCartridgeException extends Exception{
     EmptyCartridgeException(){
      super("Cartridge is empty. Please refill it");
     }
    }
    
    class OwnExceptionDemo{
     public static void main(String args[]){
      //creating Printer object and and calling print() method
      System.out.println("---------p1 Printer object---------");
         Printer p1=new Printer(5,100);
      for(int i=0;i<8;i++) //this will cause NoPagesException
       p1.print();
    
      System.out.println("\n---------p2 Printer object---------");
      Printer p2=new Printer(5,4);
      for(int i=0;i<5;i++) //this will cause EmptyCartridgeException
       p2.print();
     }
    }
    
    මෙම වැඩසටහනෙහි output එක පහත පරිදි වේ.
    දැන් ඔබට Error handling පිලිබඳව යම් වැටහීමක් ඇතැයි සිතමි. වැඩිදුරටත් අභ්‍යාසයේ යෙදීමෙන් තවදුරටත් එය තහවුරු කරගැනීමට උත්සාහ ගන්න. ගැටලු සහගත තැන් ඇත්නම් ඒවා ඉදිරිපත් කරන්න. 
    මීලඟ පාඩමෙන් අපි Threads පිලිබඳ අධ්‍යයනය කරමු.

    Wednesday, August 11, 2010

    GUI සැකසීම සඳහා Basic Layouts යොදාගන්නා අයුරු

    අපි මෙම පාඩමෙන් සාකච්ඡා කරන්න යන්නේ ජාවා වැඩසටහන් සඳහා අතුරුමුහුනත් සැකසීමේදී components සුදුසු පරිදි ස්ථානගත කිරීම සඳහා layouts යොදාගන්නා ආකාරයයි. මෙම layouts java.awt පැකේජයේ අන්තර්ගත වනවා. අප මෙම පාඩමේදී මූලික වශයෙන් පහත දැක්වෙන layouts භාවිතා කරන අයුරු සලකා බලමු.
    • FlowLayout
    • BorderLayout
    • GridLayout
    අපි මුලින්ම බලමු ජාවා අතුරුමුහුණතකට Layout manager එකක් set කරන්නේ කොහොමද කියලා.
    syntax:
    void setLayout(LayoutManager layoutObj);
    උදා:
    this.setLayout(new FlowLayout());
    

    දැන් අපි ඉහත සඳහන් කල layouts විස්තරාත්මකව සලකා බලමු

    FlowLayout

    Panel class එකෙහි සහ එහි සියලුම child classes (උදා:Applet,JApplet,JPannel,.. ) වල default layout එක වන්නේ මෙයයි. සාමාන්‍යයෙන් components place(ස්ථානගත) කරනුයේ වමේ සිට දකුණට සහ උඩ සිට පහලට යන ආකාරයෙනි(left to right and top to bottom).
    constructor:
    FlowLayout(int align, int hgap, int vgap);
    
    align: මෙමගින් සඳහන් කරනුයේ components ස්ථානගත කරනුයේ මොන පැත්තෙන් ආරම්භ වන පරිදිද යන්නයි
    FlowLayout.LEFT
    FlowLayout.CENTER
    FlowLayout.RIGHT
    hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.

    දැන් අපි FlowLayout එක යොදාගෙන සරල  චිත්‍රකමුහුනතක් (GUI )සකසන අයුරු බලමු.
    Code:
    import java.awt.FlowLayout;
    import javax.swing.*;
    
    public class FlowLayoutDemo extends JFrame{
    
     public FlowLayoutDemo(){
      setTitle("FlowLayoutDemo Demo");  
      JPanel myPanel=new JPanel();
      myPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
      //creating JButton objects
      JButton btn1=new JButton("Component1");
      JButton btn2=new JButton("Component2");
      JButton btn3=new JButton("Component3");
      JButton btn4=new JButton("Component4");
      JButton btn5=new JButton("Component5");
      JButton btn6=new JButton("Component6");
      //adding JButton objects to the panel
      myPanel.add(btn1);
      myPanel.add(btn2);
      myPanel.add(btn3);
      myPanel.add(btn4);
      myPanel.add(btn5);
      myPanel.add(btn6);
      this.add(myPanel);
      this.setSize(492,100);
      this.setDefaultCloseOperation(3);
      this.setVisible(true);
     }
    
     public static void main(String args[]){
      new FlowLayoutDemo();
     }
    }
    

    BorderLayout

    Window සහ එහි sub class සඳහා default layout එක වේ. මෙහිදී container එක කලාප 5 කට බෙදනු ලැබේ ඒවා නම්
    1. North
    2. South
    3. East
    4. West
    5. Center
    components place කරනුයේ මෙම කලාප 5 ට වේ.
    Constructor:
    BorderLayout(int hgap, int vgap) 
    
    hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.

    දැන් අපි BorderLayout එක යොදාගෙන සරල චිත්‍රකමුහුනතක් (GUI )සකසන අයුරු බලමු.
    Code:
    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class BorderLayoutDemo extends JFrame{
    
     public BorderLayoutDemo(){
      setTitle("BorderLayout Demo");  
      JPanel myPanel=new JPanel();
      myPanel.setLayout(new BorderLayout(5,5));
      //creating JButton objects
      JButton btn1=new JButton("North");
      JButton btn2=new JButton("East");
      JButton btn3=new JButton("South");
      JButton btn4=new JButton("West");
      JButton btn5=new JButton("Center");
      //adding JButton objects to the panel
      myPanel.add(btn1,BorderLayout.NORTH);
      myPanel.add(btn2,BorderLayout.EAST);
      myPanel.add(btn3,BorderLayout.SOUTH);
      myPanel.add(btn4,BorderLayout.WEST);
      myPanel.add(btn5,BorderLayout.CENTER);
    
      this.add(myPanel);
      this.pack();
      this.setDefaultCloseOperation(3);
      this.setVisible(true);
     }
    
     public static void main(String args[]){
      new BorderLayoutDemo();
     }
    }
    
    output:

    GridLayout

    මෙම layout එක බොහෝ දුරට FlowLayout එකට සමානය නමුත්  components place කරනුයේ සමාන cell sizes ඇති virtual grid එකක් තුලටය. මෙහිදී අදාල component එකෙහි preffered size එක නොසලකා හැරේ.

    Constructor: 
    GridLayout(int rows, int cols, int hgap, int vgap) ;
    

    rows, cols : මෙමගින් දැක්වෙන්නේ components add කරනු ලබන virtual grid එකෙහි rows සහ columns ප්‍රමාණයයි.

    hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.
    Code:

    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class GridLayoutDemo extends JFrame{
    
     public GridLayoutDemo(){
      setTitle("GridLayoutDemo Demo");  
      JPanel myPanel=new JPanel();
      myPanel.setLayout(new GridLayout(2,3,5,5));
      //creating JButton objects
      JButton btn1=new JButton("Component1");
      JButton btn2=new JButton("Component2");
      JButton btn3=new JButton("Component3");
      JButton btn4=new JButton("Component4");
      JButton btn5=new JButton("Component5");
      JButton btn6=new JButton("Component6");
      //adding JButton objects to the panel
      myPanel.add(btn1);
      myPanel.add(btn2);
      myPanel.add(btn3);
      myPanel.add(btn4);
      myPanel.add(btn5);
      myPanel.add(btn6);
    
      this.add(myPanel);
      this.pack();
      this.setDefaultCloseOperation(3);
      this.setVisible(true);
     }
    
     public static void main(String args[]){
      new GridLayoutDemo();
     }
    }
    
    output:

    අපි ඉහත සාකච්ඡා කල layout managers කිහිපය GUI සෑදීමේදී බහුල වශයෙන් යෙදේ.

    වඩාත් සංකීර්ණ GUI සෑදීම
    මෙහිදී සිදු කෙරෙනුයේ Layouts කිහිපයක් සංයුක්ත කොට කොට භාවිතා කිරීමය. Panel objects කිහිපක් සුදුසු පරිදි යෙදවීමෙන් අපට වඩාත් සංකීර්ණ GUI සකසාගත හැකිය.
    දැන් අපි Windows calculator එකෙහි අනුරුවක් අප ඉගෙනගත් LayoutManagers යොදාගෙන සෑදීමට උත්සාහ කරමු.
    Output:
    code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ComplexGUIDemo extends JFrame{
    
     public ComplexGUIDemo(){
      //this.setLayout(new GridLayout(3,1));
      this.setLayout(new FlowLayout(FlowLayout.RIGHT));
      //main panels
      JPanel txtPanel=new JPanel();
      JPanel numPanel1=new JPanel();
      JPanel numPanel2=new JPanel();
      //sub panels of numPanel2
      JPanel numPanel2Sub1=new JPanel();
      JPanel numPanel2Sub2=new JPanel();
      
      JTextField textBox=new JTextField("0",33);
      textBox.setHorizontalAlignment(JTextField.RIGHT);
      txtPanel.setLayout(new GridLayout(1,1));
      txtPanel.add(textBox);
      
      numPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
      JButton btnbacksp=new JButton("Backspace");
      JButton btnce=new JButton("CE");
      JButton btnc=new JButton("C");
      numPanel1.add(btnbacksp);
      numPanel1.add(btnce);
      numPanel1.add(btnc);
      
      numPanel2Sub1.setLayout(new GridLayout(4,1,5,5));
      JButton btnMC=new JButton("MC");
      JButton btnMR=new JButton("MR");
      JButton btnMS=new JButton("MS");
      JButton btnMPl=new JButton("M+");
      numPanel2Sub1.add(btnMC);
      numPanel2Sub1.add(btnMR);
      numPanel2Sub1.add(btnMS);
      numPanel2Sub1.add(btnMPl);
      
      numPanel2Sub2.setLayout(new GridLayout(4,5,5,5));
      JButton btn7=new JButton("7");
      JButton btn8=new JButton("8");
      JButton btn9=new JButton("9");
      
      JButton btndv=new JButton("/");
      JButton btnsqrt=new JButton("sqrt");
      numPanel2Sub2.add(btn7);
      numPanel2Sub2.add(btn8);
      numPanel2Sub2.add(btn9);
      numPanel2Sub2.add(btndv);
      numPanel2Sub2.add(btnsqrt);
      
      JButton btn4=new JButton("4");
      JButton btn5=new JButton("5");
      JButton btn6=new JButton("6");
      
      JButton btnmul=new JButton("*");
      JButton btnperc=new JButton("%");
      numPanel2Sub2.add(btn4);
      numPanel2Sub2.add(btn5);
      numPanel2Sub2.add(btn6);
      numPanel2Sub2.add(btnmul);
      numPanel2Sub2.add(btnperc);
      
      JButton btn1=new JButton("1");
      JButton btn2=new JButton("2");
      JButton btn3=new JButton("3");
      
      JButton btnmin=new JButton("-");
      JButton btn1bx=new JButton("1/x");
      numPanel2Sub2.add(btn1);
      numPanel2Sub2.add(btn2);
      numPanel2Sub2.add(btn3);
      numPanel2Sub2.add(btnmin);
      numPanel2Sub2.add(btn1bx);
      
      JButton btn0=new JButton("0");
      JButton btnPM=new JButton("+/-");
      JButton btnpt=new JButton(".");
      
      JButton btnpl=new JButton("+");
      JButton btneq=new JButton("=");
      numPanel2Sub2.add(btn0);
      numPanel2Sub2.add(btnPM);
      numPanel2Sub2.add(btnpt);
      numPanel2Sub2.add(btnpl);
      numPanel2Sub2.add(btneq);
      
      numPanel2.setLayout(new FlowLayout());
      numPanel2.add(numPanel2Sub1);
      numPanel2.add(numPanel2Sub2);
      
      this.add(txtPanel);
      this.add(numPanel1);
      this.add(numPanel2);
      
      this.setTitle("My Calculator");
      this.setDefaultCloseOperation(3);
      this.setVisible(true);
      this.setSize(390,235);
     }
    
     public static void main(String args[]){
      new ComplexGUIDemo();
     }
    }
    
    GUI සම්බන්ද තවත් කරුණු අපි ඉදිරි පාඩම්වලදී සාකච්ඡා කරමු.
    මෙම පාඩමට සම්බන්ද source codes බාගත කරගැනීම සඳහා සඳහා පහත ලින්ක් එක භාවිතා කරන්න. 

    Saturday, July 31, 2010

    Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම III කොටස

    JRadioButton
    විකල්ප කිහිපයක් දී ඇති විටදී ඉන් එක් විකල්පයක් තෝරාගැනීමට අවකාශ සැලසීමට මෙම component එක යොදාගත හැක. උදාහරණයක් පහත ඉදිරිපත් කර ඇත. Mr,Mrs,Ms,Dr යන විකල්ප වලින් එකක් පමණක් තෝරාගැනීමට මෙහිදී සිදුවේ. මෙම පාඩමෙන් සිදුකෙරෙන්නේ JRadioButton එකක් සාදාගන්නා ආකාරය පමණි. මේවායේ event handling සිදුකිරීම ඉදිරි පාඩම් වලදී සාකච්ඡා කෙරේ.
    JRadioButtonDemo.java
    import javax.swing.*;
    import java.awt.*;
    
    public class JRadioButtonDemo extends JFrame{
     
     public JRadioButtonDemo(){
       setTitle("JRadioButton Demo");
       setLayout(new FlowLayout(FlowLayout.LEADING  ));
       setSize(300,300);
       JRadioButton jrb_mr=new JRadioButton("Mr");
       JRadioButton jrb_mrs=new JRadioButton("Mrs");
       JRadioButton jrb_ms=new JRadioButton("Ms");
       JRadioButton jrb_dr=new JRadioButton("Dr");
       //grouping
       ButtonGroup btnGroup=new ButtonGroup();
       btnGroup.add(jrb_mr);
       btnGroup.add(jrb_mrs);
       btnGroup.add(jrb_ms);
       btnGroup.add(jrb_dr);
       //add them to the JFrame
       add(jrb_mr);
       add(jrb_mrs);
       add(jrb_ms);
       add(jrb_dr);
       setVisible(true);
     }
    
     public static void main(String args[]){
      new JRadioButtonDemo();
     }
    }
    
    Output:

    line14:
    මෙහිදී අපි සාදාගත් JRadioButton ටික group කිරීම සිදුකෙරේ. මේවා group නොකලහොත් Mr, Mrs, Ms, Dr යන විකල්පයන්ගෙන් එකක් හෝ කිහිපයක් තේරීම සිදුකල හැකිය. නමුත් JRadioButton යෙදීමේ අරමුණ මෙය නොවන නිසා සාමාන්‍යයෙන් JRadioButtons group කිරීම සිදුවේ.

    JList
    කිසියම් elements ප්‍රමාණයක් පහත ආකාරයට දැක්වීමට JList නම් swing component එක භාවිතාකෙරේ.
    උදා: JListDemo.java

    import javax.swing.*;
    import java.awt.*;
    
    public class JListDemo extends JFrame{
     
     public JListDemo(){
       setTitle("JList Demo");
       setLayout(new FlowLayout(FlowLayout.LEADING  ));
       setSize(300,300);
       
       DefaultListModel dlm=new DefaultListModel();
       JList countries=new JList(dlm);
       dlm.addElement("Sri Lanka");
       dlm.addElement("India");
       dlm.addElement("Japan");
       dlm.addElement("Usa");
       dlm.addElement("UK");
       
       add(countries); 
       setVisible(true);
     }
       
       public static void main(String args[]){
      new JListDemo();
       } 
    }
    
    JList එක construct කරගැනීමට පෙර DefaultListModel එකක් සාදාගැනීම කල යුතුයි. පසුව අපි සාදාගත් JList එකට දත්ත add කිරීම්ට JList එක clear කිරීමට, JList එකෙන් elements ඉවත් කිරීම මෙම DefaultListModel object එක හරහා සිදුවේ.Line11 දී මෙම DefaultListModel object එකක් සාදාගෙන ඇත. 

    Line12: මෙහිදී අපි සාදාගත් DefaultListModel object එක සහ JList එක සම්බන්ද කිරීම සිදුකෙරේ.

    දැන් අපි JList එක හා සම්බන්ද වැදගත් method කිහිපයක් සලකා බලමු.

    JList Methods
    විස්තරය
    int getSelectedIndex()  user විසින් list එකීහි මේ වනවිට select කර ඇති item එකෙහි index එක ලබාදේ.
    void setSelectedIndex()  Program එක තුලින් list එකෙහි item එකක් select කර ඇති ලෙසට දක්වයි.
    Object getSelectedValue()  user විසින් list එකීහි මේ වනවිට select කර ඇති item එක ලබාදේ
    boolean isSelectedIndex(int idx)  ලබාදෙන int value එකක් select වී ඇති item එකේ index value එක නම් true ලෙසටද නැතිනම් false ලෙසටද return කරයි
    setModel(ListModel model)  සකස් කරගත් ListModel object එකක් JList එක හා සම්බන්ද කිරීමට යොදාගැනේ
    DefaultListModel Methods
    විස්තරය
    void add(int index, Object element)  ලබාදෙන index අගයකට(location එකකට) item එකක් ඇතුලු කිරීම සිදුකරයි
    void addElement(Object obj)  list එකේ අගට item එකක් ඇතුලු කිරීම සිදුකරයි
    int capacity()  දැනට list එකේ capacity එක නොහොත් items ගනණ ලබාදේ
    void clear()  list එකේ ඇති සියලු items clear කිරීම සිදුකරයි.
    boolean  contains(Object obj)  ලබදෙන Object එකක් list එක තුල අන්තර්ගත වේනම් true ලෙසද නැත්නම් false ලෙසටද ලබාදේ.
    Object elementAt(int index)  ලබාදෙන index එකක ඇති value එක ලබාදේ
    boolean isEmpty()  list එක empty නම් true ලෙසටද නැත්නම් false ලෙසටද ලබාදේ
    Object remove(int index)  ලබාදෙන index එකක ඇති item එක ඉවත්කරයි
    void removeAllElements()  list එක තුල ඇති සියලු items ඉවත් කරයි

    JList එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.
    DefaultListModel එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.

    JList එකට scroll bar යෙදීමට අවශ්‍ය නම් JScrollPane එකක් යොදාගත හැකිය. එය සිදුකරන අයුරු පහත දැක්වේ.
    import javax.swing.*;
    import java.awt.*;
    
    public class JListDemo extends JFrame{
     
     public JListDemo(){
       setTitle("JList Demo");
       setLayout(new FlowLayout(FlowLayout.LEADING  ));
       setSize(300,300);
       
       DefaultListModel dlm=new DefaultListModel();
       JList countries=new JList(dlm);  
       
       JScrollPane scrollP=new JScrollPane(countries);
       scrollP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
       dlm.addElement("Sri Lanka");
       dlm.addElement("India");
       dlm.addElement("Japan");
       dlm.addElement("Usa");
       dlm.addElement("Canada");
       dlm.addElement("Australlia");
       dlm.addElement("Italy");
       dlm.addElement("South Affrica");
       dlm.addElement("Thailand");
       
       add(scrollP); 
       setVisible(true);
     }
       
       public static void main(String args[]){
      new JListDemo();
       } 
    }
    
    Output:
    පහත ජාවා ඇප්ලට් එක භාවිතයෙන් ඔබට ඉහත සඳහන් කල methods හි ක්‍රියාකාරීත්වය පහසුවෙන් වටහාගත හැකිවෙතැයි සිතමි.
    පාඩමේ භාවිතා කර ඇති මූලාශ්‍ර කේත(source codes) බාගත කරගැනීම සඳහා පහත ලින්ක් එක භාවිතා කරන්න.

    Friday, July 23, 2010

    Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම II කොටස

    අපි ඉහත කලින් පාඩමේදී සාකච්ඡා කලේ JLabel, JButton හා JTextField පිලිබඳවයි. මෙම පාඩමෙන් තවත් swing components කිහිපයක් පිලිබඳවා සාකච්ඡා කරමු.

    JTextArea

    මෙය JTextField සඳහා බොහෝ සමාන වුවද මෙහි lines කිහිපයකින් යුතු user inputs ගත හැක. එනම් address එකක් හෝ කිසියම් description එකක් ගැනීමේදී මෙය භාවිතා කල හැක.
    JTextArea(int rows, int columns)
    JTextArea(String text)
    JTextArea(String text, int rows, int columns)
    උදා;

    අපිට අවශ්‍ය නම් මෙම JTextArea සඳහා scrollbar යෙදිය හැක. මේ සඳහා කල යුත්තේ JScrollPane එකක් තුලට JTextArea එක දැමීමයි. මෙය සිදුකරන අයුරු පහත දැක්වේ.
    import javax.swing.*;
    import java.awt.*;
    class JTextAreaDemo extends JFrame{
    
     public JTextAreaDemo(){
      setTitle("JTextArea Demo");
      JTextArea jtxt=new JTextArea(10,20);
      JScrollPane scrollpane=new JScrollPane(jtxt);
      
      setLayout(new FlowLayout());
      setSize(300,300);
      setVisible(true);
      add(new JLabel("Description :"));
      add(scrollpane);
     }
     
     public static void main(String args[]){
      new JTextAreaDemo();
     }
    }
    
    Output:
    JScrollPane එකෙහි constructor එකට(line 8) JTextArea object එක වන jtxt object එක ලබාදීම මගින් scrollpane එකට textarea එක ඇතුලුකිරීම සිදුකර ඇත.

    JTextArea එකෙහි method ලෙස getText(),setText() බහුලව යෙදේ.


    JCheckBox

    විකල්ප 1ක් හෝ කීපයක් තේරීමට ඉඩලබාදිය යුතුවිට මෙය භාවිතා කෙරේ. 

    isSelected()method එක භාවිතා කර යම් CheckBox එකක් select කර ඇත්දැයි පරීක්ෂා කල හැක. Event handling පාඩමේදී JCheckBox පිලිබඳ වැඩිදුරටත් සාකච්ඡා කෙරේ.
    JCheckBox එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.
     

    Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම I කොටස

    අතුරු මුහුණත්(Graphical User Interfaces) යනු යම් පරිගණක වැඩසටහනක් භාවිතා කිරීම පහසු කරවන්නක්. සාමාන්‍ය පරිගණක පරිශීලකයෙක් නම් මේ වකවානුවේ බොහෝ දුරට භාවිතා කරනුයේ මෙවැනි වැඩසටහන්. ජාවා පරිගණක භාෂාව යොදාගෙන මෙවැනි GUI සහිත වැඩසටහන් සැකසීමට විශේෂ පහසුකම් ලබාදී තිබෙනවා. මේ සඳහා යොදාගත හැකි ප්‍රධාන ජාවා පැකේජ 2ක තමයි
    • AWT (Abstract Window Toolkit)
    • Swing
    විශේෂ හේතූන් කිහිපයක් නිසා Swing පැකේජය භාවිතා කිරීම ප්‍රයෝජනවත් වනවා. ඉන් ප්‍රධානම හේතුව වන්නේ swing පැකේජය pure Java වලින් ලියැවුනු එකක් වීම. එම නිසා සුවිශේෂ වාසි කිහිපයක් අදාල වැඩසටහනට ලැබෙනවා. මීට අමතරව AWT සහය දක්වන සියලුම පහසුකම් සඳහා swing ද සහය දක්වනවා. AWT හි නොමැති පහසුකම් ගනණාවක්ද swing හි අඩංගු වනවා.
    AWT සහ Swing හි වාසි අවාසි පිලිබඳ වැඩි විස්තර කියවීම සඳහා මෙම ලින්ක් එක භාවිතා කරන්න.

    Friday, July 16, 2010

    ජාවා interface සංකල්පය සහ එහි යෙදීම්

    Interface එකක් කිව්වාම එකපාරටම මතක් වෙන්නෙ GUI එකක් නැත්නම් web form එකක් නේ. නමුත් මෙම පාඩමේදී අපි සලකා බලන්නේ වස්තු පාදක සංකල්පයක් වන interface සංකල්පය පිලිබඳවයි. මෙම සංකල්පයත් ජාවා ක්‍රමලේඛනයේ යෙදීමේදී බහුල වශයෙන් භාවිතා වන්නක්. මෙම පාඩමේදී පෙර පාඩම වන "ජාවා තුල Abstract Classes යොදාගන්නා අයුරු" යන පාඩමේදී උගත් කරුණු වැදගත් වන නිසා අවශ්‍ය නම් නැවත එය අධ්‍යයනය කරන්න.

    interface එකක ඇති ප්‍රධාන ලක්ෂණ ගැන අපි සලකා බලමු.
    • interface එකක් යනු සම්පූර්ණයෙන්ම abstract class එකකි.

    • interface එකක ඇති methods සියල්ලම අර්ථදක්වා(define) ඇත්තේ method body එකකින් තොරවය(මෙයට හේතුව interface එක fully abstract වීමයි. එමනිසා මෙහි methods සියල්ලක්ම abstract වේ) 

    • interface එකක් තුල class variables (උදා : private int age;) පැවතිය නොහැක. නමුත් final attributes පැවතිය හැක(උදා: private final SIZE=512;)
    මීලඟට අපි සලකා බලමු ලියන ලද interface එකක් භාවිතා කිරීමේදී සැලකිය යුතු කරුණු.
    • class එකකට අවශ්‍ය නම් interface එකකට වැඩි ප්‍රමාණයක් භාවිතා කල හැක. එනම් class එකකට අවශ්‍ය නම් interface එකකට වැඩි ප්‍රමාණයක් implement කල හැක. මේ සඳහා implement keyword එක භාවිතාවේ.

    • යම් class එකක් interface එකක් implement කර ඇත්නම් එම interface එකෙහි ඇති සියලුම methods override කිරීම අනිවාර්‍යයෙන්ම කල යුතුය.

    • class එක abstract නම් එසේ අවශ්‍ය නොවේ.

    • class වලදී මෙන් inheritance සංකල්පය interface සඳහාද භාවිතා කල හැක. එනම් interface එකකදී වෙනත් interface එකක් extend කල හැක.

    • multiple inheritance නම් සංකල්පය C++ වලදී මෙන් java තුලදී කිරීමට ඉඩදී නොමැති වුවද, මෙම interface සංකල්පය යොදාගෙන එම ක්‍රියාවලිය ඉටුකරගත හැක.

    අපි දැන් බලමු interface එකක් ලිවීම සඳහා භාවිතා වන syntax එක
    interface MyInterface {
     
     final int VAL=512;
     
     void myMethod1();
     int myMethod2();
     ......................
     ...................... 
    }
    
    දැන් අපි බලමු interface සංකල්පයේ යෙදීම් ආදර්ශනය කිරීමට ලියන ලද සරල වැඩසටහනක්
    /**    
    *class  : CarControllerDemo    
    *Author : Kanishka Dilshan    
    *Purpose: Describe interface concept in Java  
    *Blog   : http://javaxclass.blogspot.com    
    */ 
    
    interface CarController{
     final float MAX_SPEED=320f;
     
     void accelerate(); //accelerate the car
     void applyBreak(); //apply breaks
    }
    
    class BMW implements CarController{
     private String model;
     private float speed;
     
     public BMW(String model){
      this.model=model;
      speed=0f;
     }
     public void accelerate(){
     //implementing accelerate() method in the interface
      if(speed < MAX_SPEED)
       speed+=15;
     }
     
     public void applyBreak(){
     //implementing applyBreak() method in the interface
      if(!((speed-20) < 0))
       speed-=20;
      else
       speed=0;   
     }
     
     public void showDetails(){
      System.out.println("Car model : "+model);
      System.out.println("Speed     : "+speed);
     }
    }
    
    //another car called Nissan implements CarController interface
    class Nissan implements CarController{
     private String model;
     private float speed;
     
     public Nissan(String model){
      this.model=model;
      speed=0f;
     }
     public void accelerate(){
     //implementing accelerate() method in the interface
      if(speed < MAX_SPEED)
       speed+=15;
     }
     
     public void applyBreak(){
     //implementing applyBreak() method in the interface
      if(!((speed-25) < 0))
       speed-=25;
      else
       speed=0;
     }
     
     public void showDetails(){
      System.out.println("Car model : "+model);
      System.out.println("Speed     : "+speed);
     }
    }
    
    
    public class CarControllerDemo{
     public static void main(String args[]){
      //creating BMW car object
      BMW car1=new BMW("BMW M6");
      Nissan car2=new Nissan("Altima Sedan");
      
      //accelerate car1 twice
      car1.accelerate();
      car1.accelerate();
      //applyBreak() for car1
      car1.applyBreak();
      car1.showDetails();
      
      //accelerate car2 4 times
      car2.accelerate();
      car2.accelerate();   
      car2.accelerate();
      car2.accelerate(); 
      //applyBreak() for car2
      car2.applyBreak();  
      car2.showDetails();
     }
    }
    
    Output:

    පැහැදිලි කිරීම:
    ඉහත උදාහරණයේ Line8 සිට Line13 දක්වා ඇත්තේ define කරන ලද interface එකයි. BMW සහ Nissan යන class තුලදී implement කර ඇත්තේ එම interface එක තුල ඇති accelerate() සහ applybreak() යන methods දෙකයි. මීට පෙර පාඩම් වලදී උගත් සිද්ධාන්ත භාවිතා කරමින් ඉහත කේතයේ අනෙකුත් කොටස් අවබෝධ කිරීම ඔබගේ කාර්‍යයකි. 

    ජාවා ක්‍රමලේඛන භාෂාව යොදාගෙන වැඩසටහන් ලිවීමේදී පහත අවස්ථා වලදී මෙම interface භාවිතය ප්‍රායෝගිකව දැකගත හැකිය.
    • Event handling
    • Database connectivity
    මෙම පාඩමත් සමග ජාවා වල මූලික OOP සංකල්ප කොටස අවසන් වේ. ඉදිරි පාඩම් වලදී ජාවහි අතුරු මුහුණත් සැකසීම පිලිබඳ අධ්‍යයනය කරමු.