DOC PREVIEW
UNC-Chapel Hill COMP 14 - State

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

StateWhat-if BMI CalculationsBMI SpreadsheetInstance VariablesState-less Vs State-ful ObjectsDeclaring Instance VariablesOutside Access to a ClassAccessing Instance Variables Via Public MethodsCoding the MethodsSlide 10Coding Getter and Setter MethodsGetter and Setter MethodsFunction Vs ProcedureSlide 14Assignment StatementPropertiesRead-Only and Editable PropertiesProperties ClassificationSlide 19Calling Getter and Setter MethodsTracing Method CallsActual TracePrint LineSlide 24Printing WeightOverloadingAmbiguous ContextPrinting Multiple Values on One LinePrint Vs +Variable Declaration ErrorsDeclarations Vs Statement OrderExpressions Vs StatementsPure Vs Impure FunctionsState• Instance Variables• Procedures• Properties• Print Statements• Println Vs Print• Overloading• J++What-if BMI CalculationsBMI SpreadsheetState: Data Remembered by an Object between computationsInstance VariablesABMICalculator InstancecalculateBMIParameters Body accessesABMISpreadsheet InstancegetBMIInstanceVariables Body accessesBelong to a single method Belong to all methods of an instancelocal variable global variableState-less Vs State-ful Objects~ car radios without presets~ car radios with presetsIdentical InstancesDifferent InstancesDeclaring Instance Variablespublic class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {return weight/(height*height);} …}Instance VariablesMissing CodeNo ParametersOutside Access to a Classpublic class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {return weight/(height*height);} …}ObjectEditorVariables should not be publicBut ObjectEditor needs their valuesoutside accessAccessing Instance Variables Via Public MethodsObjectEditorABMISpreadsheet Instanceweight heightgetBMI() readssetWeight()new Weight calls writessetHeight()new Height calls writesheightgetHeight() calls readsgetWeight() readsweight callsCoding the MethodsObjectEditorABMISpreadsheet Instanceweight heightsetWeight()new Weight calls writessetHeight()new Height calls writesheightgetHeight() calls readsgetWeight() readsweight callsCoding the MethodsObjectEditorABMISpreadsheet InstanceweightsetWeight()new Weight calls writesgetWeight() readsweight callsCoding Getter and Setter MethodsObjectEditorABMISpreadsheet InstanceweightsetWeight()new Weight calls writesgetWeight() readsweight callspublic double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}procedure functionreturns nothingGetter and Setter Methodspublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public vo id setHeight(doub le newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public vo id setWeight(d ouble newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}functionsprocedures return nothingFunction Vs ProcedureFunctionProcedureFunction Vs ProcedureFunctionProcedureAssignment Statementpublic void setHeight(double newHeight) {height = newHeight;}newHeight0height0variables memorysetHeight(1.77)1.771.77<expression>LHSRHS<variable> =code that yields a valueweight1.75*weight0.0weightPropertiespublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public vo id setHeight(doub le newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public vo id setWeight(d ouble newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}HeightWeightBMIpublic class C {}Read-Only and Editable Propertiespublic T getP() { ...}Typed, Named Unit of Exported Object StateName: PType: TReadonlypublic vo id setP(T newValue) { ...}Editable newPobtainPViolates Bean ConventionsBeanConventions for•humans•toolsGetter methodSetter methodProperties Classificationpublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public vo id setHeight(doub le newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public vo id setWeight(d ouble newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}HeightWeightBMIEditableEditableRead-onlyIndependentIndependentDependentRead-OnlyProperties Classificationpublic class ABMICalculator { public double calculateBMI (double weight, double height) { ret urn weight/(height*height); }}Calling Getter and Setter Methodspublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public vo id setHeight(doub le newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public vo id setWeight(d ouble newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}Tracing Method Callspublic class ABMISpreadsheet {double height;public double getHeight() {System.out.println (“getHeight Called”);return height;}public void setHeight(double newHeight) {System.out.println (“setWeight Called”);height = newHeight;}double weight;public double getWeight() {System.out.println (“getWeight Called”);return weight;}public void setWeight(double newWeight) {System.out.println (“setWeight Called”);weight = newWeight;}public double getBMI() {System.out.println (“getBMI Called”);return weight/(height*height);}}Actual TracePrint Line System.out.println(“setWeight called”); print statementActual Parametermethod invocation/callMethod Nameinteractive callTarget Objectprogrammed callActual TracePrinting Weight System.out.println(“setWeight called”);System.out.println(newWeight);Look at the airplane fly.The fly is bothering me.OverloadingSystem.out.println(“setWeight called”);System.out.println(newWeight);Context of Actual ParametersTwo different words with same nameTwo different operations with same nameStringdoublepublic vo id println (String val) {…}public vo id println (double val) {…}Operation DefinitionsAmbiguous ContextSystem.out.println(“setWeight called”);Time flies like an arrow.public vo id println (String val) {…}public vo id println (String val) {…}Operation DefinitionsFruit flies like an orange.Printing Multiple Values on One Line System.out.print(“setWeight called: ”); System.out.println(newWeight);System.out.println("setWeight called: " + newWeight);5 + 6Operator OverloadingPrint Vs +public voi d setWeight(double newWeight) {System.out.print (“weight = “ + weight);weight =


View Full Document

UNC-Chapel Hill COMP 14 - State

Download State
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view State and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view State 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?