Tutorial - Week: 3: IT0206 - Programming in Java LAB
Tutorial - Week: 3: IT0206 - Programming in Java LAB
Part I
Time !ration: " #r$
1. Evaluate the following expression without running the code. For each of the following expressions, indicate the type of the expression and the value returned, or, if the evaluation would lead to an error, write 'None Type' and write the word 'Error' as the value returned. 1. 3 + .! ". #" 3. #" $$ #".! %. #".! . int &".'( '. round &".'( ). *" $$ .! * ".! +. ".! + .!
%o&!tion:
1. ". 3. %. . '. ). +. T'(e , -ou.le / T'(e , 0nteger / T'(e , 1oolean / T'(e , -ou.le / T'(e , 0nteger / T'(e , 2ong / T'(e , 1oolean / T'(e , -ou.le / )a&!e , +.! )a&!e , " )a&!e , False )a&!e , ". )a&!e , " )a&!e , 3 )a&!e , True )a&!e , ).!
%o&!tion:
1. ". 3. %. 6ddition 6ssign7ent 8u.traction 6ssign7ent 9ultiplication 6ssign7ent -ivision 6ssign7ent
3. :onvert the following pseudo codes to ;ava progra7. Try to answer the
<uestions without running the code. :hec= your answers, then run the code for the ones you get wrong.
1. Nu7 $ 0f nu7 > " ?@rint nu7 nu7 4$ 1A print nu7 ". nu7 $ ! while nu7 B$ ? print nu7 Nu7 +$ 1A @rint CDutside of loopC @rint nu7 3. Nu7 $ 1!! 3hile not false ?0f nu7 B !, .rea=A @rint nu7
%o&!tion:
1. ,%,3," ". !,1,",3,%, ,Dutside of 2oop,' 3. Error. %. :onvert the following code to code that uses 1.for ".while 3.do...while and also i7ple7ent the sa7e to print in reverse.
8yste7.out.println &E"F(/ 8yste7.out.println &E%F(/ 8yste7.out.println &E'F(/ 8yste7.out.println &E+F(/ 8yste7.out.println &E1!F(/ 8yste7.out.println &EGood 1yeF(/
%o&!tion: pu.lic class 2oop ? pu.lic static void 7ain &8tringHI loop( ? int i, ;/ for &i $ 1/ i B / i++( ? ; $ i * "/
8yste7.out.println&;(/ A 8yste7.out.println &CGood.yeC(/ i $ 1/ while &i J$ ( ? ; $ i * "/ 8yste7.out.println&;(/ i++/ A 8yste7.out.println&CGood.yeC(/ i $ 1/ do ? ; $ i * "/ 8yste7.out.println&;(/ i++/ A while &i J$ (/ 8yste7.out.println&CGood.yeC(/ A A
. 3rite a ;ava code with while loop that su7s the values 1 through EendF, inclusive. EEndF is a varia.le that we define for you. 8o, for exa7ple, if we define end to .e ', your code should print out the result, "1 which is 1 + " + 3 + % + + '.
%o&!tion: pu.lic class 8u77ing ? pu.lic static void 7ain&8tring su7HI( ? 8yste7.out.println&C@lease enter the end varia.le ,C(/ 8canner s $ new 8canner&8yste7.in(/ int n $ s.next0nt&(/ int i $ 1/ int result $ !/ while &i J$ n + 1( ?
%o&!tion: 1. pu.lic class ForO2oopO1 ? pu.lic static void 7ain&8tring ;HI( ? int i/ for &i $ !/ i B / i++( ? 8yste7.out.println&CPnC + i(/ A 8yste7.out.println&CPnC + &i 4 1((/ A A
". pu.lic class ForO2oopO" ? pu.lic static void 7ain&8tringHI s( ? int d $ "/ for &int i $ !/ i B 1!/ i +$ "( ? int result $ i # d/ 8yste7.out.println&CPnC + result(/ A A A 3. pu.lic class ForO2oopO3 ? pu.lic static void 7ain&8tringHI s( ? for&int i $ !/ i B "!/ i +$ %( ? if&iN%$$!( ? 8yste7.out.println&i(/ A if&iN1'$$!( ? 8yste7.out.println&CFooJC(/ A A A A
Part II
Time !ration: 2 #r*$
1. 6pply an D.;ect oriented paradig7 +#int: ,&a**e*- using ;ava to develop a standalone application without a 7ethod to print CLello 3orldC
%o&!tion: pu.lic class @rint ? pu.lic static void 7ain&8tringHI args( ? 8yste7.out.println&CLello 3orldC(/
A A
". 3rite a si7ple ;ava progra7 to find the greatest of 3 nu7.ers using if...else state7ent.
%o&!tion:
i7port ;ava.util.8canner/ pu.lic class GreatestDfThree ? pu.lic static void 7ain&8tringHI args( ? int a, ., c/ 8canner s $ new 8canner&8yste7.in(/ 8yste7.out.println&CEnter 'a' value,C(/ a $ s.next0nt&(/ 8yste7.out.println&CEnter '.' valueC(/ . $ s.next0nt&(/ 8yste7.out.println&CEnter 'c' valueC(/ c $ s.next0nt&(/ if &&a > .( QQ &a > c(( ? 8yste7.out.println&C6 $ C + a + C is the greatest of the three nu7.ersC(/ A else if &&. > a( QQ &. > c(( ? 8yste7.out.println&C1 $ C + . + C is the greatest of the three nu7.ersC(/ A else if &&c > a( QQ &c > .(( ? 8yste7.out.println&C: $ C + c + C is the greatest of the three nu7.ersC(/ A A A 3. 3rite a si7ple ;ava progra7 to find the given nu7.er is palindro7e or not. E.g. 1"3%, "3% R not palindro7e/ 1"3"1, %%%%% R are palindro7e.
%o&!tion: i7port ;ava.util.8canner/ pu.lic class @alindro7e ? pu.lic static void 7ain&8tringHI args( ? 8canner s $ new 8canner&8yste7.in(/ 8yste7.out.println&CEnter string , C(/ 8tring1uilder s1 $ new 8tring1uilder&s.next2ine&((/ 8tring1uilder s" $ new 8tring1uilder&s1.reverse&((/
%o&!tion: i7port ;avax.swing.SDption@ane/ #** * Nu7.er:onversion is used to convert nu7.ers fro7 one for7at to another Q to verify whether the entered nu7.er is of a specific type. * 0t has the 7ethods nu7.er:onverter Q nu7.erTerifier. * Uauthor 6nagh Vu7ar 1aranwalO1!+1"1!11" * Uversion 1.) * Usince 1.) *# pu.lic class Nu7.er:onversion ? #** * nu7.er:onverter is used to convert a nu7.er fro7 a given for7at to another for7at specified .y the user. 0t has three 8tring argu7ents * whose uses are specified under @ara7eters. * Exa7ple of Msage, * B:D-E>nu7.er:onverter&FF,Lexa-eci7al,Dctal(B#:D-E> * converts FF which is a Lexadeci7al nu7.er to 3)) which is an octal nu7.er Q then prints the output. * 1efore conversion it first chec=s whether the input type Q output type are sa7e and if true then prints the sa7e nu7.er * in a Bcode>36KN0NGO9E886GEB#code> dialog .ox. * Upara7 nu7.er , 8tring as=ed fro7 user which is to .e converted to other types. * Upara7 . , Type of the nu7.er entered. :an .e C1inaryC, CDctalC, C-eci7alC Q CLexa-eci7alC. * Upara7 c , Type of the nu7.er which is to .e o.tained as output. :an .e C1inaryC, CDctalC, C-eci7alC Q CLexa-eci7alC.
* Usee 8tring * Usee 0nteger.parse0nt * Usee 0nteger.toDctal8tring * Usee 0nteger.toLex8tring * Usee 0nteger.to1inary8tring * Usee [email protected] * Uexception Nu7.erFor7atException *# pu.lic void nu7.er:onverter&8tring nu7.er, 8tring ., 8tring c( ? int deci7al $ !/ ## The varia.le deci7al is initialised Q set to !. 8tring octal $ null, hexadeci7al $ null, .inary $ null/ ## The varia.les octal, hexadeci7al Q .inary are initialised Q set to null. switch &.( ? case C1inaryC , deci7al $ 0nteger.parse0nt&nu7.er, "(/ octal $ 0nteger.toDctal8tring&deci7al(/ hexadeci7al $ 0nteger.toLex8tring&deci7al(/ ## :onverts into deci7al, octal Q hexadeci7al for7ats using 0nteger,parse0nt .rea=/ ## Q 0nteger.toDctal&or Lex or 1inary( 8tring.
case CDctalC , deci7al $ 0nteger.parse0nt&nu7.er, +(/ .inary $ 0nteger.to1inary8tring&deci7al(/ hexadeci7al $ 0nteger.toLex8tring&deci7al(/ .rea=/ case C-eci7alC , deci7al $ 0nteger.parse0nt&nu7.er, 1!(/ .inary $ 0nteger.to1inary8tring&deci7al(/ hexadeci7al $ 0nteger.toLex8tring&deci7al(/ octal $ 0nteger.toDctal8tring&deci7al(/ .rea=/ case CLexa-eci7alC , deci7al $ 0nteger.parse0nt&nu7.er, 1'(/ .inary $ 0nteger.to1inary8tring&deci7al(/
octal $ 0nteger.toDctal8tring&deci7al(/ .rea=/ A if &..e<uals&c(( ? [email protected]&null, C:onverted Nu7.er is C + nu7.er + CPnPnThe Dutput type was the C + Csa7e as the 0nput type.C, C8a7e 0nput Q Dutput TypesC, [email protected](/ A else ? switch &c( ? case C1inaryC , [email protected]&null, CThe nu7.er C + nu7.er + C has .een converted fro7 C + . + C to C + c + C and the resulting nu7.er is , C + .inary(/ .rea=/ case CDctalC , [email protected]&null, CThe nu7.er C + nu7.er + C has .een converted fro7 C + . + C to C + c + C and the resulting nu7.er is , C + octal(/ .rea=/ case C-eci7alC , [email protected]&null, CThe nu7.er C + nu7.er + C has .een converted fro7 C + . + C to C + c + C and the resulting nu7.er is , C + deci7al(/ .rea=/ case CLexa-eci7alC , [email protected]&null,
CThe nu7.er C + nu7.er + C has .een converted fro7 C + . + C to C + c + C and the resulting nu7.er is , C + hexadeci7al(/ .rea=/ A A A #** * nu7.erTerifier verifies the nu7.er entered with the input type selected and returns a .oolean value depending on the result. * Mses try4catch .loc=s to identify nu7.er Q type 7is7atch. * Upara7 d , Nu7.er entered .y the user. * Upara7 e , 0nput type selected .y the user. * Usee Exception * Usee 0nteger.parse0nt * Ureturn 6 .oolean value depending upon the result. *# pu.lic .oolean nu7.erTerifier&8tring d, 8tring e( ? .oolean ret $ false/ switch &e( ? case C1inaryC , ? try ? int t $ 0nteger.parse0nt&d, "(/ an integer, using " as radix ## @rints the result.
ret $ true/ ## parsing succeeded, string is valid .inary nu7.er A catch &Nu7.erFor7atException f( ? ret $ false/ A .rea=/ A case CDctalC , ? try ? int t $ 0nteger.parse0nt&d, +(/ an integer, using + as radix
ret $ true/ ## parsing succeeded, string is valid octal nu7.er A catch &Nu7.erFor7atException f( ? ret $ false/ A .rea=/ A case C-eci7alC , ? try ? int t $ 0nteger.parse0nt&d, 1!(/ an integer, using 1! as radix
ret $ true/ ## parsing succeeded, string is valid deci7al nu7.er A catch &Nu7.erFor7atException f( ? ret $ false/ A .rea=/ A case CLexa-eci7alC , ? try ? int t $ 0nteger.parse0nt&d, 1'(/ an integer, using 1' as radix
ret $ true/ ## parsing succeeded, string is valid hex nu7.er A catch &Nu7.erFor7atException f( ? ret $ false/ A .rea=/ A A return &ret(/ A #** * This is the 7ain 7ethod which is used to instantiate the
Nu7.er:onversion class as well as as= the user for his input to pass * it onto the 7ethod re<uired. * depending upon the function re<uired of the class as well as to show output Q exit the progra7. * 0t verifies whether the nu7.er Q inputOtype 7atch and only then proceeds with the rest of the progra7. * Upara7 a , B:ode>8tringB#code> generally used for co77and4line argu7ents. no function here. * Usee SDption@ane * Usee D.;ect * Usee 8tring * Usee 8yste7 * Uexception Nu7.erFor7atException *# pu.lic static void 7ain&8tringHI a( ? D.;ectHI possi.ilities $ ? C1inaryC, CDctalC, C-eci7alC, CLexa-eci7alC A/ 8tring type $ &8tring( [email protected]&null, C8elect the Nu7.er 0nput type , C, C0nput Type -ialogC, [email protected], null, possi.ilities, C-eci7alC(/ 8tring no $ [email protected]&null, C@lease Enter the Nu7.er , C, C0nput -ialogC, [email protected](/ Nu7.er:onversion n1 $ new Nu7.er:onversion&(/ .oolean r $ n1.nu7.erTerifier&no, type(/ if &r $$ true( ? 8tring nextOtype $ &8tring( [email protected]&null, C8elect the Nu7.er Dutput type , C, CDutput Type -ialogC, [email protected], null, possi.ilities, C-eci7alC(/ n1.nu7.er:onverter&no, type, nextOtype(/ 8yste7.exit&!(/ A else if &r $$ false( ? [email protected]& null, CThe nu7.er you entered does not 7atch the entered type. @lease run the progra7 again.C, CNu7.er Q Type 9is7atchC, [email protected](/
8yste7.exit&1(/ A A A
. 3rite a si7ple ;ava progra7 to find the su7, product, difference and <uotient of any " nu7.ers using switch-case state7ent .y getting the choice fro7 the user. -e7onstrate type casting for finding the <uotient.
%o&!tion: i7port ;ava.util.8canner/ pu.lic class 6rith7eticDperations ? static float a, ./ pu.lic void read&( ? 8canner s1 $ new 8canner&8yste7.in(/ 8canner s" $ new 8canner&8yste7.in(/ 8yste7.out.println&CEnter 'a' value,C(/ a $ s1.nextFloat&(/ 8yste7.out.println&CEnter '.' value,C(/ . $ s".nextFloat&(/ A pu.lic void operate&float x, float y( ? int choice/ 8canner s3 $ new 8canner&8yste7.in(/ 8yste7.out.println&C8elect your choice of operation,C(/ 8yste7.out.println&C1.6dditionPn".8u.tractionPn3.9ultiplicationPn%.-i visionC(/ choice $ s3.next0nt&(/ switch &choice( ? case 1 , 8yste7.out.println&C8u7 $ C + &a + .((/ .rea=/ case " , 8yste7.out.println&C-ifference $ C + &a 4 .((/ .rea=/ case 3 , 8yste7.out.println&C@roduct $ C + &a * .((/ .rea=/ case % , 8yste7.out.println&CWuotient $ C + &int( &a # .((/ .rea=/
default , 8yste7.out.println&C0nvalid :hoiceC(/ A A pu.lic static void 7ain&8tringHI args( ? 6rith7eticDperations o. $ new 6rith7eticDperations&(/ o..read&(/ o..operate&a, .(/ A A
'. 3rite a si7ple ;ava progra7 to print the following pattern & for loop( * * * * * * * * %o&!tion: pu.lic class -ia7ond@attern ? pu.lic static void 7ain&8tringHI args( ? int n $ 3/ int i, ;, =/ for &i $ !/ i B n/ i++( ? for &; $ 1/ ; B &n 4 i(/ ;++( ? 8yste7.out.print&C C(/ A if &i $$ !( ? 8yste7.out.print&C*C(/ A else ? 8yste7.out.print&C*C(/ for &= $ 1/ = B$ i/ =++( ? 8yste7.out.print&C C(/ A for &= $ 1/ = B$ i/ =++( ? 8yste7.out.print&C C(/ A 8yste7.out.print&C*C(/ A 8yste7.out.println&C C(/ A n $ "/ for &i $ n 4 1/ i >$ !/ i44( ? for &; $ 1/ ; B &n 4 i + 1(/ ;++( ? 8yste7.out.print&C C(/
A if &i $$ !( ? 8yste7.out.print&C*C(/ A else ? 8yste7.out.print&C*C(/ for &= $ 1/ = B$ i/ =++( ? 8yste7.out.print&C C(/ A for &= $ 1/ = B$ i/ =++( ? 8yste7.out.print&C C(/ A 8yste7.out.print&C*C(/ A 8yste7.out.println&C C(/ A A A ). 3rite a si7ple ;ava progra7 to print the following & while loop( . ./. .0 1 1 1 " % " 3 X ' et,$
%o&!tion: pu.lic class FactorialO8<uare ? pu.lic static void 7ain&8tringHI args( ? 8canner s $ new 8canner&8yste7.in(/ 8yste7.out.println&C@lease enter the nu7.er of ter7s you want ,C(/ int n $ s.next0nt&(/ int i $ 1,fact $ 1, s<r/ 8yste7.out.println&CN N*N NJC(/ while&i B$ n( ? fact *$ i/ s<r $ i*i/ 8yste7.out.println&i+C C+fact+C C+s<r(/ i++/ A A A
+. 3rite a si7ple ;ava progra7 to co7pute the value of e x .y using the for7ula
%o&!tion: pu.lic class Exponential ? pu.lic static void 7ain&8tringHI args( ? 8yste7.out.print&CPnPnEnter the value of x , C(/ 8canner s $ new 8canner&8yste7.in(/ -ou.le x $ -ou.le.parse-ou.le&s.next2ine&((/ 8yste7.out.print&CPnPnEnter the nu7.er of ter7s , C(/ 8canner s1 $ new 8canner&8yste7.in(/ int n $ 0nteger.parse0nt&s1.next2ine&((/ dou.le su7, fact/ su7 $ fact $ 1/ int i/ for &i $ 1/ i B n/ i++( ? fact *$ i/ dou.le ter7 $ 9ath.pow&x, i( # fact/ A su7 +$ ter7/
%o&!tion: ##.rea= with la.el total1$!/ outer1, for&int s$!/ sB1!/ s++( ? for&int ;$!/ ;B1!/ ;++( ? if&;$$'(.rea= outer1/
total1+$;/##!+1+"+3+%+ $1 A A 8yste7.out.println&CTotal is C+ total1+CPnC(/ ##continue total$!/ outer, for&int r$!/ rB"/ r++( ? for&int i$!/ iB / i++( ? if&i>3( continue outer/ total+$i/##!+1+"+3+!+1+"+3 A A 8yste7.out.println&CTotal is C+ total+CPnC(/ A A
1!. Terify all codes of part 0 in syste7.
2ome1or3
Pro4e,t 5!e*tion:
6!e: %#o!& 7e ,om(&ete 7e8ore .e9t 1ee3 T!toria& ,&a**
1. 0n this pro.le7, you'll create a progra7 that guesses a secret nu7.erJ The progra7 wor=s as follows, you &the user( thin=s of an integer .etween ! &inclusive( and 1!! ¬ inclusive(. The co7puter 7a=es guesses, and you give it input R is its guess too high or too low5 Msing 1isection search, the co7puter will guess the user's secret nu7.erJ Lere is a transcript of an exa7ple session,
@lease thin= of a nu7.er .etween ! and 1!!J 0s your secret nu7.er !5 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. l 0s your secret nu7.er ) 5 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. l 0s your secret nu7.er +)5 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. h 0s your secret nu7.er +15
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. l 0s your secret nu7.er +%5 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. h 0s your secret nu7.er +"5 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. l 0s your secret nu7.er +35 Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate 0 guessed correctly. c Ga7e over. [our secret nu7.er was, +3
[our progra7 should use .isection search. 8o thin= carefully what that 7eans. 3hat will the first guess always .e5 Low should you calculate su.se<uent guesses5 Lint, [our initial endpoints should .e ! Q 1!!.
%o&!tion: i7port ;ava.util.8canner/ pu.lic class 8ecretNu7.er ? pu.lic static void 7ain&8tringHI guess( ? 8canner s $ new 8canner&8yste7.in(/ int nu7.er $ !, lower.ound $ !, upper.ound $ 1!!, a $ !, count $ !/ 8yste7.out.println&C@lease thin= of a nu7.er .etween ! Q 1!!.C(/ while &a J$ 3( ? count++/ nu7.er $ &int( &&lower.ound + upper.ound( # "(/ 8yste7.out.println&C0s your secret nu7.er C + nu7.er + C 5C(/ 8yste7.out.println&CPn1. Too LighJPn". Too 2owJPn3. [esJC(/ a $ s.next0nt&(/ if &a $$ 1( ? upper.ound $ nu7.er/
A else if &a $$ "( ? lower.ound $ nu7.er/ A else if &a J$ 3( ? 8yste7.out.println&C0nvalid :hoice.C(/ 8yste7.exit&1(/ A A 8yste7.out.println&C[our 8ecret Nu7.er is C + nu7.er(/ A