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