0% found this document useful (1 vote)
3K views

Java Codelab Solutions - Section 2.6

This document presents several coding challenges involving variables, data types, comments, and output statements in Java. Examples are provided for writing statements that display variable values, swap two variables using a temporary variable, and rotate the values of four integer variables. Correct code snippets are given as solutions for tasks like outputting multiple variables on one line and commenting

Uploaded by

thetechboss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views

Java Codelab Solutions - Section 2.6

This document presents several coding challenges involving variables, data types, comments, and output statements in Java. Examples are provided for writing statements that display variable values, swap two variables using a temporary variable, and rotate the values of four integer variables. Correct code snippets are given as solutions for tasks like outputting multiple variables on one line and commenting

Uploaded by

thetechboss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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)
}
}

You might also like