State Instance Variables Procedures Properties Print Statements Println
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 calculate. BMI Body accesses Parameters get. BMI 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 get. BMI() { return weight/(height*height); } … } No Parameters
Outside Access to a Class public class ABMISpreadsheet { double height; . . . double weight; . . . public double get. BMI() { return weight/(height*height); } … } outside access Object. Editor Variables should not be public But Object. Editor needs their values
Accessing Instance Variables Via Public Methods ABMISpreadsheet Instance weight reads writes get. Weight() set. Weight() weight calls height reads writes get. Height() set. Height() new Weight height calls Object. Editor calls get. BMI() new Height
Coding the Methods ABMISpreadsheet Instance weight reads writes get. Weight() set. Weight() weight calls height reads writes get. Height() set. Height() new Weight height calls Object. Editor calls new Height
Coding the Methods ABMISpreadsheet Instance weight reads writes get. Weight() set. Weight() weight calls new Weight Object. Editor
Coding Getter and Setter Methods ABMISpreadsheet Instance public double get. Weight() { return weight; } weight reads writes get. Weight() public void set. Weight(double new. Weight) { weight = new. Weight; } set. Weight() function weight calls new Weight Object. Editor procedure returns nothing
Getter and Setter Methods public class ABMISpreadsheet { double height; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return weight/(height*height); } } functions procedures return nothing
Function Vs Procedure Function Procedure
Function Vs Procedure Function Procedure
Assignment Statement public void set. Height(double new. Height) { height = new. Height; } set. Height(1. 77) <variable> = <expression> code that yields a value variables memory LHS RHS weight 1. 75*weight new. Height 1. 77 0 height 0 1. 77 weight 0. 0
Properties public class ABMISpreadsheet { double height; public double get. Height() { return height; Height } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; Weight } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { 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 get. P() {. . . } public void set. P(T new. Value) {. . . } } Violates Bean Conventions Name: P Readonly Editable Getter method Setter method Conventions for new. P obtain. P • humans • tools
Properties Classification public class ABMISpreadsheet { double height; public double get. Height() { return height; Height } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; Weight } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { BMI return weight/(height*height); } } Read-Only Editable Independent Read-only Dependent
Properties Classification public class ABMICalculator { public double calculate. BMI (double weight, double height) { return weight/(height*height); } }
Calling Getter and Setter Methods public class ABMISpreadsheet { double height; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return weight/(height*height); } }
Tracing Method Calls public class ABMISpreadsheet { double height; public double get. Height() { System. out. println (“get. Height Called”); return height; } public void set. Height(double new. Height) { System. out. println (“set. Weight Called”); height = new. Height; } double weight; public double get. Weight() { System. out. println (“get. Weight Called”); return weight; } public void set. Weight(double new. Weight) { System. out. println (“set. Weight Called”); weight = new. Weight; } public double get. BMI() { System. out. println (“get. BMI Called”); return weight/(height*height);
Actual Trace
Print Line programmed call System. out. println(“set. Weight called”); Target Object Method Name Actual Parameter print statement method invocation/call interactive call
Actual Trace
Printing Weight System. out. println(“set. Weight called”); System. out. println(new. Weight);
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(“set. Weight called”); public void println (double val) {…} System. out. println(new. Weight);
Ambiguous Context Time flies like an arrow. Fruit flies like an orange. Operation Definitions public void println (String val) {…} System. out. println(“set. Weight called”);
Printing Multiple Values on One Line System. out. print(“set. Weight called: ”); System. out. println(new. Weight); Operator Overloading System. out. println("set. Weight called: " + new. Weight); 5+6
Print Vs + Cannot use + instead of print() public void set. Weight(double new. Weight) { System. out. print (“weight = “ + weight); weight = new. Val; System. out. println(“weight = “ + weight); }
Variable Declaration Errors public class ABMISpreadsheet { height; double weight; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { 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 • “set. Weight called” • new. Height • x*x • weight/(height*height) Statement: computer instruction executed autonomously • System. out. println(“se. W 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 calculate. BMI get. Weight Body accesses weight calculate. BMI(77, 1. 77) height 24. 57 77 set. Weight(71). . . calculate. BMI(77, 1. 77) set. Weight(77) get. Weight() 24. 57 get. Weight() 71
- Slides: 33