D286: JAVA FUNDAMENTALS
Main()m-manswers--
Wheremamprogrammbeginsmexecutingmstatementsmcontainedmwithinmbracesm{}.
System.out.print()m-manswers--
Outputsmanmitemmonmthemscreen,mstartingmatmthemscreenmcursor'smpresentmlocation,m
andmmovingmthemcursormaftermeachmoutputmitem.
System.out.println()m-manswers--
Printsmanmitemmandmthenmmovesmthemcursormtomthemnextmlinem(ie:mnewline).
importmjava.util.Scanner;m-manswers--
Specificmimportmcodemplacedmatmthemtopmofmamfilemtomenablemthemprogrammtomgetmin
put
Scannermscnrm=mnewmScanner(System.in);m-manswers--
Themstatementmthatminstantiatesmamnewmscannermobjectminmthemvariablem"scnr"msomsy
stemminputmcanmbemparsed.
scnr.nextInt();m-manswers--
Themstatementmthatmretrievesmthemsystem'smnextmintegermvalueminput.m
Itmismthemvariablem"scnr"mcallingmthismscannermclassmmethodmtomreadmintegermvaluesm
fromminputmmadempossiblemafterm"scnr"misminstantiatedmwithmamnewmscannermclassmo
bject.
"desiredmtext"m-manswers--Syntaxmformstringmliterals,mwhichmismtextminmdoublemquotes
System.out.println("Wagemis:m"m+mwage);m-manswers--
Thismexamplemshowsmhowmthem"+"msymbolmismusedmtomoutputmmultiplemitemsmusingm
onemoutputmstatement.
\nm-manswers--
Thismismthemsyntaxmformthemnewlinemcharacter,manmalternativemtom"println",mthoughmno
tmusedmoftenmformpracticalmpurposes.
//mThismismamcommentm-manswers--Thismismthemsyntaxmformwritingmamsingle-
linemcommentmthatmcommonlymappearsmaftermamstatementmonmthemsamemline.
/**
,Thismismamcomment
**/m-manswers--Thismismthemsyntaxmformwritingmammulti-
linemcomment,malsomcalledmamblockmcomment.
Whitespacem-manswers--
Thismismblankmspacem(spacemandmtabmcharacters)mbetweenmitemsmwithinmamstatemen
tmandmblankmlinesmbetweenmstatementsm(calledmnewlines).
Amcompilermignoresmmostmofmthismandmismusedmtommakemamprogrammmoremreadable.
intmnumValue;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmanmintegermvariablemwithmthemidentifierm"nu
mValue".mInmgeneral,manymidentifiermthatmismnotmamreservedmwordm(ie:mkeyword)mcan
mbemusedmtomnamemamvariable.mNotmhowmthismexamplemismusingmlowerCamelCase.
numValuem=m8;m-manswers--
Thismismanmexamplemsyntaxmformanmassignmentmstatement,mwhichmassignsmanmintege
rmliteralmtomamdeclaredmvariable.
intmnumValuem=m100;m-manswers--
Thismismanmexamplemsyntaxmforminitializingmanmintegermvariable,mwhichmassignsmanmin
tegermliteralm(ie:minitialmvalue)matmthemsamemtimemthemvariablemismdeclared.
+,m-,m*,m/m-manswers--
Syntaxmformthemaddition,msubtraction,mmultiplication,mandmdivisionmarithmeticmoperator
s,mrespectively.
(),munarym-,m*m/m%,m+m-m-manswers--
Thesemfourmoperatormcategoriesmaremlistedmfirstmtomlastmaccordingmtomprecedencemrul
esmformarithmeticmoperators.
+=,m-=,m*=,m/=,m%=m-manswers--
Thesemaremthemfivemspecialmoperatorsmknownmasmcompoundmoperators,mwhichmallmpr
ovidemamshorthandmwaymtomupdatemamvariable
doublemmilesTravel;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmamvariablemthatmcanmstoremamfloating-
pointmnumber.
scnr.nextDouble();m-manswers--Amscannermmethodmthatmreadsmamfloating-
pointmvaluemfromminput,mwhichmcanmbemassignedmtomamdoublemvariable.
Infinity,m-Infinitym-manswers--
Javamoutputsmthesemwhenmamusermdividesmamnonzeromfloating-
pointmnumbermbymzero.
, Notmamnumberm(NaN)m-manswers--Ifmthemdividendmandmdivisorminmfloating-
pointmdivisionmarembothm0,mthemdivisionmresultsminmthismoutput.
System.out.printf("%.2f",mmyFloat);m-manswers--
Thismexamplemshowsmthemsyntaxmformcontrollingmthemnumbermofmdecimalmplacesmwhe
nmprintingmamfloating-
pointmnumber.mInmthismcase,m'printf'mformfloat,m'.2'mmeansmtomtwomdecimalmplaces,man
dm'myFloat'mismanmexamplemvariablemwheremthemdecimalmplacemmanipulationmismappli
ed
Math.PIm-manswers--
Javamprovidesmthismtomholdmthemfirstm15mdigitsmofmthemirrationalmmathematicalmconsta
ntmpim(π).
6.02e23m-manswers--Thismismanmexamplemsyntaxmofmamfloating-
pointmliteralmusingmscientificmnotationmwheremthememprecedesmthempower-of-
10mexponent.
finalmdoublemSPEED_OF_SOUNDm=m761.207;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmamconstantmvariablem(ie:mfinalmvariable).mN
otemthemusemofm'final'mtompreventmmodificationmandmthemusemofmuppermcasesm&munde
rscoresmformidentifiability.
Math.m-manswers--
ThemsyntaxmforminvokingmthemMathmclass,mamJavamstandardmpackagemthatmallowsmab
outm30mmathmmethodsmtombemcalled.
sqrt(x),mpow(x,my),mabs(x)m-manswers--
Themsyntaxmofmthreemcommonmmathmmethodsmtominvokemsquaremroot,mpower,mandma
bsolutemvaluemformamgivenmvalue,mrespectively.mThesemmustmbemappendedmtom'Math.'
mtomwork.
5m/m2m=m2mvsm5.0m/m2.0m=m2.5m-manswers--
Thesemtwomdivisionsmshowmhowmthemresultmdependsmonmwhethermormnotmtheminvolve
dmvaluesmaremintegersmormfloating-point.mIntegermdivisionmneglectsmanymremainder.
randNumm%m10m-manswers--
Thismexamplemyieldsmamrandommnumbermbetweenm0mandm9,mmadempossiblembymthem
modulomoperator.mNotemthatm0mismpossible,mnotm10.
(randNumm%m11)m+m20m-manswers--
Thismexamplemyieldsmamrandommnumbermbetweenm20mandm30,mmadempossiblembymth
emmodulomoperatormandmthenmaddingm20mtomthemresult.
0.504m*m316m=m159.264m-manswers--
Thismexamplemshowsmhowmcalculatingmanymintegermwithmamfloating-
Main()m-manswers--
Wheremamprogrammbeginsmexecutingmstatementsmcontainedmwithinmbracesm{}.
System.out.print()m-manswers--
Outputsmanmitemmonmthemscreen,mstartingmatmthemscreenmcursor'smpresentmlocation,m
andmmovingmthemcursormaftermeachmoutputmitem.
System.out.println()m-manswers--
Printsmanmitemmandmthenmmovesmthemcursormtomthemnextmlinem(ie:mnewline).
importmjava.util.Scanner;m-manswers--
Specificmimportmcodemplacedmatmthemtopmofmamfilemtomenablemthemprogrammtomgetmin
put
Scannermscnrm=mnewmScanner(System.in);m-manswers--
Themstatementmthatminstantiatesmamnewmscannermobjectminmthemvariablem"scnr"msomsy
stemminputmcanmbemparsed.
scnr.nextInt();m-manswers--
Themstatementmthatmretrievesmthemsystem'smnextmintegermvalueminput.m
Itmismthemvariablem"scnr"mcallingmthismscannermclassmmethodmtomreadmintegermvaluesm
fromminputmmadempossiblemafterm"scnr"misminstantiatedmwithmamnewmscannermclassmo
bject.
"desiredmtext"m-manswers--Syntaxmformstringmliterals,mwhichmismtextminmdoublemquotes
System.out.println("Wagemis:m"m+mwage);m-manswers--
Thismexamplemshowsmhowmthem"+"msymbolmismusedmtomoutputmmultiplemitemsmusingm
onemoutputmstatement.
\nm-manswers--
Thismismthemsyntaxmformthemnewlinemcharacter,manmalternativemtom"println",mthoughmno
tmusedmoftenmformpracticalmpurposes.
//mThismismamcommentm-manswers--Thismismthemsyntaxmformwritingmamsingle-
linemcommentmthatmcommonlymappearsmaftermamstatementmonmthemsamemline.
/**
,Thismismamcomment
**/m-manswers--Thismismthemsyntaxmformwritingmammulti-
linemcomment,malsomcalledmamblockmcomment.
Whitespacem-manswers--
Thismismblankmspacem(spacemandmtabmcharacters)mbetweenmitemsmwithinmamstatemen
tmandmblankmlinesmbetweenmstatementsm(calledmnewlines).
Amcompilermignoresmmostmofmthismandmismusedmtommakemamprogrammmoremreadable.
intmnumValue;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmanmintegermvariablemwithmthemidentifierm"nu
mValue".mInmgeneral,manymidentifiermthatmismnotmamreservedmwordm(ie:mkeyword)mcan
mbemusedmtomnamemamvariable.mNotmhowmthismexamplemismusingmlowerCamelCase.
numValuem=m8;m-manswers--
Thismismanmexamplemsyntaxmformanmassignmentmstatement,mwhichmassignsmanmintege
rmliteralmtomamdeclaredmvariable.
intmnumValuem=m100;m-manswers--
Thismismanmexamplemsyntaxmforminitializingmanmintegermvariable,mwhichmassignsmanmin
tegermliteralm(ie:minitialmvalue)matmthemsamemtimemthemvariablemismdeclared.
+,m-,m*,m/m-manswers--
Syntaxmformthemaddition,msubtraction,mmultiplication,mandmdivisionmarithmeticmoperator
s,mrespectively.
(),munarym-,m*m/m%,m+m-m-manswers--
Thesemfourmoperatormcategoriesmaremlistedmfirstmtomlastmaccordingmtomprecedencemrul
esmformarithmeticmoperators.
+=,m-=,m*=,m/=,m%=m-manswers--
Thesemaremthemfivemspecialmoperatorsmknownmasmcompoundmoperators,mwhichmallmpr
ovidemamshorthandmwaymtomupdatemamvariable
doublemmilesTravel;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmamvariablemthatmcanmstoremamfloating-
pointmnumber.
scnr.nextDouble();m-manswers--Amscannermmethodmthatmreadsmamfloating-
pointmvaluemfromminput,mwhichmcanmbemassignedmtomamdoublemvariable.
Infinity,m-Infinitym-manswers--
Javamoutputsmthesemwhenmamusermdividesmamnonzeromfloating-
pointmnumbermbymzero.
, Notmamnumberm(NaN)m-manswers--Ifmthemdividendmandmdivisorminmfloating-
pointmdivisionmarembothm0,mthemdivisionmresultsminmthismoutput.
System.out.printf("%.2f",mmyFloat);m-manswers--
Thismexamplemshowsmthemsyntaxmformcontrollingmthemnumbermofmdecimalmplacesmwhe
nmprintingmamfloating-
pointmnumber.mInmthismcase,m'printf'mformfloat,m'.2'mmeansmtomtwomdecimalmplaces,man
dm'myFloat'mismanmexamplemvariablemwheremthemdecimalmplacemmanipulationmismappli
ed
Math.PIm-manswers--
Javamprovidesmthismtomholdmthemfirstm15mdigitsmofmthemirrationalmmathematicalmconsta
ntmpim(π).
6.02e23m-manswers--Thismismanmexamplemsyntaxmofmamfloating-
pointmliteralmusingmscientificmnotationmwheremthememprecedesmthempower-of-
10mexponent.
finalmdoublemSPEED_OF_SOUNDm=m761.207;m-manswers--
Thismismanmexamplemsyntaxmformdeclaringmamconstantmvariablem(ie:mfinalmvariable).mN
otemthemusemofm'final'mtompreventmmodificationmandmthemusemofmuppermcasesm&munde
rscoresmformidentifiability.
Math.m-manswers--
ThemsyntaxmforminvokingmthemMathmclass,mamJavamstandardmpackagemthatmallowsmab
outm30mmathmmethodsmtombemcalled.
sqrt(x),mpow(x,my),mabs(x)m-manswers--
Themsyntaxmofmthreemcommonmmathmmethodsmtominvokemsquaremroot,mpower,mandma
bsolutemvaluemformamgivenmvalue,mrespectively.mThesemmustmbemappendedmtom'Math.'
mtomwork.
5m/m2m=m2mvsm5.0m/m2.0m=m2.5m-manswers--
Thesemtwomdivisionsmshowmhowmthemresultmdependsmonmwhethermormnotmtheminvolve
dmvaluesmaremintegersmormfloating-point.mIntegermdivisionmneglectsmanymremainder.
randNumm%m10m-manswers--
Thismexamplemyieldsmamrandommnumbermbetweenm0mandm9,mmadempossiblembymthem
modulomoperator.mNotemthatm0mismpossible,mnotm10.
(randNumm%m11)m+m20m-manswers--
Thismexamplemyieldsmamrandommnumbermbetweenm20mandm30,mmadempossiblembymth
emmodulomoperatormandmthenmaddingm20mtomthemresult.
0.504m*m316m=m159.264m-manswers--
Thismexamplemshowsmhowmcalculatingmanymintegermwithmamfloating-