DOC PREVIEW
UNC-Chapel Hill COMP 401 - COMP 401 STATE

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 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 39 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 39 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 39 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 39 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 39 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 39 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 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1“What if” BMI Calculations With General Purpose Calculator“What if” BMI Calculations With Specialized CalculatorBMI SpreadsheetInstance VariablesState-less vs. State-full ObjectsDeclaring Instance VariablesObject Access to a ClassAccessing Instance Variables via Public MethodsCoding Getter and Setter MethodsFunction vs. ProcedureCoding Getter and Setter MethodsGetter and Setter MethodsPropertiesRead-only and Editable PropertiesPropertiesProperties ClassificationProperties ClassificationCalling Getter and Setter MethodsTracing Method CallsActual TraceProgrammatic Property ManipulationObjects vs. PrimitivesUniitialized Primitive vs. Object VariablesDefault Values for VariablesInvoking Methods on nullOverloadingAmbiguous ContextPrinting Multiple Values on One LinePure vs. Impure FunctionsInconsistent BMI StateFixing Inconsistent BMI StateConsistent BMI StateConstructorConstructorEvery Class has a ConstructorEquivalent Class Created by JavaA Class Can Have Multiple ConstructorsUsing Overloaded ConstructorsCOMP 401STATEInstructor: Prasun Dewan2“WHAT IF” BMI CALCULATIONS WITH GENERAL PURPOSE CALCULATORMust re-enter height each time!3“WHAT IF” BMI CALCULATIONS WITH SPECIALIZED CALCULATORpublic double calculateMyBMI(double weight) { final double MY_HEIGHT = 1.77; return (new ABMICalculator).calculateBMI(weight, MY_HEIGHT);}Must only enter the weightBut the height is hardwired! Must create a separate class for each user!General purpose solution that does not require re-entry of height each time?4Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weightBMI SPREADSHEETState: Data remembered by an object between computations5INSTANCE VARIABLESABMICalculator InstancecalculateBMIParametersParameters Body Body accessesABMISpreadsheet InstancegetBMIInstanceVariablesInstanceVariables Body Body accessesBelong to a single methodLocal variableBelong to all methods of an instanceGlobal variable6STATE-LESS VS. STATE-FULL OBJECTSIdentical Instances ~ car radios with no presetsDifferent Instances ~ car radios with presets7DECLARING INSTANCE VARIABLESpublic class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {return weight/(height*height);} …}Missing CodeNo ParametersInstance Variables8OBJECT ACCESS TO A CLASSpublic class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {return weight/(height*height);} …}ObjectEditorObjectEditorOutside Access:Variables should not be public (like hidden thoughts)But ObjectEditor needs their values9ACCESSING INSTANCE VARIABLES VIA PUBLIC METHODSABMISpreadsheet InstanceweightweightheightheightgetWeight() setWeight() getHeight() setHeight() getBMI()ObjectEditorcallswritesweightcallsreadsheightcallsreads writescallsnew weightnew heightreads10CODING GETTER AND SETTER METHODSABMISpreadsheet InstanceweightweightgetWeight() setWeight()ObjectEditorcallswritesweightcallsreadsnew weight11FUNCTION VS. PROCEDUREprocedure: depositfunction: withdraw12CODING GETTER AND SETTER METHODSABMISpreadsheet InstanceweightweightgetWeight() setWeight()ObjectEditorcallswritesweightcallsreadsnew weightpublic double getWeight(){ return weight;}public void setWeight(double newWeight){ weight = newWeight;}procedure – returns nothingfunction13GETTER AND SETTER METHODSpublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public void setHeight(double newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}procedure – returns nothingfunction14PROPERTIESpublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public void setHeight(double newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}15READ-ONLY AND EDITABLE PROPERTIESpublic class C{}public T getP() { ...}public void setP(T newValue) { ...}Typed, Named Unit of Exported Object StateName PType TName PType TRead-onlyRead-onlyEditableEditableGetter methodSetter methodnewPobtainPViolates Bean conventionBeanBean convention:For humans and tools16PROPERTIESpublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public void setHeight(double newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}HeightWeightBMI17PROPERTIES CLASSIFICATIONpublic class ABMISpreadsheet {double height;public double getHeight() {return height;}public void setHeight(double newHeight) {height = newHeight;}double weight;public double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}public double getBMI() {return weight/(height*height);}}HeightWeightBMIEditableIndependentEditableIndependentRead-onlyDependent18PROPERTIES CLASSIFICATIONpublic class ABMICalculator { public double calculateBMI (double weight, double height) {return weight/ (height * height); }}No Properties19CALLING GETTER AND SETTER METHODSpublic class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}20TRACING METHOD CALLSpublic class ABMISpreadsheet { double height; public double getHeight() { System.out.println(“getHeight Called”); return height; } public void setHeight(double newHeight) { System.out.println(“setHeight 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); }}21ACTUAL TRACEExtra getWeight() call made by the undo-redo mechanism in ObjectEditorLoadChange weight22public class BMISpreadsheetUser { public static void main(String[]


View Full Document

UNC-Chapel Hill COMP 401 - COMP 401 STATE

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download COMP 401 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 COMP 401 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 COMP 401 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?