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

State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J What if BMI Calculations BMI Spreadsheet State Data Remembered by an Object between computations Instance Variables ABMICalculator Instance ABMISpreadsheet Instance calculateBMI Body accesses Parameters getBMI accesses Body Instance Variables Belong to a single method Belong to all methods of an instance local variable global variable State less Vs State ful Objects Identical Instances car radios without presets Different Instances car radios with presets Declaring Instance Variables Missing Code public class ABMISpreadsheet Instance double height Variables double weight public double getBMI return weight height height No Parameters Outside Access to a Class public class ABMISpreadsheet double height double weight public double getBMI return weight height height outside access ObjectEditor Variables should not be public But ObjectEditor needs their values Accessing Instance Variables Via Public Methods ABMISpreadsheet Instance weight reads writes getWeight setWeight weight calls calls height reads reads writes getHeight setHeight getBMI new Weight height calls ObjectEditor calls new Height Coding the Methods ABMISpreadsheet Instance weight reads writes getWeight setWeight weight calls calls height reads writes getHeight setHeight new Weight height calls ObjectEditor calls new Height Coding the Methods ABMISpreadsheet Instance weight reads writes getWeight setWeight weight calls calls new Weight ObjectEditor Coding Getter and Setter Methods ABMISpreadsheet Instance public double getWeight return weight weight reads writes getWeight public void setWeight double newWeight weight newWeight setWeight function weight calls calls new Weight ObjectEditor procedure returns nothing Getter and Setter Methods public 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 functions procedures return nothing Function Vs Procedure Function Procedure Function Vs Procedure Function Procedure Assignment Statement public void setHeight double newHeight height newHeight setHeight 1 77 variable expression code that yields a value variables memory LHS RHS weight 1 75 weight newHeight 1 77 0 height 1 77 0 weight 0 0 Properties public class ABMISpreadsheet double height public double getHeight return height Height public void setHeight double newHeight height newHeight double weight public double getWeight return weight Weight public void setWeight double newWeight weight newWeight public double getBMI BMI return weight height height Read Only and Editable Properties Typed Named Unit of Exported Object State public class C Bean Type T public T getP public void setP T newValue Violates Bean Conventions Name P Readonly Editable Getter method Setter method Conventions for newP obtainP humans tools Properties Classification public class ABMISpreadsheet double height public double getHeight return height Height public void setHeight double newHeight height newHeight double weight public double getWeight return weight Weight public void setWeight double newWeight weight newWeight public double getBMI BMI return weight height height Read Only Editable Independent Editable Independent Read only Dependent Properties Classification public class ABMICalculator public double calculateBMI double weight double height return weight height height Calling Getter and Setter Methods public 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 Tracing Method Calls public 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 Trace Print Line programmed call System out println setWeight called Target Object Method Name Actual Parameter print statement method invocation call interactive call Actual Trace Printing Weight System out println setWeight called System out println newWeight Overloading Look at the airplane fly Two different words with same name The fly is bothering me Operation Definitions Context of Actual Parameters String double Two different operations with same name public void println String val System out println setWeight called public void println double val System out println newWeight Ambiguous Context Time flies like an arrow Fruit flies like an orange Operation Definitions public void println String val public void println String val System out println setWeight called Printing Multiple Values on One Line System out print setWeight called System out println newWeight Operator Overloading System out println setWeight called newWeight 5 6 Print Vs Cannot use instead of print public void setWeight double newWeight System out print weight weight weight newVal System out println weight weight Variable Declaration Errors public class ABMISpreadsheet height double weight 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 Undefined variable Multiply defined variable Declarations Vs Statement Order Order of variable and method declarations in a class does not matter in Java Order of statements in a method body matters Statements executed sequentially Expressions Vs Statements Expression Piece of code yielding value 5 setWeight called newHeight x x weight height height Statement computer instruction executed autonomously System out println seW eight called return x x bmi weight height height Expression always evaluated as part of some statement Pure Vs Impure Functions ABMICalculator Instance ABMISpreadsheet Instance calculateBMI getWeight Body accesses Body


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