Lab 4 ‐ Variablestbriggs/csc110/Lab4.pdf · Geers and Seers • Since we have hidden our instance...

16
Lab 4 ‐ Variables

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

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

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

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

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

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

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

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

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

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

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

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

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

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

‐‐‐‐‐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

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

That’sAll!