Você está na página 1de 12

Question Paper

Object Oriented Programming and Java (MC221) : January 200


Section A : Basic Concepts (30 Marks)
This section consists of questions with serial
number ! 30"
Answer all questions"
#ach question carries one mark"
Ma$imum time for answerin% Section A is 30
Minutes"
" &hich of the followin% is not correct re%ar'in% a (a)a *ro%ram+
(a) The e$ecution of a ,a)a pro%ram starts with the main () metho'"
(b) Main metho' shoul' ha)e the -public. access specifier
(c) Main metho' ar%uments shoul' be pro)i'e' at e$ecution time
(') Static in'icates that the main metho' is a class metho'
(e) Main metho' nee' not be place' insi'e a class"
/" The compile' )ersion of a ,a)a pro%ram will be in
(a) Machine co'e
(b) B0te co'e
(c) #ncr0pte' co'e
(') 1e$a'ecimal co'e
(e) Assembl0 co'e"
3" &hat are the )alues of a an' b after the followin% loop+
int a 2 03
int b 2 03
for (a203 a40 55 b4/03 a2a6/) 7
a 2 a 6 b3
b 2 b 6 a 6 3
8
(a) a is 0 an' b is 9
(b) a is /: an' b is ;<
(c) a is /: an' b is 9
(') a is 30 an' b is ;<
(e) a is / an' b is 9"
;" Consi'er the followin% instance )ariable 'eclaration:
,a)a"awt"Color newColour3
This 'eclaration
(a) Creates a )ariable calle' newColour with initial )alue 0
(b) Creates a )ariable calle' newColour with an unspecifie' initial )alue
(c) Creates a )ariable calle' newColour with initial )alue null
(') &ill cause a s0nta$ error= as it is not permitte' to 'eclare a )ariable of reference t0pe without creatin%
the associate' ob,ect
(e) Creates a )ariable calle' newColour referrin% to a newl0 create' ,a)a"awt"Color ob,ect which b0 'efault
represents the colour black"
<" The class BankAccount (from lectures) has an instance )ariable calle' balance" >urin% runtime= how man0 copies of the
)ariable calle' balance are create'+ (Assume that no other class has a )ariable calle' balance")
(a) 0
(b)
(c) The number of BankAccount ob,ects create'
(') The number of calls to the metho's 'eposit an' with'raw
(e) ?ne more than the number of BankAccount ob,ects create'"
9" &hich of the followin% is@are !a"se for a constructor in a class+
(a) 1as same name of that of the class
(b) Ase' to initialiBe the instance )ariables
(c) 1as )oi' as return t0pe
(') Can be o)erloa'e'
(e) All the abo)e"
i.exe
C" &hich of the followin% are true about static metho'+
D" Dt can access onl0 static 'ata"
DD" Dt can call static metho's"
DDD" Dt cannot use -this. ke0wor'"
DE" Dt cannot use -super. ke0wor'"
(a) Both (D) an' (DD) abo)e
(b) Both (DD) an' (DDD) abo)e
(c) Both (DD) an' (DE) abo)e
(') Both (D) an' (DE) abo)e
(e) Both (DDD) an' (DE) abo)e"
:" #$ception han'lin% 'oes not use FFFFFFFFFF ke0wor' (clause)"
(a) Tr0
(b) Catch
(c) Ginal
(') Throw
(e) Throws"
H" FFFFFFFFFFFFFFFF metho' is in)oke' = when an ob,ect is concatenate'"
(a) %etMessa%e()
(b) toStrin%()
(c) printStackTrace()
(') setEal()
(e) %etEal()"
0" &hich of the followin% is !a"se+
(a) Class e$ten's class
(b) Class implements interface
(c) Class implements interface = interface/III"
(') Dnterface implements class
(e) Dnterface e$ten's interface"
" &hich of the followin% is not a metho' of Threa' class+
(a) init()
(b) %etJame()
(c) %et*riorit0()
(') sleep()
(e) start()"
/" &hich of the statements is@are #rong+
(a) #ach machine on a network is calle' a no'e
(b) #ach no'e that is full0 functional computer is calle' a host
(c) #)er0 host has an a''ress
(') The a''ress of the host shoul' be e$presse' in numeric form
(e) All of the abo)e"
3" B0 'efault = Threa'"J?KMF*KD?KDTL has )alue of FFFFFFFF"
(a) 0
(b)
(c) <
(') 0
(e) Jull"
;" To restrict the access to onl0 one threa' at a time= simpl0 prece'e metho's.s 'efinition with the ke0wor' FFFFFFFFF"
(a) static
(b) abstract
(c) s0nchroniBe'
(') final
(e) *rotecte'"
<" An applet 'oes not ha)e FFFFFFFFFFFFFmetho'"
(a) init()
(b) stop()
(c) 'estro0()
(') paint()
(e) main()"
9" FFFFFFFFFFFFFFis a metho' to %et )alues from html to applet"
(a) %etEal()
(b) %et*arameter()
(c) %etMessa%e()
(') %etDma%e()
(e) %etApplets()"
C" &hich of the followin% is !a"se+
(a) A class can be abstract an' final
(b) A class can implement an0 number of interfaces
(c) An abstract class cannot be instantiate'
(') Me0wor' -final. before a metho' makes it not to be o)erri''en
(e) Static metho's can access static 'ata onl0"
:" FFFFFis a sub 'irector0 where the compiler an' tools are presentin% the ,'k"
(a) lib
(b) 'ocs
(c) bin
(') inclu'e
(e) 'emo"
H" &hen ?b,ects are passe' to a metho' =it is 'one b0 use of
(a) Call b0 )alue
(b) Call b0 reference
(c) Static o)erloa'in%
(') Kecursion
(e) All of the abo)e"
/0" public class ANuestion
7
pri)ate int i 2 ,3
pri)ate int , 2 03
public static )oi' main(Strin% ar%sOP)
7
S0stem"out"println((new ANuestion())"i)3
8
8
&hat is the out put of abo)e pro%ram+
(a) Compiler error complainin% about forwar' referencin%
(b) Jo error ! The output is 03
(c) Compiler error complainin% about access restriction of pri)ate )ariables of ANuestion
(') Jo error ! The output is 03
(e) Jo error!The output is null"
/" ?ne -tr0. shoul' ha)eFFFFFFFFFFFFF"
(a) ?ne catch
(b) ?ne finall0
(c) Multiple catch.s
(') Catch or finall0
(e) Multiple finall0.s"
//" FFFFFFFFFFFFF is a superclass for all the ob,ects we create"
(a) Class
(b) Dnterface
(c) #$ception
(') *acka%e
(e) ?b,ect"
/3" Dn KMD = stub an' skeleton are %enerate' usin%
(a) ,a)ac
(b) ,a)a
(c) path
(') rmic
(e) rmire%istr0"
/;" Dn (>BC= FFFFFFFFFFFFFF is one of the 'ri)er we use to connect 'atabase"
(a) sun"o'bc",'bc"('bc?'bc>ri)er
(b) sun",'bc"o'bc"('bc?'bc>ri)er
(c) sun:,'bc:o'bc:('bc?'bc>ri)er
(') sun=,'bc=o'bc=('bc?'bc>ri)er
(e) sun",'bc"o'bc",'bco'bc>ri)er"
/<" &hat is the wa0 to take input from the ke0 boar' in ,a)a+
(a) Buffere'Kea'er br2new Buffere'Kea'er((S0stem"in))3
(b) Buffere'Kea'er br2new Buffere'Kea'er(new ?utputStreamKea'er (S0stem"in))3
(c) Buffere'Kea'er br2new Buffere'Kea'er(new DnputStreamKea'er (S0stem"out))3
(') Buffere'Kea'er br2new Buffere'&riter(new DnputStreamKea'er (S0stem"in))3
(e) Buffere'Kea'er br2new Buffere'Kea'er(new DnputStreamKea'er (S0stem"in))3
/9" >SJ stan's for
(a) >ata Source Jame
(b) >ata Ser)er Jetwork
(c) >eco'e Source Jame
(') >ata Ser)ice Jo'e
(e) >umb Ser)er Jetwork"
/C" To write an application usin% swin% FFFFFFFFFFFFF is a packa%e to be importe'"
(a) ,a)a$"ser)let
(b) ,a)a"swin%
(c) ,a)a$"swin%
(') ,a)a"io
(e) ,a)a$"awt"
/:" The sql 'ata t0pe is Dnte%er= FFFFFFFFFFF is the correspon'in% mappe' 'ata t0pe in ,a)a"
(a) num
(b) int
(c) float
(') short
(e) B0te"
/H" To which packa%e 'oes s0stem belon% to+
(a) ,a)a"awt
(b) ,a)a"util
(c) ,a)a"lan%
(') ,a)a$"swin%
(e) ,a)a",'bc"
30" &hich of the followin% si%natures are the same+
D" public )oi' one(int number= float number/)"
DD" public int one(int counter= float counter/)"
DDD" pri)ate )oi' one(int e$ample= float e$ample/)"
(a) Both (D) an' (DD) abo)e
(b) Both (DD) an' (DDD) abo)e
(c) Both (D) an' (DDD) abo)e
(') ?nl0 (D) abo)e
(e) All (D)= (DD) an' (DDD) abo)e"
$%& O' ($C)*O% +
(ection , : Prob"ems (-0 Mar.s)
This section consists of questions with serial number Q < "
Answer all questions"
Marks are in'icate' a%ainst each question"
>etaile' workin%s shoul' form part of 0our answer"
>o not spen' more than 0 ! /0 minutes on Section B"
1/
Create a (a)a ob,ect that simulates a mort%a%e calculator" 1ere is the formula

This ob,ect mort%a%e calculator takes the followin% 'ata as input:
the loan amount
the number of pa0ments
the interest rate
an' returns the monthl0 pa0ment"
&rite a 'ri)er pro%ram for this ob,ect that prompts for an' rea's in the 'ata (loan amount= number of pa0ments=
an' interest rate) an' calls the calculate metho'"
The output to the 'ri)er is as follows (#ither on a file or on S0stem"out):
the loan amount
the number of pa0ments
the interest rate
the monthl0 pa0ments
the total amount pai'
the total amount of interest
Kepeat the process until 0"0 is entere' for the loan amount"
( 10 mar.s)
2/
class #mplo0ee
7
public #mplo0ee()
7
S0stem"out"println( R#mplo0ee constructorS)3
8
8
class 1ourl0#mplo0ee e$ten's #mplo0ee
7
public 1ourl0#mplo0ee()
7
S0stem"out"println( R1ourl0#mplo0ee constructorS)3
8
8
class Test#mplo0ee
7
public Test#mplo0ee()
7
1ourl0#mplo0ee he 2 new 1ourl0#mplo0ee()3
8
public static )oi' main( Strin%OP ar%s )
7
Test#mplo0ee te 2 new Test#mplo0ee()3
8
8
a" &hat is the output of the abo)e co'e+
( 0 mar.s)
b" &hat are the causes of e$ceptions an' what reasons make e$ceptions to cause+
( 1 mar.s)
0/
&rite a (AEA class 2ectang"e that has metho's %etArea () an' %et*erimeter()" A rectan%le is 'efine' b0 a len%th
an' brea'th" Allinstance )ariables shoul' be pri)ate" Lou shoul' suppl0 a sin%le constructor that takes two
parameters= accessor an' mutator metho's= an' a to Strin% metho' for all instance )ariables"
( 10 mar.s)
3/
&rite a (a)a pro%ram to 'emonstrate at least fi)e mouse e)ent han'lers"
( 10 mar.s)
-/
>eclare an interface calle' Gunction that has a metho' name' e)aluate that takes an int parameter an' returns an
int )alue" Create a class 1alf that implements Gunction" Make the implementation of the metho' e)aluate()
return the )alue obtaine' b0 'i)i'in% the int ar%ument b0 /"
( 10 mar.s)
$%& O' ($C)*O% ,
(ection C : +pp"ied )4eory (20 Mar.s)
This section consists of questions with serial number 9 ! C "
Answer all questions"
Marks are in'icate' a%ainst each question"
>o not spen' more than /< !30 minutes on Section C"
5/
a" *arameters that are primiti)e 'ata t0pes are passe' from one metho' to another metho' b0 )alue T e$plain
what this means"
( - mar.s)
b" 1ow is an ob,ect passe' as a parameter to a metho'+ ( - mar.s)
1/
a" &hat are the 'ifferences between instance metho's an' static metho's+ ( 5 mar.s)
b" &rite a metho' that takes two parameters= an arra0 of inte%ers an' a sin%le inte%er= an' returns an arra0
containin% all the )alues in the parameter arra0 that are e$actl0 'i)isible (i"e"= lea)e a remain'er of Bero) b0
the secon' inte%er parameter" The arra0 returne' shoul' not ha)e an0 unuse' elements"
( 3 mar.s)
$%& O' ($C)*O% C
$%& O' Q6$()*O% P+P$2
(uggested +ns#ers
Object Oriented Programming and Java (MC221) : January 200
(ection + : ,asic Concepts
+ns#er 2eason
1/ > Dn ,a)a a Main metho' shoul' be place' insi'e a class
2/ B The compile' )ersion of a ,a)a pro%ram will be in B0te co'e
0/ # The )alues of a an' b after the abo)e loop e$ecute' are a is / an' b is 9"
3/ C Creates a )ariable calle' newColour with initial )alue null"
-/ C As the number of Bank Account ob,ects are create'
5/ C Constructor will not ha)e a return t0pe"
1/ # Static metho' cant use this ke0wor' an' super ke0wor'
/ C #$ceptional han'lin% 'oesnot use final ke0wor'
7/ B toStin%() metho' is in)oke' when an ob,ect is concatenate'"
10/ > Dnterface 'oesnot implement class
11/ A init() is the metho' of applet"
12/ > The a''ress of the host is e$presse' in b0te form"
10/ C Threa'JormF*riorot0 has a )alue of <
13/ C To restrict the access to onl0 one threa' at a time= simpl0 prece'e metho's.s 'efinition with the ke0wor'
s0nchroniBe'"
1-/ # An applet 'oesnot ha)e main() metho'"
15/ B %et*aramter() is a metho' to %et )alues from html to applet"
11/ A it is the contra'ictor0 one"
1/ C bin is a sub 'irector0 where the compiler an' tools are presentin% the ,'k
17/ B when ob,ects are passe' to a metho' it is 'one b0 use of call b0 reference
20/ A Compiler error complainin% about forwar' referencin%
21/ > one tr0 shoul' ha)e catch or finall0"
22/ # ob,ect is a superclass for the ob,ects we create
20/ > in Kmi stub an' skeleton are %enerate' usin% rmic"
23/ B Dn (>BC= sun",'bc"o'bc"('bc?'bc>ri)er is one of the 'ri)er we use to connect 'atabase"
2-/ # Buffere'Kea'er br2new Buffere'Kea'er(new DnputStreamKea'er (S0stem"in))3
is a wa0 to take input from the ke0 boar'"
25/ A >ata source name
21/ C To write an application usin% swin% ,a)a$"swin% is to be importe'"
2/ B Dnt is the correspon'in% mappe' 'ata t0pe in ,a)a"
27/ C S0stem belon%s to ,a)a"lan%" packa%e"
00/ # All are equi)alent"
(ection , : Prob"ems
1/
@@UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
@@ Mort%a%e *a0ment Tables pro%ram
@@ This application is a 'ri)er pro%ram for class Mort%a%eCalculator"
@@ Dt prompts for an' rea's a loan amount= the len%th of the loan in
@@ months= an' an interest rate" Dt prints the loan amount= len%th of
@@ the loan in months= interest rate= monthl0 pa0ments= total cost of
@@ a mort%a%e= an' total amount of interest pai'"
@@ The output )alues are not roun'e' to the nearest 'ollar"
@@ A Bero loan amount stops the processin%"
@@UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
@@ Testin%: There are no precon'itions on the 'ata )alues in class
@@ Mort%a%eCalculator" Je%ati)e or Bero )alues for months an'@or
@@ interest 'o not stop the processin%3 the0 ,ust %i)e meanin%less
@@ output" A Bero loan amount stops the processin%"
@@ Test *lan: Because there are no selection statements in metho'
@@ calculate= one set of reasonable )alues ser)es to check the
@@ metho'Vs correctness a%ainst a known result" Test run:
@@ #nter the followin% )alues all on one line separate' b0 a blank"
@@ loan amount= number of months= interest rate
@@ 00000 390 9"3
@@ Woan amount 00000"0
@@ Jumber of Months 390
@@ Dnterest rate 9"3
@@ Monthl0 pa0ment 9:"HC39:H/9<9:9
@@ Total pai' ///:30"</:3<9;9H:
@@ Total interest pai' //:30"</:3<9;9H:
@@ This answer a%rees with publishe' mort%a%e calculator results for
@@ the same 'ata foun' at
@@ http:@@mort%a%es"interest"com@content@calculators@monthl0!pa0ment"asp
@@UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
import ,a)a"io"U3
import ,a)a"util"Scanner3
public class Mort%a%eTables
7
public static )oi' main(Strin% ar%sOP)
7
@@ Dnput )ariables
float loanAmount3
float 0earl0Dnterest3
int number?fMonths3
'ouble pa0ment3
@@ >eclare an' open output file
Scanner in 2 new Scanner(S0stem"in)3
S0stem"out"println(X#nter the followin% )alues all on one line X
6 Xseparate' b0 a blank" X)3
S0stem"out"println(Xloan amount= number of months= interest rateX)3
loanAmount 2 in"ne$tGloat()3
while (loanAmount Y2 0"0)
7
number?fMonths 2 in"ne$tDnt()3
0earl0Dnterest 2 in"ne$tGloat()3
pa0ment 2 Mort%a%eCalculator"calculate(loanAmount= number?fMonths= 0earl0Dnterest)3
S0stem"out"println(XWoan amount X 6 loanAmount)3
S0stem"out"println(XJumber of Months X 6 number?fMonths)3
S0stem"out"println(XDnterest rate X 6 0earl0Dnterest)3
S0stem"out"println(XMonthl0 pa0ment X 6 pa0ment)3
S0stem"out"println(XTotal pai' X 6 number?fMonthsUpa0ment)3
S0stem"out"println(XTotal interest pai' X 6 (number?fMonthsUpa0ment ! loanAmount))3
S0stem"out"println(X#nter loan amount= number of months= interest rate" X 6
X A ne%ati)e loan amount stops the processin%"X)3
loanAmount 2 in"ne$tGloat()3
8
S0stem"out"println(XSession complete'"X)3
8
8

@@UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
@@ Mort%a%eCalculator",a)a
@@ Create' b0 Jell >ale on :@/;@0<"
@@ Class Mort%a%eCalculator represents a mort%a%e calculator"
@@ Metho' calculate takes a loan amount= the number of pa0ments
@@ (len%t of the loan)= an' the interest rate an' returns the
@@ monthl0 pa0ment"
@@UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
public class Mort%a%eCalculator
7
public Mort%a%eCalculator()
@@ >efault constructor
78
public static 'ouble calculate
( @U in U@ float loanAmount= @@ Woan amount
@U in U@ int number?f*a0ments= @@ Term of loan
@U in U@ float 0earl0Dnterest) @@ Dnterest rate
@@ Calculates the monthl0 pa0ment for a loan amount usin% the
@@ formula for compoun' interest"
@@ *recon'ition:
@@ Ar%uments ha)e been assi%ne' )alues
@@ *ostcon'ition:
@@ pa0ment contains the monthl0 pa0ment as calculate' b0 the
@@ compoun' interest formula
7
@@ local )ariables
float monthl0Kate3
'ouble pa0ment3
monthl0Kate 2 0earl0Dnterest @ /003
pa0ment 2 (loanAmount U Math"pow( 6 monthl0Kate= number?f*a0ments)
U monthl0Kate) @
(Math"pow( 6 monthl0Kate= number?f*a0ments) ! )3
return pa0ment3
8
8
2/ a" Zram[,a)a Test#mplo0ee
#mplo0ee constructor
1ourl0#mplo0ee constructor
b"
" An #$ception is Thrown for ?ne of Three Keasons:
*ossible Causes:
T #)aluation of an e$pression )iolates the rules of the (a)a Wan%ua%e= such as an inte%er 'i)i'e b0 Bero= etc"
T An error occurs in loa'in% or linkin% the (a)a pro%ram"
T Some limitation in a resource is e$cee'e'= such as usin% too much memor0"
/" TA t4ro# statement was e$ecute' in (a)a co'e
*ossible Causes:
The pro%ram e$ecution met an error that ha' been e$pecte'"
3" TAn as0nchronous e$ception occurre'"
*ossible Causes:
TThe metho' stop() of class )4read was in)oke'"
An internal error has occurre' in the )irtual machine"
"TAn abnormal e$ecution con'ition was s0nchronousl0 'etecte' b0 the (a)a Eirtual Machine"
0/ public class Kectan%le 7
pri)ate 'ouble len%th= brea'tht 3 @@ inte%ers ok
public Kectan%le('ouble l= 'ouble b) 7
wi'th 2 w3
hei%ht 2 w3
8
public )oi' setlen%th('ouble l 7 len%th2l3 8
public 'ouble %etlen%th() 7 return len%th3 8
public )oi' setbrea'th ('ouble b) 7 brea'th 2 b3 8
public 'ouble %etbrea'th() 7 return brea'th3 8
public 'ouble %et*erimeter() 7
return / U len%th 6 / U brea'th3
8
public 'ouble %etArea() 7
return len%thUbrea'th3
8
public Strin% toStrin%() 7
@@ an0 strin% that returns the 'ata ok
return RKectan%le: len%th 22S 6 len%th
R= brea'th 22 R 6 brea'th3
8
8
mark for class 'efinition
mark for )ariable 'eclaration
mark for constructor
points for each accessor@mutator (; total)
points for toStrin%
point each for Area@perimeter (/ total)
(can loose one mark for ba' punctuation
3/ import ,a)a"awt"U3
import ,a)a"awt"e)ent"U3
import ,a)a"applet"U3
public class Mouse#)ents e$ten's Applet implements MouseWistener= MouseMotionWistener
7
Strin% ms% 2 RS3
int mouse\ 2 0= mouse0 2 03
public )oi' init()
7
a''MouseWistener(this)3
a''MouseMotionWistener(this)3
8
public )oi' mouseClicke'(Mouse#)ent me)
7
mouse\ 2 03
mouseL 2 03
ms% 2 RMouse Clicke'"S3
repaint()3
8
public )oi' mouse#ntere'(Mouse#)ent me)
7
mouse\ 2 03
mouseL 2 03
ms% 2 RMouse #ntere'S3
repaint()3
8
public )oi' mouse#$ite'(Mouse#)ent me)
7
mouse\ 2 03
mouseL 2 03
ms% 2 RMouse e$ite'S3
repaint()3
8
public )oi' mouse*resse'(Mouse#)ent me)
7
mouse\ 2 me"%et\()3
mouseL 2 me"%etL()3
ms% 2 R'ownS3
repaint()3
8
public )oi' paint(]raphics %)
7
%"'rawStrin%(ms%= mouse\= mouseL)3
8
8
-/ interface Gunction
7
public int e)aluate(int ar%)3
8
class 1alf implements Gunction
7
public int e)aluate(int ar%)
7
return ar%@/3
8
8
public class #$ample
7
public static intOP appl0GunctionToArra0(intOP arrDn)
7
int len%th 2 arrDn"len%th3
intOP arr?ut 2 new intOlen%thP3
Gunction func 2 new 1alf()3
for (int i203i4len%th3i66)
7
arr?utOiP 2 func"e)aluate(arrDnOiP)3
8
return arr?ut3
8
8
(ection C
5/
a" Eariables ma0 be passes from one metho' to another throu%h the ar%ument list= e"%" the )ariable $Ealue ma0 be passe' to a
metho' with the si%nature
public )oi' metho'Two('ouble $Ealue)
b0 a call from another metho'= e"%" metho'?ne= which of course ma0 be the main metho'=
public )oi' metho'?ne()7
" " "
'ouble $Eal 2 <"0>3
metho'Two($Eal)3
" " "
8
Df the )ariable= e"%" $Ealue in the abo)e e$ample= is a primiti)e 'ata t0pe= i"e" 'ouble= float= int= lon%= short= b0te= char=
boolean= the transfer is referre' to as pass b0 )alue" &hat this means is that when the call is ma'e a cop0 is ma'e of the
)ariable bein% transferre'= i"e" in the abo)e e$ample= $Eal is a separate )ariable than $Ealue= which is assi%ne' the same
)alue as $Eal= that is= <"0" Df $Ealue is chan%e' in metho'Two $Eal in metho'?ne will not be chan%e'" Dt will retain its
)alue of <"0"
b" Wets mo'if0 the abo)e e$ample to make the )ariable passe' an ob,ect= e"%"
a calle' metho'
public )oi' metho'Two(Strin% sss/)
an' a callin% metho'
public )oi' metho'?ne()7
" " "
Strin% sss 2 Xabc'X3
metho'Two(sss)3
8
The transfer is now referre' to as pass b0 reference" &hat this means is that when the call is ma'e sss/ is create' as a new
Strin% with the same contents as sss but simpl0 as a reference to sss= i"e" is simpl0 a pointer the the memor0 elements
where sss is store'" This has the consequence that if sss/ is altere' in metho'Two then the same alteration occurs to sss
in the callin% pro%ram as the0 are the same entit0"
To a)oi' this one must pass a cop0 or a clone throu%h the ar%ument list"
1/ a" An instance metho' is one that= which when nee'e'= is attache' to an instance of an ob,ect= e"%" the a''ition of two
comple$ numbers usin% the metho' plus is shown in the followin% se%ment of co'e:
Comple$ com 2 new Comple$("0>= !3"<>)3
Comple$ com/ 2 new Comple$("3;e3= /"9C">)3
Comple$ com3 2 com"plus(com/)3
The metho' plus= in this e$ample a''s the real part of com/ to the real part of the instance to which it is attache'= i"e"
com an' similarl0 a''s the ima%inar0 parts of com an' com/"
A static metho' cannot be attache' to an instance of a metho'" Static metho's %enerall0 contain co'in% that carries out a
function that is %eneral rather than specific to an ob,ect= e"%" a mathematical function such as sin or lo% or the truncation of
a primiti)e 'ata t0pe"
Static metho's are calle' b0 attachin% them to the class name of the class to which the0 belon%=
e"%" Math"sin($)= Math"lo%($)= Gmath"truncate($= 3)"
Dnstance metho's= when written in the class that contains them= are prece'e' b0 either public or pri)ate= usuall0 public=
an' the return t0pe= e"%"
public Comple$ plus(Comple$ $)7
Static metho's= when written in the class that contains them= are prece'e' b0 either public or pri)ate= usuall0 public= the
ke0wor' static an' the return t0pe= e"%"
public static 'ouble truncate('ouble $= int n)
7
b" public static intOP metho'Three(intOP intArra0= int sin%leDnt)7
@@ Gin' len%th of arra0
int arra0Wen%th 2 intArra0"len%th3
@@ number of e$actl0 'i)isible elements
int n>i) 2 03
for(int i203 i4arra0Wen%th3 i66)7
if(intArra0OiP^ sin%leDnt 22 0)7
n>i)663
8
8
@@ Create new arra0 of int
intOP 'i)Arra0 2 new intOn>i)P3
@@ Gill new arra0
int kk 2 03
for(int i203 i4arra0Wen%th3 i66)7
if(intArra0OiP^ sin%leDnt 22 0)7
'i)Arra0OkkP 2 intArra0OiP3
kk663
8
8
return 'i)Arra03
8

Você também pode gostar