Section2.
6Exercises,ProblemsandProjects
60126:Wehopeyouneverhavetoencounteranythinglikethefollowingexpressioninaprogram,butconsideritfora
momentandindicateitsvalue:(/*5+3*/9+6/*8*//2)//*+4*/10.Answeris6.
60125:Whichofthefollowinglinescontainsavalidcomment?inttwoPi=3.14159?*hodlsthevalueoftwotimespi*/
WhichofthefollowinglinesdoesNOTconsistof(a)valid,thoughboastful,comment(s)?/*Thisisa//*//FirstRate
Program//*//
60127:Whichcommentbelowisthemosthelpful?intvolume//sizeoftrunkincubicfeet
20509:Givenanintegervariableiandafloatingpointvariablef,thathavealreadybeengivenvalues,writeastatement
thatwritesbothoftheirvaluestostandardoutputinthefollowingformat:i=valueofif=[Link],ifi'svaluewere
25andf'svaluewere12.34,theoutputwouldbe:i=25f=[Link]:inyouroutputyoumustbedisplayingboth
thenameofthevariable(likei)anditsvalue.
[Link]("i="+i+"f="+f)
20507:Givenanintegervariablecount,[Link]
anythingelseonthescreenjustthevalueofcount.
[Link](count)
20508:Givenafloatingpointvariablefraction,[Link]
displayanythingelseonthescreenjustthevalueoffraction.
[Link](fraction)
20979:Twovariables,numandcosthavebeendeclaredandgivenvalues:[Link]
[Link](numfirst,thencost),separatedbya
[Link].
[Link](num+""+cost+"\n")
20671:Giventwodoublevariables,bestValueandsecondBestValue,[Link]
additionalvariablesasnecessary.
doubletemp
temp=bestValue
bestValue=secondBestValue
secondBestValue=temp
20668:Giventwointvariables,Iandj,whichhavebeendeclaredandinitialized,andtwootherintvariables,itempandjtemp
whichhavebeendeclared,writesomecodethatswapsthevaluesinIandjbycopyingtheirvaluestoitempandjtemp
respectively,andthencopyingitemandjtemptojandIrespectively.
itemp=i
jtemp=j
i=jtemp
j=itemp
20669:Giventhreealreadydeclaredintvariables,i,jandtemp,[Link]
[Link],whichwassavedintemp,cannowbeassignedto
j.
temp=i
i=j
j=temp
20670:Giventwointvariables,firstPlaceWinnerandsecondPlaceWinner,[Link]
anyadditionalvariablesasnecessary.
inttemp
temp=firstPlaceWinner
firstPlaceWinner=secondPlaceWinner
secondPlaceWinner=temp
20769:Fourintegervariables,pos1,pos2,pos3,[Link]
rotatetheirvalues:foreachvariabletogetthevalueofthesuccessivevariable,withpos4gettingpos1svalue.
inttemp
temp=pos1
pos1=pos2
pos2=pos3
pos3=pos4
pos4=temp
PostedbyJavaSolutionsat12:26PM
2comments:
Augustine September5,2015at1:03PM
[Link]
publicclassWordGa
{
publicstaticvoidmain(String[]args)
{
Stringname
name=[Link]("Enteryourname")
Stringage
age=[Link]("Enteryourage")
Stringcity
city=[Link]("Enterthenameofacity")
Stringcollege
college=[Link]("Enterthenameofacollege")
Stringprofession
profession=[Link]("Enterprofession")
Stringanimal
animal=[Link]("Enteratypeofanimal")
Stringpet
pet=[Link]("Enterapetname")
Stringstr="Thereoncewasapersonnamed"+name+
"\[Link]"+age+
","+name+"wenttocollegeat\n"+college+"."+name
+"graduatedandwenttoworkasa\n"+profession+".Then,"+name
+"adopteda(n)"+animal+"\nnamed"+pet+
".Theybothlivedhappilyeverafter!"
[Link](null,str)
}
}