Professional Documents
Culture Documents
មេម ៀនទី៤
1>mUldæanRKiHén Class
Class KWCalMnaMmYysMrab;kMnt;TrM g;rbs; Object .vakMnt;eGay data nig code sMrab;RbtibtiþeTAelI
data enaH . java eRbIkarkMnt;lkçN³ class EbbenHedIm,IbegáIt object . object KWCa instances én class
mYy.
1.1 TMrg;TUeTAén Class
mYymandUcxageRkamenH³
Class classname {
type instance-uariable1;
type instance-uariable2;
/ /..
type instance-uariableN;
type methodname1(parameter-list) {
/ / body of method
}
Type methodname2(parameter-list) {
/ / boody of method
}
/ / …
Type methodnameN(parameter-list) {
/ / boody of method
}
}
Page 1
Industrial Technical Instituted 2015
Page 2
Industrial Technical Instituted 2015
passengers 7
minivan fuelcap 16
mpg 21
sports passengers 2
fuelcac 14
mpg 12
Page 3
Industrial Technical Instituted 2015
nwgbgðajtMéldUcKña KW 26 .
Page 4
Industrial Technical Instituted 2015
4/ GMBI Method
object-oriented eTAeGay class .]TahrN¾ kMEnERbfµién Vehicle man method mYyeQµaH range()
EdlbgðajGMBIcMgaycrényanynþ .
Page 5
Industrial Technical Instituted 2015
lkçxNÐTBI IrkalNaXøa return )anRbtibtiþkar . return 2manBIrTMrg;KWmsY Mrab;eRbIkúñg method RbePT void nig
mYyeToteRbIsMrab;eGaytMél.
enAkñgú method RbbePT void eyIgGaceFVikarbBa©b; method mYyPøam²edayeRbItrM g; return dUcxag
eRkamenH ³
kalNaXøaenHRbtibtiþkar eBlenaHkarepÞógpÞat;kmµviFInwgeGaytMéleTAkan;GñkehAeRbIeday
rMlgecalnUv code EdlenAesssl;kñúg method. ]TahrN¾ cUrsegát method enH³
void myMeth ( ) {
int i ,
for ( i = 0 , i < 10 , i + + ) {
if ( i = = 5) return , // stop at 5
System . out . println ( ) ,
}
}
Page 6
Industrial Technical Instituted 2015
kñúgenH dMeNIrdEdl² for nwgdMeNIrkarBI 0 dl; 5 b:ueNÑaH eRBaHeBlEdl i esIµ 5 enaH method eGay
tMélbBa¢b;. Method eGaytMélmYyeTAkan;GñkehaAeRbIenaHmanTMrg;dUcxageRkam³
return value;
4>3 karRbI)a:ra:Em:Rt
eKGacbBa¢ÚntMélmYy b¤eRcIneTAkan; method mYyenAeBlvaRtUv)anehAeRbI. tMélmYyEdl bBa¢Ún
eTAkan; method enaHehAfa argument. enAkñúg mehtod GBaØatEdlTTYl argument ehAfa )a:ra:Em:Rt.
)a:ra:Em:RtRtUv)anRbkalenAxagkñúgsBaØavg;RkckbnÞab;BIeQµaHrbs; method. EbbbTénkarRbkal)a:ra:Em:Rt
KWdcU KñaeTAnwgkareRbIGBaØatEdr.
Page 7
Industrial Technical Instituted 2015
class Factor{
boolean isFactor ( int a, int b ) {
if (( b&a ) = = 0 ) return true,
else return false ,
}
}
class IsFact {
public static void main (String args [ ] ) {
Page 8
Industrial Technical Instituted 2015
if ( x . isFactor (2, 20 ) )
System. out. println ( “ 2 is factor ” ) ,
if ( x . isFator ( 3, 20 ) )
System. out. println ( “ this won’t be displayed ” ) ,
}
}
edaysresrlMdab;XøadUcxageRkamenH³
innivan. Passengers= 7;
innivan. Fuelcap = 16;þ
innivan. Mpg = 21;
eQµaH Class rbs;va ehIyvaRsedógeTAnwg method Edr. eTaHCaya:gNak¾eday Constructor BMmu an bB¢aak;
BIRbePTTinñn½yeT,Iy. CaFmµtaeyIgeRbI Constructor mYysMrab;knM t;tMéldMbUgeTAeGayGBØaat EdlkMNt;
eday Class b¤edVIkarNamYy EdltMrUv[begáIt Object manlkçN³eBjelj.
RKb; Class TaMGs;Etgman Constructor minfaeyIg)anbegáIt b¤km¾ in)anbegáIteLIy eRBaH Java pþln; Uv
Constructor EdlmanlkçN³ default edaysV½yRbvtþ ehIykMNt;tMéltMbUgeTA[GBØØaatTaMGs;esµI sUnü.
}
}
6>kareRbIBakü this
vamansar³sMxan;Nas;cMeBaHkareRbIBakü this . kalNa method mYyRtUv)anehAeRbIenaHvabBa¢Ún
argument mYyedaysV½yRbvtiþ EdlCakarbBa¢ak;mYyeTAeGay object kMBugeRbI ( KWCa object Edl mothod
enaHRtUv
RtYvehAeRbI). karbBa¢ak;enHKWCa this . edIm,Iyl;bBaðarenH CabzmcUrsegátkmµviFImYyEdlbegáItnUv class
mYymaneQµaHfa Pwr . Class enHGacKNnalTÐplénmYycMnYnelIkCasV½yKuNéncMnYnKt;³
class Pwr {
double b ;
int e ;
double val ;
Pwr ( doule base , int exp ) {
b = base ;
e = exp ;
val = exp ;
if ( exp = = 0 ) return ;
for ( ; exp >0; exp - - )
val = val * bass ;
}
double getPower() {
return val ;
}
}
class DemoPwr {
public static void main ( String args [ ] ) {
Pwr x = new Pwr ( 4. 0, 2 );
Pwr y = new Pwr ( 2 .5, 1 );
Pwr z = new Pwr ( 5. 7, 0 );
System . out . printlh ( x.b + “raised to the” + x .e +
“power is” + x.getPower ( ));
System . out . printlh ( y.b + “raised to the” + y . e +
“power is” + y.getPower ( ));
System . out . printlh (z.b + “raised to the” + z . e +
“power is” + z.getPower ( ));
}
}
kñúgenH this sMedAeTAelI object Edl get Power( ) )anehAeRbI. ehtuenH this.val sMedAeTAelIkarcMlg val én
object enaH. karsresrXøaedaymineRbI this RKan;EtviFIsresrkat;b:eu NÑaH.
int e;
double val;
Pwr (double base, int exp) {
this.b = base;
this.e = exp;
this.val = 1;
if (exp = = 0 ) return;
for ( ; exp>0; exp--)
this.val = this.val * base;
}
double getPower( ) {
return this.val;
}
}
kalNabBaðaenHekIteT,IgenaHeQµaHGBaØatkñúgbM)aMgeQµaHrbs;GBaØatxageRkA. eyIgGaccUleRbI
GBaØatEdlbM)aMg edayeFIVkarbBa¢ak;tamry³Bakü this. ]TahrN_ ³
Pwr (double b, int e) {
this.b = b;
this.e = e;
val = 1;
if (e = = 0 )
return;
for ( ; e>o; e--)
val = val * b;
}
Page 12
Industrial Technical Instituted 2015
}
}
Page 13
Industrial Technical Instituted 2015
lT§plénkmµviFxI agelI)anbgðajdUcxageRkamenH³
Inside f ( int ) : 10
Inside f ( double ) : 10.1
Inside f ( int ) : 99
Inside f ( int ) : 10
Inside f ( double ) : 11.5
MyClass (int I ){
System . out .println ( “Inside MyClass(int). “);
X=I;
}
MyClass(double d) {
System .out . println ( “Inside MyClass(double).”);
X=(int) d;
}
MyClass(int I , int ,j ) {
System .out .println ( “Inside Myclass (int , int ). “);
X=I * j ;
}
}
Class overloadconsDemo {
Public static void main (string args[ ] ) {
Myclass t1`= new Myclass();
Myclass t2 = new Myclass (88);
Myclass t3 = new Myclass (17 .23 );
Myclass t4 = new Myclass (2.,4);
System .out . println ( “t1 . x : “+ t1 .x);
System .out . println ( “t2 . x : “+ t2 .x );
System .out . println (“t3 . x : “+ t3.x );
System .out . println ( “t4 . x : “+ t4 . x );
}
}
Sum =0;
For (int i=1 ; I < = num ; I ++)
Sum + =i;
}
//construct from another object .
Summation (summation ob ) {
Sum = ob . sum ;
}
}
Class sumDemo {
Public static void main (string args [ ] ) {
Summation s1 = new summation (5);
Summation s2 = new summation (s1);
System . out . println (“s1 . sum : “ + s1 . sum);
System . out . println (“s2 . sum : “ + s2 . sum);
}
}
lTæplénkmµviFxI agelIenH³
s1. sum : 15
s2. sum : 15
Page 16
Industrial Technical Instituted 2015
Page 17
Industrial Technical Instituted 2015
lTæplénkmµviFeI nHKW
a and b before call : 15 20
a and b after call : 15 20
ob . a = ob . a + ob . b ;
ob . b = -ob . b ;
}
}
class CallByRef {
public static void main (String args [ ] ) {
Test ob = new Test (15 , 20 ) ;
System. out. println (“ob . a and ob . b after call :” +
ob . a + “ ” + ob . b ) ;
ob . change (ob) ;
System. out. println (“ob . a and ob . b after call :” +
ob a + “ ” + ob . b ) ;
}
}
kmµvFi IenHeGaylT§pldUcxageRkam³
ob . a and ob . b before call : 15 20
ob . a and ob . b after call : 35 -20
Edlva)anbBa¢ÚleTA.
// Returning a String object .
class ErrorMsg {
String msgs[] = {“output Error”,“Input Error “,“Disk Full”,“Index out–of-
Bounds”};
// Return the error message. Return an object of
String getErrorMsg (int i) { type String.
if (i >=0 & i < msgs. length )
return msgs [ i ] ,
else
return “Invalid Error Code”
}
}
class ErrMsg {
public static void main (string args[]) {
ErrorMsg err = new ErrorrMsg ();
System. out. println (err. getErrorMsg (2) );
System . out . println (err . getErrorMsg (19));
}
}
lTæplénkmµviFIxagelI)anbgøajdUcxagenH
Disk Full
Invalid Error Code.
Page 19
Industrial Technical Instituted 2015
12>GMBI Recursion
enAkñgú Java ,method mYyGacehAeRbIedayxøÜnva. dMeNIrkarenHehAfa recursion ehIy method mYyEdl
manlkçN³EbbenHehAfa recursive method .
]TahrN_mYyén recursion KWCaKNna factorial énmYycMnYn. factorial éncMnYn N
KWCaplKuNénRKb; cMnYnTaMgGs; cenøaH1nig N . ]TahrN_ factorial 3 KW 1x 2x 3 b¤ 6 . enHbgðajBirebob
factorial mYyEdl GacKNnaedayeRbI recursive method :
Binitü.
Page 20
Industrial Technical Instituted 2015
14 lkçN³ Static
CaFmµta member rbs; class RtUv)aneRbItamry³ object én class mYyb:uEnþvaGaceGay eKbegIátnUv
member mYyEdlGaceRbIxøÜnva)anedaymincaM)ac;bBa¢ak;nUv object eLIy.edIm,I begIátnUv member EbbenH
Page 21
Industrial Technical Instituted 2015
static )aneLIy. tam BitRKb; objects TaMgGs;rbs; class eRbICarYmnUvGBaØatEdlmanlkçN³ static dUcKña.
PaBxusKñarvag method manlkçN³ static nig method Fmµta enaHKW method manlkçN³ static Gac
ehAeRbItamry³eQµaH class rbs;va edaymincaM)ac;man object én class begáIteLIg.kmµviFIxageRkamenH
Ca]TahrN_EdlbgðajBIkarbegáIt method mYymanlkçN³ static :
// Use a static method .
class Static Meth {
static int val = 1024 ; // a static variable
// a static method
static int valDiv2 ( ) {
return val /2 ;
}
}
class SDemo 2 {
public static void main ( String args [ ] ) {
System . out . println ( “val is ” + Static Meth . val ) ;
System.out.println(”Static Meth.valDiv2( ):“+Static Meth.valDiv2( ) ) ;
Static Meth . val = 4 ;
System . out . println ( “ val is “ + Static Meth . val ) ;
System.out.println (“Static Meth.valDiv2( ): “ +Static Meth.valDiv2 ( ) ) ;
}
}
Page 22
Industrial Technical Instituted 2015
Page 23
Industrial Technical Instituted 2015
TMhMén Class mYyenAkñúg Class mYyeTótRtUv)ankMNt;eday Class eRkArbs;va. Class mYy enAkñúg
Class mYyeTótenaHGaccUleRbI members TaMgGs;rYmTaM member rbs; Class EdlmanlkçN³ private
Page 24
Industrial Technical Instituted 2015
eKGackMNt; Class kñúgenAkñúg block mYy. ]TahrN_ eyIgGackMNt; Class kñúg enAkñgú method mYyb¤
enAkñgú tYéndMeMIrdEdl² for dUc)anbGgðajtamkmµvFi IxageRkamenH.
// Define an inner class within a for loop.
class Outer {
int outer _x = 100 ;
void test ( ) {
for (int i = 0 ; i <10 ; i ++ ) {
class Inner {
void display ( ) {
system . out . printlh ( “display: outer_x = “+ outer_x ) ;
}
}
inner inner = new Inner ( ) ;
inner . display ( ) ;
}
}
}
class Inner ClassDemo {
public static void main ( String args [ ] ) {
Outer outer = new Outer ( ) ;
outer. test ( );
}
}
kalNaeKbegáIt class mYyenAkñgú class mYyeTot ehIy class kñgú BMu)anbegáIt object enAkñúg class
eRkAeTenaH eKGaccUleTAeRbI members rbs; class kñúgtamry³ object enAkMritén class kñúgenaH.
]TahrN_ ³ kmµvFi IxageRkambgðajBIkareRbIlkçN³EbbenH.
class A {
in x , y ;
int sum ( ) {
return ( x+y) ;
}
class B {
int z ;
int mysum ( ) {
return ( x+y+z ) ;
}
}
}
class MyAB {
public static void main ( String args [ ] ) {
A ob1 = new A ( ) ;
A.B ob2 = obl.new B ( ) ;
obl.x = 5;
obl.y = 6;
ob2.z =7;
System . out . printlh ( “sum =”+ ob2 . mysum ( ) ) ;
}
}
Page 25
Industrial Technical Instituted 2015
sMNYrniglMhat;
1- GVIeTAehAfa class ?
2- ]bmafaeKman class mYyeQµaH MyClass cUrbgðajBIrebobbegáIt object mYyeQµaH ob.
3- cUrBnül;BPI aBxusKñarvag argument nig)a:ra:EmRt.
4- GVIeTACa constructor ehIyenAeBlNavacab;Rbtibtiþ ?
5- etI constructor Gacman return type Edrb¤eT?
6- etIPaBxusKñarvag class nig object enARtg;Na?
7- GVIeTA garbage collection? ehIyvadMeNIrkaya:gdUcemþc?
8- etI finalize method eRbIsMrab;eFVGI VI?
9- etI this KWCaGVI? ehIyeKeRbIvaenAeBlNa?
10- cUrbegáIt class mYyEdlkñúgenaHman methods BIr. method TImYyGaceGayeKKNnaplbUksV‘Ít BI !
dl; N ehIytMélrbs;vaCacMnYnKt;. rIÉ method TIBIrGaceGayeKKNnaplbUkelxKU BI @ dl; @ N
ehIytMélrbs;vaCacMnYnKt;.
11- cUrbegáIt class A mYyenAkñgú class B eday class nimyY ²man method mYyeQµaH show0
dUcKñaEdlGacbgðajGkSrmYyXøafa “It is in class A.”cMeBaH method kñúg class A nig “It is in class
B.”cMeBaH method enAkñg ú class B. bnÞab;mkRtUvbegáIt main class mYyEdl bgðajBIkarcUleRbI members
TaMgkñúg class A nig kñgú class B.
Page 26