Anda di halaman 1dari 3

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=valueoff.Thus,ifi'svaluewere
25andf'svaluewere12.34,theoutputwouldbe:i=25f=12.34.Remember:inyouroutputyoumustbedisplayingboth
thenameofthevariable(likei)anditsvalue.
System.out.println("i="+i+"f="+f)
20507:Givenanintegervariablecount,writeastatementthatdisplaysthevalueofcountonthescreen.Donotdisplay
anythingelseonthescreenjustthevalueofcount.
System.out.println(count)
20508:Givenafloatingpointvariablefraction,writeastatementthatdisplaysthevalueoffractiononthescreen.Donot
displayanythingelseonthescreenjustthevalueoffraction.
System.out.println(fraction)
20979:Twovariables,numandcosthavebeendeclaredandgivenvalues:numisanintegerandcostisadouble.Writea
singlestatementthatoutputsnumandcosttostandardoutput.Printbothvalues(numfirst,thencost),separatedbya
spaceonasinglelinethatisterminatedwithanewlinecharacter.Donotoutputanythingelse.
System.out.println(num+""+cost+"\n")
20671:Giventwodoublevariables,bestValueandsecondBestValue,writesomecodethatswapstheirvalues.Declareany
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,writesomecodethatswapsthevaluesinIandj.Usetemp
toholdthevalueofiandthenassignjsvaluetoi.Theoriginalvalueofi,whichwassavedintemp,cannowbeassignedto
j.
temp=i
i=j
j=temp
20670:Giventwointvariables,firstPlaceWinnerandsecondPlaceWinner,writesomecodethatswapstheirvalues.Declare
anyadditionalvariablesasnecessary.
inttemp
temp=firstPlaceWinner
firstPlaceWinner=secondPlaceWinner
secondPlaceWinner=temp
20769:Fourintegervariables,pos1,pos2,pos3,pos4havebeendeclaredandinitialized.Writethecodenecessarytoleft
rotatetheirvalues:foreachvariabletogetthevalueofthesuccessivevariable,withpos4gettingpos1svalue.
inttemp
temp=pos1
pos1=pos2
pos2=pos3
pos3=pos4
pos4=temp

PostedbyJavaSolutionsat12:26PM

2comments:
Augustine September5,2015at1:03PM
importjavax.swing.JOptionPane
publicclassWordGa
{
publicstaticvoidmain(String[]args)
{
Stringname
name=JOptionPane.showInputDialog("Enteryourname")
Stringage
age=JOptionPane.showInputDialog("Enteryourage")
Stringcity

city=JOptionPane.showInputDialog("Enterthenameofacity")
Stringcollege
college=JOptionPane.showInputDialog("Enterthenameofacollege")
Stringprofession
profession=JOptionPane.showInputDialog("Enterprofession")
Stringanimal
animal=JOptionPane.showInputDialog("Enteratypeofanimal")
Stringpet
pet=JOptionPane.showInputDialog("Enterapetname")
Stringstr="Thereoncewasapersonnamed"+name+
"\nwholivedinCITY.Attheageof"+age+
","+name+"wenttocollegeat\n"+college+"."+name
+"graduatedandwenttoworkasa\n"+profession+".Then,"+name
+"adopteda(n)"+animal+"\nnamed"+pet+
".Theybothlivedhappilyeverafter!"
JOptionPane.showMessageDialog(null,str)
}
}

Anda mungkin juga menyukai