Professional Documents
Culture Documents
Question 1
Search for a name
Write a program to accept an array of names and a name and check whether the
name is present in the array. Return the count of occurrence. Use the following
array as input
~Dave, ~Ann, ~George, ~Sam, ~Ted, ~Gag, ~Saj, ~Agati, ~Mary, ~Sam,
~Ayan, ~Dev, ~Kity, ~Meery, ~Smith, ~1ohnson, ~Bill, ~Williams, ~1ones,
~Brown, ~Davis, ~Miller, ~Wilson, ~Moore, ~Taylor, ~Anderson, ~Thomas,
~1ackson]
ANSWER:
import java.util.*;
lmporL [avauLll*
lmporL [avalo*
class arrayofname
publlc sLaLlc vold array(SLrlng name)
lnL lcounL0
SLrlng name1uave Ann Ceorge Sam
1edCagSa[AgaLlMarySamAyanuevklLyMeerySmlLh!ohnson8lllWllllams
!ones8rownuavlsAnnMlllerWllsonMoore1aylorAnderson1homas!ackson
lnL nname1lengLh
SysLemouLprlnLln(n1he 1oLal number of names ln Lhe
SysLem ls +n+n)
SysLemouLprlnLln(?ou Searched for +name+ n)
for(l0lnl++)
lf(name1lequals(name))
SysLemouLprlnLln(1he name ls found aL
poslLlon +(l+1))
counLcounL+1
lf(counL0)
SysLemouLprlnLln(1he name ls noL loundn)
else
SysLemouLprlnLln(n1he name Apeared for +counL+
Llmes)
publlc sLaLlc vold maln(SLrlng args)
Console conSysLemconsole()
SysLemouLprlnL(nLnLer a name Lo Search (Case
senslLlve) )
SLrlng nameconreadLlne()
array(name)
Improve the understandability of the below given code:
import java.util.`;
class problem3
int] numArray new int10];
public static void incrementElements (int] integerArray)
int arraylen integerArray.length;
for (int i 0; i < arraylen; i ++)
System.out.println(integerArrayi]);
]
for (int i 0; i < arraylen; i ++)
integerArrayi] integerArrayi] + 10;
]
for (int i0; i < arraylen; i ++)
System.out.println(integerArrayi]);
]
]
ANSWER:
lmporL [avauLll* //lmporLlng Lhe requlred packages
lmporL [avalo*
class roblem3 //creaLlng a class
publlc sLaLlc vold lncremenL(lnL arr) //deflnlng Lhe lncremenL() meLhod wlLh one
lnLeger parameLer
lnL len arrlengLh // lnlLlallzlng a lnLeger varlable
len wlLh Lhe lengLh le Lhe no of elemenLs ln array
SysLemouLprlnLln(n8efore lncremenL 1he Array was )
for (lnL l 0 l len l ++) //deflnlLlon of for loop Lo prlnL Lhe
currenL values of array
SysLemouLprlnL(arrl+L)
for (lnL l 0 l len l ++) //deflnlLlon of for loop Lo lncremenL Lhe
currenL values of array
arrl arrl + 10 //add 10 Lo each value of array
SysLemouLprlnLln(nnAfLer lncremenL 1he Array ls )
for (lnL l0 l len l ++) //deflnlLlon of for loop Lo prlnL Lhe updaLed values of
array
SysLemouLprlnL(arrl+L)
//meLhod
deflnlLlon ends
publlc sLaLlc vold maln(SLrlng args) //deflnlLlon of maln meLhod sLarLs
lnL ln
Console conSysLemconsole() //creaLlng an obecL of console class
SysLemouLprlnL(nLnLer 1he LengLh of Array )
nlnLegerparselnL(conreadLlne()) //readlng Lhe value for
no of elemenLs from console enLered by user
lnL a new lnLn //lnlLlallzlng Lhe
array
SysLemouLprlnLln(nLnLer Array values )
for(l0lnl++) //deflnlng a for loop Lo readlng
Lhe values from console Lo be sLored ln array
allnLegerparselnL(conreadLlne()) //readlng user lnpuL from console
lncremenL(a) // calllng Lhe meLhod
lncremenL() wlLh Lhe array a as lLs parameLer
// maln
meLhod ends
//class
closes
Question 2
Greatest common divisor
Calculate the greatest common divisor of two positive numbers a and b.
gcd(a,b) is recursively defined as
gcd(a,b) a if a b
gcd(a,b) gcd(a-b, b) if a >b
gcd(a,b) gcd(a, b-a) if b > a
ANSWER:
lmporL [avalang*
lmporL [avamaLh*
lmporL [avalo*
publlc class Ccd
publlc sLaLlc lnL gcdCalc ( lnL a lnL b )
lnL gcd0
lf( a b)
gcd a
else
whlle ( a ! b)
lf( a b )
a a b
else
b b a
gcda
reLurn( gcd )
publlc sLaLlc vold maln( SLrlng args )
Console conSysLemconsole()
SysLemouLprlnL(nLnLer llrsL number )
lnL arg1lnLegerparselnL(conreadLlne())
SysLemouLprlnL(nLnLer Second number )
lnL arg2lnLegerparselnL(conreadLlne())
lnL endCcd gcdCalc( arg1 arg2 )
SysLemouLprlnLln( n1he CCu for lnLegers + arg1 + and + arg2 + ls + endCcd )
Improve the understandability of the below given code:
class Problem1
int] a;
int nElems;
public ArrayBub(int max)
a new intmax];
]
public void insert(int value)
anElems] value;
nElems++;
]
public void Sort()
int out, in;
for(outnElems-1; out>1; out--)
for(in0; in<out; in++)
if( ain] > ain+1] )
swap(in, in+1); ]
public void swap(int one, int two)
long temp aone];
aone] atwo];
atwo] temp;
]
ANSWER:
class Problem1 //creating a new class
sLaLlc lnL a //declarlng a new lnLeger varlable a of array Lype Lo sLore Lhe numbers
sLaLlc lnL nLlems //declarlng an lnLeger varlable Lo hold Lhe value of elemenL no ln Lhe
array
publlc sLaLlc vold Array8ub(lnL max) //deflnlng a new parameLerlsed meLhod Lo lnlLlallze Lhe
array wlLh max as slze of Lhe array
a new lnLmax
publlc sLaLlc vold lnserL(lnL value) //deflnlng Lhe lnserL meLhod Lo lnserL values ln Lhe
array
anLlems value //asslgnlng Lhe value Lo array aL
currenL poslLlon
nLlems++ //lncremenLlng Lhe
poslLlon counLer
publlc sLaLlc vold SorL() //deflnlng Lhe meLhod Lo sorL Lhe array
lnL ouL ln // declarlng Lwo lnLeger
varlables ouL ln
for(ouLnLlems1 ouL1 ouL) //ouLer loop
for(ln0 lnouL ln++) //lnner loop
lf( aln aln+1 ) //condlLlonal sLaLemenL Lo compare Lhe
ad[ecenL values
swap(ln ln+1) //swaplng Lhe Lwo values ln speclfled
order
publlc sLaLlc vold swap(lnL one lnL Lwo) //deflnlng swap funcLlon Lo perform swapplng of
elemenLs
lnL Lemp aone //lnLerchanglng Lhe
values
aone aLwo
aLwo Lemp
publlc sLaLlc vold maln(SLrlng args) //deflnlLlon of maln meLhod
Array8ub(3) //calllng Lhe meLhod arraybub() wlLh 3 as
parameLer
lnserL(3) //calllng Lhe meLhod lnserL() wlLh 3 as
parameLer
lnserL(6) //calllng Lhe meLhod lnserL() wlLh 6 as
parameLer
lnserL(1) //calllng Lhe meLhod lnserL() wlLh 1 as
parameLer
SorL() // calllng Lhe sorL() meLhod
SysLemouLprlnLln(a2) //prlnLlng Lhe currenL value of array aL 3rd poslLlon
swap(21) //calllng Lhe swap funcLlon
SysLemouLprlnLln(a0) //prlnLlng Lhe currenL value of array aL 1sL poslLlon
//maln meLhod ends here
// class deflnlLlon ends