Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Friday, June 8, 2012

ජාවා අභියෝගය: ගැටළුවලට විසඳුම් මෙන්න...!!!

 කළින් post එකේ මම ඉදිරිපත් කළා ජාවා ගැටළු කීපයක්.


බොහෝ දෙනෙක් විසින් ලිපිය කියවා තිබුණත් ගැටළුවලට පිළිතුරු සැපයීමට ඉදිරිපත් වී තිබුනේ නැහැ.

නමුත් confuzed programmer විසින් නිවැරදි පිළිතුරු ඉදිරිපත් කර තිබුනා.

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



1) for loop  එක i=0  සිට i=4 දක්වා පස් වතාවක් execute වෙයි.
switch එකේදී අදාළ i අගයට අනුව case statement එක execute වෙයි.
break statement ඇත්නම් flow එක නැවතී,switch case එකෙන් එළියට යයි.එනම් ඊළඟ i අගය execute වීම ඇරඹෙයි.
එක සහ තුන cases වලදී break statement එක නොමැත. එමනිසා ඊළඟ case එකද execute වන අතර   x සහ z දෙවරක් print වෙයි.

ඒ අනුව පිළිතුර: v w x x y z z

2) valueOf method එකෙන් කරන්නේ input parameter එකේ String representation එක ලබාදීමයි.Input parameter එක object එකක් හෝ char array එකක් හෝ ඕනෑම primitive type එකක් විය හැක. එමනිසා එයින් 12  print වෙයි.

toString method එකෙන්  කරන්නේ String object එකට reference එකක් return කිරීමයි.එය අළුත් String instance එකක් නිර්මාණය නොකරයි.

ඒ අනුව පිළිතුර: 12, true

3) Parameter යක් නැති m1() method එකේදී A.m1() යන්න B.m1() මඟින් override වෙයි.Overriding නීති අනුව print වන්නේ B.m1() යන්නය.එක් parameter යක් අවශ්‍ය m1() method එකේදී m1 method එක overload වී ඇත. එමනිසා overloading rules සැළකිය යුතුය.

Overloaded methods දෙකෙන් එකක් instance method එකක් වන අතර අනික static method එකක් වෙයි.මෙහිදී call වෙන්නේ static method එකයි.

ඒ අනුව පිළිතුර: hi, B.m1

4) synchronized යනු method modifier එකක් මිස class modifier එකක් නොවේ.එමනිසා class එකක් synchronized මඟින් declare කිරීමට යාමේදී compile error ලැබේ.
synchronized මඟින් අදහස් කරන්නේ implementation detail එකක් නිසා, implementation එකක් නැති abstract method එකකට එය යෙදීමේ අර්ථයක් නැත.

ඒ අනුව පිළිතුර: 2,3,4 lines

5) සෑම array type එකක්ම Serializable interface එක implement කරයි. එමනිසා Serializable type එකේ reference එකකට array එකක් assign කිරීමේ ගැටළුවක් නැත.

ඒ අනුව පිළිතුර: ගැටළුවකින් තොරව compile වීම සහ run වීම සිදුවෙයි.





Monday, June 4, 2012

ඔබේ ජාවා දැනුම කොහොමද? හැකිනම් විසඳන්න, ජාවා වලින් පොඩි ගැටළු කීපයක්..

මේවා ඇත්තටම ඉතාම සරල මට්ටමේ ජාවා ගැටළු කීපයක්.
ජාවා පිළිබඳ මූලික මට්ටමේ සිද්ධාන්ත පිළිබඳ ගැටළු..
නමුත් ආරම්භය සඳහා සරල ගැටළු කීපයක් ඉදිරිපත් කළත් ඉදිරියට තරමක් අමාරු ගැටළු ඉදිරිපත් කරන්න බලාපොරොත්තු වෙනවා.

පහත ගැටළුවලට ඔබේ පිළිතුර සටහන් කරන්න comment තීරය භාවිතා කරන්න.
කෙසේ වෙතත් නිවැරදි පිළිතුරු ඉක්මනින් පළ කිරීමට බලාපොරොත්තු වෙනවා.

වැඩසටහන් compile කර run කර බලා පිළිතුරු ලියා පලක් නැත.:-) .
Program එක කියවා බලා ප්‍රතිඵලය කීමට උත්සහ කරන්න. ප්‍රතිඵලයට හේතුව ද දක්වන්න.

1) පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

class ABC {
public static void main ( String args [] ) {
for ( int i=0 ; i < 5 ; i++ ) {
switch (i) {
case 0 : System.out.print ("v" ) ; break ;
case 1 : System.out.print ("w" ) ; 
case 2 : System.out.print ("x" ) ; break ;
case 3 : System.out.print ("y" ) ; 
case 4 : System.out.print ("z" ) ; break ;
default : System.out.print ("d" ) ;
   }
  }
 }
}

2)  පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

 class ABC {
 public static void main ( String args [] ) {
 System.out.print (String.valueOf (1) + String.valueOf (2) ) ;
String s1 = "S1" ;
String s2 = s1.toString ();
System.out.print ( " , " + ( s1==s2) ) ;
 }
}


3)   පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

class A {
void m1 () {
System.out.print("A.m1" ) ; }
 }
class B extends A {
   void m1() {
   System.out.print("B.m1" ) ; }
   static void m1( String s ) {
   System.out.print(s + "," ) ; }
  }

class C {
public static void main ( String args [] ) {
B.m1("hi") ;
new B().m1() ;
 }
}

4) Compile errors ලබාදෙන්නේ පහත කුමන lines වලින්ද?

abstract class ABC {                                         // LINE 1
abstract  synchronized void m1 () ;                    // LINE2
abstract synchronized class B {}                       // LINE3
synchronized class C extends B { }                   // LINE4
}

5) පහත වැඩසටහන compile කර run කිරීමට උත්සහ කළහොත් ලැබෙන ප්‍රතිඵලය කුමක්ද?

import java.io.Serializable ;
class ABC {
public static void main ( String args [] ) {
int [] i = { 1, 2 , 3 } ;
Serializable  s = i ;
i = ( int [] ) s ;
 }
}

ගැටළුවලට නිවැරදි පිළිතුරු විස්තරාත්මකව මෙතනින්.





















Monday, April 11, 2011

Encapsulation හඳුනා ගනිමු.....!

උදාහරණයක් සලකමු.

class woman{
private int age;

public int getAge(){
return age;
}

public void setAge(int x){
age=x;
}
}

මෙහි woman class එකේ ඇති age නම් variable එකට private access modifier එක යොදා ඇත.
එමනිසා එහි අගය ලබාගැනීම හෝ වෙනස් කිරීම වෙනත් class වල සිට කල නොහැක.
මෙය encapsulation ලෙස හැඳින්වෙයි.

Class එකක ඇති සියළු variables(properties) private විට එය encapsulated class එකකි.

ex) class A{
private int x;
} //encapsulated class

ex) class B{
private int p;
int q;
} // not encapsulated.

getter සහ setter methods ඇතිවිට එය tightly encapsulated class එකක් ලෙස හැඳින්වෙයි.


ex)class A{
private int x;
}

class B extends A{
int y;
}

1) variable x පමනක් private නම් class A encapsulated.නමුත් class B encapsulated නොවේ.

2) x,y දෙකම private නම්,A,B classes දෙකම encapsulated වේ.

3)y පමණක් private නම්, classes දෙකම encapsulated නොවේ.


Encapsulation හි භාවිත

1.data hiding
2.data immutability
3.validation

1.data hiding

variable age හි අගය වෙනත් class එකක සිට ලබාගත නොහැක.

public class woman{
private int age;

public void setAge(int x){
age=x;
}
}

2.data immutability

variable age හි අගය වෙනත් class එකක සිට වෙනස් කල නොහැක.

class woman{
private int age;

public int getAge(){
return age;
}
}

නමුත් class එක තුල සිට මෙහි අගය වෙනස් කල හැකිය.

method 1) private void doChange(){
age=20;}

method 2) woman(int x){
this.age=x;}

නමුත් variable එක final කලහොත් කොහේ සිටවත් වෙනස් කල නොහැක.

3) Validation

public class woman{
private int age;

public void setAge(int x){
if(x>25){
age=x;
}else{
throws IllegalArgumentException;
}
}

Tuesday, March 22, 2011

Classes and Objects in Java

class: a set of same type of objects
a blueprint

ex: class Family
class Drama
class Student
class Button
class Connection
class HttpServlet

objects: instances of a class
attributes/variables : properties of those objects
methods: functions/ activities
static variable: common attribute to all objects

ex1)
public class Family {
float height;
String name;
static String surname ="perera";


public static void main(String[] args) {
Family father = new Family();
father.height = 6.0f;
father.name = "sunil";
System.out.println(father.height);
System.out.println(father.name+" "+surname);
System.out.println(surname);
father.sing();

Family son = new Family();
son.name="gayan";
System.out.println(son.surname);
System.out.println("full name: "+son.name+" "+surname);
}

public void sing(){
System.out.println("hehe");
}

}

class: Family
objects:father, son
variables/attributes: name, height
static variable: surname
method:sing


output
sunil perera
perera
hehe
perera
full name: gayan perera

Saturday, March 5, 2011

Final භාවිතය

Final

final කියන දෙයින් අදහස් කරන්නේ අවසානය (end) හෝ වෙනස් කල නොහැකි බව (immutability)

final යෙදිය හැකි තැන්: classes, methods, member variables, method local variables

final classes

final classes, extend කල නොහැක.
final class යන්නෙන් අදහස් කරන්නේ, එම class එක inheritance hierarchy එකේ අවසානය බවයි.

final class එකක abstract methods තිබිය නොහැක.

අනිත් ඕනෑම වර්ගයක methods තිබිය හැක.

ex) class A{}
final class B extends A{}

class B තව දුරටත් extend කල නොහැක.

final methods

-static හෝ non-static (instance) methods වලට final යෙදිය හැක.
-final methods, override කල නොහැක.
-නමුත් final methods, sub classes වලට inherit කල හැක.

final variables