Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance...
Transcript of Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance...
![Page 1: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/1.jpg)
Lab4‐Variables
![Page 2: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/2.jpg)
Informa2onHiding
• GeneralPrinciple:– Restricttheaccesstovariablesandmethodsasmuchaspossible
• Canlabelinstancevariablesandmethodswith:– private–onlythatclasscansee– public–anyclasscansee
• Standard:– allinstancevariableareprivate– methodsothersusesarepublic– methodsusedonlybythecurrentclassareprivate
![Page 3: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/3.jpg)
GeEersandSeEers
• Sincewehavehiddenourinstancevariablesweneedsomewaytoletothersviewormodifythem
• Makepublicmethodstoprovidethataccess:– geEer:returnsthevalueinthevariable– seEer:changesthevalueinthevariable– namingconven2ons...
![Page 4: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/4.jpg)
ExampleofAccessors
publicclassSimpleClass{
privateintinstanceVariable;
publicintgetInstanceVariable(){ returninstanceVariable;}
publicvoidsetInstanceVariable(intval){ instanceVariable=val;}
}
InstanceVariable
GeEer
SeEer
SimpleClassx=newSimpleClass();
x.setInstanceVariable(32);System.out.println(x.getInstanceVariable());
Use
![Page 5: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/5.jpg)
Whydidwebotherwiththat?
• Thereisoneplacewherethevalueofthevariablechanges– sincevariableisprivate,nocodeinotherclassescanchangeit
– onlytheseEerchangesitinourcode• AsinglebreakpointcatchesANYchangestothevalue
![Page 6: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/6.jpg)
Primi2veTypesforIntegers
• Remember:primi2vemeansthatthecompilerknowshowmuchspacetoallocate
• Thismeansthatthesetypeshavefixedspaceand,therefore,havelimita2onsonthevaluestheycanhold.
![Page 7: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/7.jpg)
AllPrimi2veTypes
Wetypicallyuseintforintegersanddoubleforrealvalueseveniftheyholdmoreinforma2onthanwerequire
![Page 8: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/8.jpg)
IntegersandSignBits
• Everyintegerhasabooleanthatrepresentswhetherthenumberisnega2ve(trueimpliesthenumberisnega2ve)
• Thatbooleanisstoredasa1or0atthehighendoftheinteger.
• Supposewecanholdthreedecimaldigitsinaninteger,then‐999wouldbestoredas1999and999wouldbestoredas0999.
![Page 9: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/9.jpg)
WhatHappensWhenThingsGetTooLarge?
• Supposewecanonlyholdthreedecimaldigits(sowecanstorenumbersfrom‐999to999)
• Whathappensifweadd130to899?
• So,899+130=‐29?• Thisiscalledoverflowandithappenswhenwetrytorepresentanumberlargerthanourintegervariablewillhold
0899+0130‐‐‐‐‐‐‐1029
![Page 10: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/10.jpg)
StoringRealValues
• Realvaluesareessen2allystoredinscien2ficnota2on
• 12.33=0.1233*102• Sothecomputerjusthastostorethetwoparts:1233and2
• Insteadofhavingfirmupperandlowerbounds,realvariabletypesvaryintheprecisiontheycanrepresent
![Page 11: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/11.jpg)
charTypeandUnicode
• ThecomputerstoresEVERYTHINGasanumber
• Whenwestorecharacters(incharvariablesorinStrings),weactuallystoreintegers
• Unicodedefinesthematchingoftheseintegerstocharacters
![Page 12: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/12.jpg)
ASCII(8bitsubsetofunicode)
*TablefromhEp://www.jimprice.com/ascii‐0‐127.gif
![Page 13: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/13.jpg)
Primi2vevs.ReferenceinAssignmentStatements
intfirst;intsecond;
first=32;second=first;
System.out.println("first="+first+"second="+second);
second=14;System.out.println("first="+first+"second="+second);
first
second
32
32‐‐‐14
Output:
first=32second=14
first=32second=32
![Page 14: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/14.jpg)
‐‐‐‐‐6
Now–WithReferenceVariables
SimpleClassfirst;SimpleClasssecond;
first=newSimpleClass();first.setInstanceVariable(32);second=first;
System.out.println(second.getInstanceVariable());
second.setInstanceVariable(‐6);System.out.print(first.getInstanceVariable());
first
second
SimpleClass
instanceVariable 32
Output:
32
‐6
![Page 15: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/15.jpg)
BigIntegers
• AclassthatcomeswithJavathatcanholdintegersthatarearbitrarilylarge– Thinkaboutthethreewaysyoucaninterpret“class”inthatstatement
– classimpliesreference(notprimi2ve)
• Aspartofthelab,youhavetogooutandfindthejavaAPI(Applica2onProgrammingInterface)
![Page 16: Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance variables we need some way to let others view or](https://reader033.fdocuments.us/reader033/viewer/2022041818/5e5c04fc21cec4720161c1e0/html5/thumbnails/16.jpg)
That’sAll!