professor: alvin chao - educ.jmu.edu

10
Professor: Alvin Chao

Upload: others

Post on 25-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Professor:AlvinChao

1. Inthefirsttable,whichnumber(s) dividedby4evaluateto3?Whatissignificantabout thenumber ofanswersyouhavewrittendown?

2. Howdotheanswersinthefirsttablediffer fromthesecondtable?3. Totherightofthesecondtable,roundeachanswertotheclosestinteger.

Howdothevaluescomparetowhatyouseeinthefirsttable?4. Carefullyexplainthedifferencebetweenthenumber formatsinthefirst

andsecondtables.

14./4. Evaluatesto

14./4 Evaluatesto

14 /4. Evaluatesto

14 /4 Evaluatesto

5.Completethetable6.Dividingnumberswithfractionalparts(known asfloating-pointnumbers)givesyoudifferent resultsfromdividing twointegers.Inthepreviousquestion:

a)Whichrowsevaluatetoaninteger?b)Whichrowsevaluatetoafloating-point number?

7.ImagineyouarewritingaJavaprogramthatrequiresdivision.a)Whatmustbetrueabouttheoperands(thenumbersaround theoperators)foryoutogetthemathematicallycorrectanswer?b)Doesitneedtobetrueforbothoperands?

8.Considerwhatyouknowaboutaddition(+).If youaddtwointegersinaJavaexpression,willtheresultalwaysbemathematicallycorrect?Justifyyouranswer.9.Whataboutsubtraction(-)andmultiplication(*)? Ifyousubtractormultiply twointegers,willtheansweralwaysbemathematicallycorrect?Justifyyouranswer.

10.IdentifytheJavakeywordusedinavariabledeclarationtoindicatea)aninteger:b)afloating-point number:

11.Considernumbersofdollarbills,cents,andgrams.Whichoftheseunitsonlymakessenseasaninteger,andwhy?12.Whatwouldyouexpectthefollowingstatementstoprintout?

a)System.out.println(dollars);b)System.out.println(cents);c)System.out.println(grams);

13.Whatdoyouthink thepurpose ofavariabledeclarationis?

14.Consider thestatement:cents=dollars;a)Comparethiscodetolines5–7inModel2.Whatvaluedoyouthinkcentsanddollarswillhaveafterrunning thisstatement?b)Whichsideoftheequalssign (leftorright)wasassignedanewvalue?

15.ExamplesofJavaoperatorsinclude+and- ;theydescribeanoperation tobeexecuted(e.g.,additionorsubtraction).

a)Doyouconsider theequalssigninJavaanoperator(anoperationtobeexecuted)?Ifso,explaintheoperation. Ifnot,explainwhynot.b)Doyouconsidertheequalssigninmathematicsanoperator(anoperation tobeexecuted)?Ifso,explaintheoperation.Ifnot,explainwhynot.

21.Inyourownwords,explainhowyoushould readthe=sign inJava.Forexample,theJava statementx=a+b;shouldbereadas“x_____aplusb.”

• TheJavalanguagedefinesaspecificorderofexecutionformathandotheroperations.Forexample,multiplicationanddivision takeprecedenceoveradditionandsubtraction.Usingparentheses,youcanoverridetheorderofoperations.Thefollowing tablelistssomeJavaoperatorsfromhighestprecedencetolowestprecedence.

Forthefollowingquestions,assumeyouhavethesetwovariables:int x;doubley;Questions(10min)22.Whatoperatorhasthelowestprecedence?WhydoyouthinkJavaisdesignedthatway?23.The+and- operatorsshowuptwiceinthetableofoperatorprecedence.FortheJavaexpressionx=5*-3;explainhowyouknowwhetherthe- operatorisbeingusedasanunaryorbinaryoperatorinthisexpression.y=9/2;24.Basedonyouranswertothepreviousquestion,explainwhythevariableywouldbeassigned4.0(asopposedto4or4.5).25.Rewritetheassignmentforysothatitwouldbesetcorrectlyto4.5.(Hint:you’llneedtorecallwhatyoulearnedaboutdivisioninModel2.)

• Acknowledgements• PartsofthisactivityarebasedonmaterialsdevelopedbyHelenHuandUrik Halliday,modifiedbyChrisMayfieldandNathanSprague,andlicensedunderCCBY-NC4.0International

</end>