EXAM 1 CS-1440 JAVA PROGRAMMING
INTRO WITH COMPLETE SOLUTIONS
- nThe ndifference nbetween na nvariable nand na nliteral n- ncorrect nanswers n-A nliteral nis
nnotation nfor nrepresenting na nfixed n( nconst n) nvalue. nA nvariable nis nstorage nlocation
nassociated nwith na nsymbolic nname n(pointed nto, nif nyou'd nlike). nIn nany
nprogramming nlanguage na nLiteral nis na nconstant nvalue, nwhere nas nidentifiers ncan
nchange ntheir nvalues. nIdentifiers ncan nstore nliterals nand nprocess nthem nfurther.
How nto ndeclare na nvariable n- ncorrect nanswers n-To ndeclare n(create) na nvariable,
nyou nwill nspecify nthe ntype, nleave nat nleast none nspace, nthen nthe nname nfor nthe
nvariable nand nend nthe nline nwith na nsemicolon n( n; n). nJava nuses nthe nkeyword nint
nfor ninteger, ndouble nfor na nfloating npoint nnumber n(a ndouble nprecision nnumber),
nand nboolean nfor na nBoolean nvalue n(true nor nfalse).
int nscore;
How nto nassign na nvalue nto na nvariable. n- ncorrect nanswers n-Java nis npass-by-value.
nThat nmeans npass-by-copy
,Declare nan nint nvariable nand nassign nit nthe nvalue n'7'. nThe nbit npattern nfor n7 ngoes
ninto nthe nvariable nnamed nx.
int nx n= n7;
How nto ninitialize na nvariable. n- ncorrect nanswers n-Initializing na nvariable. nInitializing
na nvariable nmeans nspecifying nan ninitial nvalue nto nassign nto nit n(i.e., nbefore nit nis
nused nat nall). nNotice nthat na nvariable nthat nis nnot ninitialized ndoes nnot nhave na
ndefined nvalue, nhence nit ncannot nbe nused nuntil nit nis nassigned nsuch na nvalue.
- nWhat na ndata ntype nis n(i.e., nwhat nmakes nup na ndata ntype) n- ncorrect nanswers n-A
ndata ntype nis na nset nof nvalues nand na nset nof noperations ndefined non nthem. nFor
nexample, nwe nare nfamiliar nwith nnumbers nand nwith noperations ndefined non nthem
nsuch nas naddition nand nmultiplication. nThere nare neight ndifferent nbuilt-in ntypes nof
ndata nin nJava, nmostly ndifferent nkinds nof nnumbers. nWe nuse nthe nsystem ntype nfor
nstrings nof ncharacters nso nfrequently nthat nwe nalso nconsider nit nhere.
The neight nprimitive ndata ntypes, nand nwhich nones nwe nare nusing nin nthis ncourse n-
ncorrect nanswers n-Variables nand nthe n8 nPrimitive nData nTypes
The nPurpose nof na nVariable n(and nsome nvocabulary)
You ncan nthink nof na nsimple nprogram nas na nlist nof ninstructions nto nbe nread nand
nacted nupon nsequentially
Example:
Read na nvalue nrepresenting nthe nradius nof na ncircle nfrom nthe nstandard ninput
nsource/stream
Compute nthe narea nof na ncircle nwith nthis nradius
Print nthe narea nto nthe nstandard noutput nstream n(i.e., nthe nconsole nwindow)
Remember: na ncomputer nwill nread nand nact nupon nthese ninstructions none nat na
ntime n- nit nis nnot naware nof nwhat nis ncoming nup nuntil nit ngets nthere!
Looking nat nstep n1 nin nthe nprogram nabove, nwe nwill nneed nto ntell nthe ncomputer
nthat nit nneeds nto nremember nthe nvalue nit nis nreading nin n- nit nneeds nto nstore nthis
nvalue nin nits nmemory nsomewhere nso nwe ncan nuse nit nin na ncomputation nlater.
We nneed nto ntell nthe ncomputer nhow nmuch nmemory nwill nbe nneeded nto nstore nthe
nvalue nin nquestion. nDifferent nkinds nof nnumbers nrequire ndifferent namounts nof
nmemory n(more non nthis nin na nminute). nOf ncourse, nwe nsometimes nneed nto nstore
nthings nother nthan nnumbers. nThese nthings ntoo, ncome nin ndifferent nsizes. nFor
nexample, nit nwill ncertainly ntake nmore nmemory nto nstore nthe nDeclaration nof
nIndependence nthan nit nwill nto nstore na nsingle nletter n(i.e., na n"character"). nFurther,
nwe nalso nneed nto ntell nthe ncomputer nhow nthe nvalue nshould nbe nstored nin
nmemory n(i.e., nwhat nmethod nof n"encoding" nshould nbe nemployed nto nturn nthe
,nvalue ninto na nstring nof n1's nand n0's). nExamples nof ntypes nof nencodings nused
ninclude nTwo's nComplement, nIEEE n754 nForm, nASCII, nUnicode, netc...
The ncomputer nalso nneeds nto nhave nsome nreference nto nwhere nit nstored nthe
nvalue, nso nit ncan nfind nit nagain.
The nconcept nof na nvariable nsolves nall nof nour nproblems nhere. nA nvariable nin nJava
ngives nus na nway nto nstore nvalues n(or nother nkinds nof ninformation) nfor nlater nuse,
naddressing nall nof nthe naforementioned nconsiderations.
The namount nof nmemory nallocated nfor na ngiven nv
What nthe narithmetic noperators nare, nand nhow nthey nwork nat neach ndata ntype. n-
ncorrect nanswers n-Arithmetic nOperators
The nJava nprogramming nlanguage nsupports nvarious narithmetic noperators nfor nall
nfloating-point nand ninteger nnumbers. nThese noperators nare n+ n(addition), n-
n(subtraction), n* n(multiplication), n/ n(division), nand n% n(modulo). nThe nfollowing ntable
nsummarizes nthe nbinary narithmetic noperations nin nthe nJava nprogramming
nlanguage.
Operator nUse nDescription
+ nop1 n+ nop2 nAdds nop1 nand nop2; nalso nused nto nconcatenate nstrings
- nop1 n- nop2 nSubtracts nop2 nfrom nop1
* nop1 n* nop2 nMultiplies nop1 nby nop2
/ nop1 n/ nop2 nDivides nop1 nby nop2
% nop1 n% nop2 nComputes nthe nremainder nof ndividing nop1 nby nop2
Here's nan nexample nprogram, nArithmeticDemo n(in na n.java nsource nfile), nthat
ndefines ntwo nintegers nand ntwo ndouble-precision nfloating-point nnumbers nand nuses
nthe nfive narithmetic noperators nto nperform ndifferent narithmetic noperations. nThis
nprogram nalso nuses n+ nto nconcatenate nstrings. nThe narithmetic noperations nare
nshown nin nboldface:
public nclass nArithmeticDemo n{
npublic nstatic nvoid nmain(String[] nargs) n{
n//a nfew nnumbers
nint ni n= n37;
nint nj n= n42;
ndouble nx n= n27.475;
ndouble ny n= n7.22;
nSystem.out.println("Variable nvalues...");
nSystem.out.println(" ni n= n" n+ ni);
nSystem.out.println(" nj n= n" n+ nj);
nSystem.out.println(" nx n= n" n+ nx);
nSystem.out.println(" ny n= n" n+ ny);
n//adding nnumbers
, nSystem.out.println("Adding...");
nSystem.out.println(" ni n+ nj n= n" n+ n(i n+ nj));
nSystem.out.println(" nx n+ ny n= n" n+ n(x n+ ny));
n//subtracting nnumbers
nSystem.out.println("Subtracting...");
nSystem.out.println(" ni n- nj n= n" n+ n(i n- nj));
nSystem.out.println(" nx n- ny n= n" n+ n(x n- ny));
n//multiplying nnumbers
nSystem.out.println("Multiplying...");
nSystem.out.println(" ni n* nj n= n" n+ n(i n* nj));
nSystem.out.println(" nx n* ny n= n" n+ n(x n* ny));
n//dividing nnumbers
nSystem.out.println("Dividing...");
nSystem.out.println(" ni n/ nj n= n" n+ n(i n/ nj));
nSystem.out.println(" nx n/ ny n= n" n+ n(x n/ ny));
n//computing nthe nremainder
Understand noperator nprecedence nand nassociativity n- ncorrect nanswers n-
Associativity nis nused nwhen ntwo noperators nof nsame nprecedence nappear nin nan
nexpression. nAssociativity ncan nbe neither nLeft nto nRight nor nRight nto nLeft. nFor
nexample n'*' nand n'/' nhave nsame nprecedence nand ntheir nassociativity nis nLeft nto
nRight, nso nthe nexpression n"100 n/ n10 n* n10" nis ntreated nas n"(100 n/ n10) n* n10".
How nto nuse na ncombined nassignment noperator n- ncorrect nanswers n-Variables nand
nCombined nAssignment nOperators
You'll ncommonly nwant nto ncomplete na nmath nfunction nand nassign nthe nresulting
nvalue nback nto nsome nnamed nobject, ncalled na nvariable. nActionScript nmakes nthis
neasier nby nletting nyou ncombine narithmetic nand nassignment noperators ntogether.
nTake na nlook nat nan nassignment noperator nexample:
Create na nnew nActionScript n3.0 nproject nand nenter nin nthe nfollowing ncode nfor nthe
nproject:
// nAssignment nOperators
var nmyValue:Number n= n2;
myValue n= nmyValue n+ n2;
trace(myValue);
var nmyOtherValue:Number n= n2;
myOtherValue n+= n2;
trace(myOtherValue);
Run nthe nproject. nYou'll nget nthe nfollowing nin nthe nOutput npanel:
4
4
INTRO WITH COMPLETE SOLUTIONS
- nThe ndifference nbetween na nvariable nand na nliteral n- ncorrect nanswers n-A nliteral nis
nnotation nfor nrepresenting na nfixed n( nconst n) nvalue. nA nvariable nis nstorage nlocation
nassociated nwith na nsymbolic nname n(pointed nto, nif nyou'd nlike). nIn nany
nprogramming nlanguage na nLiteral nis na nconstant nvalue, nwhere nas nidentifiers ncan
nchange ntheir nvalues. nIdentifiers ncan nstore nliterals nand nprocess nthem nfurther.
How nto ndeclare na nvariable n- ncorrect nanswers n-To ndeclare n(create) na nvariable,
nyou nwill nspecify nthe ntype, nleave nat nleast none nspace, nthen nthe nname nfor nthe
nvariable nand nend nthe nline nwith na nsemicolon n( n; n). nJava nuses nthe nkeyword nint
nfor ninteger, ndouble nfor na nfloating npoint nnumber n(a ndouble nprecision nnumber),
nand nboolean nfor na nBoolean nvalue n(true nor nfalse).
int nscore;
How nto nassign na nvalue nto na nvariable. n- ncorrect nanswers n-Java nis npass-by-value.
nThat nmeans npass-by-copy
,Declare nan nint nvariable nand nassign nit nthe nvalue n'7'. nThe nbit npattern nfor n7 ngoes
ninto nthe nvariable nnamed nx.
int nx n= n7;
How nto ninitialize na nvariable. n- ncorrect nanswers n-Initializing na nvariable. nInitializing
na nvariable nmeans nspecifying nan ninitial nvalue nto nassign nto nit n(i.e., nbefore nit nis
nused nat nall). nNotice nthat na nvariable nthat nis nnot ninitialized ndoes nnot nhave na
ndefined nvalue, nhence nit ncannot nbe nused nuntil nit nis nassigned nsuch na nvalue.
- nWhat na ndata ntype nis n(i.e., nwhat nmakes nup na ndata ntype) n- ncorrect nanswers n-A
ndata ntype nis na nset nof nvalues nand na nset nof noperations ndefined non nthem. nFor
nexample, nwe nare nfamiliar nwith nnumbers nand nwith noperations ndefined non nthem
nsuch nas naddition nand nmultiplication. nThere nare neight ndifferent nbuilt-in ntypes nof
ndata nin nJava, nmostly ndifferent nkinds nof nnumbers. nWe nuse nthe nsystem ntype nfor
nstrings nof ncharacters nso nfrequently nthat nwe nalso nconsider nit nhere.
The neight nprimitive ndata ntypes, nand nwhich nones nwe nare nusing nin nthis ncourse n-
ncorrect nanswers n-Variables nand nthe n8 nPrimitive nData nTypes
The nPurpose nof na nVariable n(and nsome nvocabulary)
You ncan nthink nof na nsimple nprogram nas na nlist nof ninstructions nto nbe nread nand
nacted nupon nsequentially
Example:
Read na nvalue nrepresenting nthe nradius nof na ncircle nfrom nthe nstandard ninput
nsource/stream
Compute nthe narea nof na ncircle nwith nthis nradius
Print nthe narea nto nthe nstandard noutput nstream n(i.e., nthe nconsole nwindow)
Remember: na ncomputer nwill nread nand nact nupon nthese ninstructions none nat na
ntime n- nit nis nnot naware nof nwhat nis ncoming nup nuntil nit ngets nthere!
Looking nat nstep n1 nin nthe nprogram nabove, nwe nwill nneed nto ntell nthe ncomputer
nthat nit nneeds nto nremember nthe nvalue nit nis nreading nin n- nit nneeds nto nstore nthis
nvalue nin nits nmemory nsomewhere nso nwe ncan nuse nit nin na ncomputation nlater.
We nneed nto ntell nthe ncomputer nhow nmuch nmemory nwill nbe nneeded nto nstore nthe
nvalue nin nquestion. nDifferent nkinds nof nnumbers nrequire ndifferent namounts nof
nmemory n(more non nthis nin na nminute). nOf ncourse, nwe nsometimes nneed nto nstore
nthings nother nthan nnumbers. nThese nthings ntoo, ncome nin ndifferent nsizes. nFor
nexample, nit nwill ncertainly ntake nmore nmemory nto nstore nthe nDeclaration nof
nIndependence nthan nit nwill nto nstore na nsingle nletter n(i.e., na n"character"). nFurther,
nwe nalso nneed nto ntell nthe ncomputer nhow nthe nvalue nshould nbe nstored nin
nmemory n(i.e., nwhat nmethod nof n"encoding" nshould nbe nemployed nto nturn nthe
,nvalue ninto na nstring nof n1's nand n0's). nExamples nof ntypes nof nencodings nused
ninclude nTwo's nComplement, nIEEE n754 nForm, nASCII, nUnicode, netc...
The ncomputer nalso nneeds nto nhave nsome nreference nto nwhere nit nstored nthe
nvalue, nso nit ncan nfind nit nagain.
The nconcept nof na nvariable nsolves nall nof nour nproblems nhere. nA nvariable nin nJava
ngives nus na nway nto nstore nvalues n(or nother nkinds nof ninformation) nfor nlater nuse,
naddressing nall nof nthe naforementioned nconsiderations.
The namount nof nmemory nallocated nfor na ngiven nv
What nthe narithmetic noperators nare, nand nhow nthey nwork nat neach ndata ntype. n-
ncorrect nanswers n-Arithmetic nOperators
The nJava nprogramming nlanguage nsupports nvarious narithmetic noperators nfor nall
nfloating-point nand ninteger nnumbers. nThese noperators nare n+ n(addition), n-
n(subtraction), n* n(multiplication), n/ n(division), nand n% n(modulo). nThe nfollowing ntable
nsummarizes nthe nbinary narithmetic noperations nin nthe nJava nprogramming
nlanguage.
Operator nUse nDescription
+ nop1 n+ nop2 nAdds nop1 nand nop2; nalso nused nto nconcatenate nstrings
- nop1 n- nop2 nSubtracts nop2 nfrom nop1
* nop1 n* nop2 nMultiplies nop1 nby nop2
/ nop1 n/ nop2 nDivides nop1 nby nop2
% nop1 n% nop2 nComputes nthe nremainder nof ndividing nop1 nby nop2
Here's nan nexample nprogram, nArithmeticDemo n(in na n.java nsource nfile), nthat
ndefines ntwo nintegers nand ntwo ndouble-precision nfloating-point nnumbers nand nuses
nthe nfive narithmetic noperators nto nperform ndifferent narithmetic noperations. nThis
nprogram nalso nuses n+ nto nconcatenate nstrings. nThe narithmetic noperations nare
nshown nin nboldface:
public nclass nArithmeticDemo n{
npublic nstatic nvoid nmain(String[] nargs) n{
n//a nfew nnumbers
nint ni n= n37;
nint nj n= n42;
ndouble nx n= n27.475;
ndouble ny n= n7.22;
nSystem.out.println("Variable nvalues...");
nSystem.out.println(" ni n= n" n+ ni);
nSystem.out.println(" nj n= n" n+ nj);
nSystem.out.println(" nx n= n" n+ nx);
nSystem.out.println(" ny n= n" n+ ny);
n//adding nnumbers
, nSystem.out.println("Adding...");
nSystem.out.println(" ni n+ nj n= n" n+ n(i n+ nj));
nSystem.out.println(" nx n+ ny n= n" n+ n(x n+ ny));
n//subtracting nnumbers
nSystem.out.println("Subtracting...");
nSystem.out.println(" ni n- nj n= n" n+ n(i n- nj));
nSystem.out.println(" nx n- ny n= n" n+ n(x n- ny));
n//multiplying nnumbers
nSystem.out.println("Multiplying...");
nSystem.out.println(" ni n* nj n= n" n+ n(i n* nj));
nSystem.out.println(" nx n* ny n= n" n+ n(x n* ny));
n//dividing nnumbers
nSystem.out.println("Dividing...");
nSystem.out.println(" ni n/ nj n= n" n+ n(i n/ nj));
nSystem.out.println(" nx n/ ny n= n" n+ n(x n/ ny));
n//computing nthe nremainder
Understand noperator nprecedence nand nassociativity n- ncorrect nanswers n-
Associativity nis nused nwhen ntwo noperators nof nsame nprecedence nappear nin nan
nexpression. nAssociativity ncan nbe neither nLeft nto nRight nor nRight nto nLeft. nFor
nexample n'*' nand n'/' nhave nsame nprecedence nand ntheir nassociativity nis nLeft nto
nRight, nso nthe nexpression n"100 n/ n10 n* n10" nis ntreated nas n"(100 n/ n10) n* n10".
How nto nuse na ncombined nassignment noperator n- ncorrect nanswers n-Variables nand
nCombined nAssignment nOperators
You'll ncommonly nwant nto ncomplete na nmath nfunction nand nassign nthe nresulting
nvalue nback nto nsome nnamed nobject, ncalled na nvariable. nActionScript nmakes nthis
neasier nby nletting nyou ncombine narithmetic nand nassignment noperators ntogether.
nTake na nlook nat nan nassignment noperator nexample:
Create na nnew nActionScript n3.0 nproject nand nenter nin nthe nfollowing ncode nfor nthe
nproject:
// nAssignment nOperators
var nmyValue:Number n= n2;
myValue n= nmyValue n+ n2;
trace(myValue);
var nmyOtherValue:Number n= n2;
myOtherValue n+= n2;
trace(myOtherValue);
Run nthe nproject. nYou'll nget nthe nfollowing nin nthe nOutput npanel:
4
4