COMP 401 STATE Instructor Prasun Dewan WHAT IF BMI CALCULATIONS WITH GENERAL PURPOSE CALCULATOR Must re enter height each time 2 WHAT IF BMI CALCULATIONS WITH SPECIALIZED CALCULATOR public double calculateMyBMI double weight final double MY HEIGHT 1 77 return new ABMICalculator calculateBMI weight MY HEIGHT Must only enter the weight But 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 3 BMI SPREADSHEET Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weight State Data remembered by an object between computations 4 INSTANCE VARIABLES ABMICalculator Instance ABMISpreadsheet Instance getBMI calculateBMI Body accesses accesses Parameters Body Instance Variables Belong to a single method Belong to all methods of an instance Local variable Global variable 5 STATE LESS VS STATE FULL OBJECTS Identical Instances car radios with no presets Different Instances car radios with presets 6 DECLARING INSTANCE VARIABLES Instance Variables Missing Code public class ABMISpreadsheet double height double weight public double getBMI return weight height height No Parameters 7 OBJECT ACCESS TO A CLASS public class ABMISpreadsheet double height double weight public double getBMI return weight height height Variables should not be public like hidden thoughts Outside Access ObjectEditor But ObjectEditor needs their values 8 ACCESSING INSTANCE VARIABLES VIA PUBLIC METHODS ABMISpreadsheet Instance weight reads height writes getWeight calls setWeight calls weight reads writes getHeight calls new weight setHeight reads getBMI calls height new height ObjectEditor 9 CODING GETTER AND SETTER METHODS ABMISpreadsheet Instance weight reads writes getWeight calls setWeight calls weight new weight ObjectEditor 10 FUNCTION VS PROCEDURE procedure deposit function withdraw 11 CODING GETTER AND SETTER METHODS ABMISpreadsheet Instance weight reads writes getWeight calls setWeight calls weight new weight public double getWeight return weight public void setWeight double newWeight weight newWeight procedure returns nothing function ObjectEditor 12 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 function procedure returns nothing 13 PROPERTIES 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 14 READ ONLY AND EDITABLE PROPERTIES Typed Named Unit of Exported Object State public class C Name P Bean Type T public T getP Read only Editable public void setP T newValue Getter method Setter method obtainP Violates Bean convention newP Bean convention For humans and tools 15 PROPERTIES public class ABMISpreadsheet double height public double getHeight Height return 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 return weight height height BMI 16 PROPERTIES CLASSIFICATION public class ABMISpreadsheet double height public double getHeight Height return 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 return weight height height BMI Editable Independent Editable Independent Read only Dependent 17 PROPERTIES CLASSIFICATION public class ABMICalculator public double calculateBMI double weight double height return weight height height No Properties 18 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 19 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 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 20 ACTUAL TRACE Load Change weight Extra getWeight call made by the undo redo mechanism in ObjectEditor 21 PROGRAMMATIC PROPERTY MANIPULATION public class BMISpreadsheetUser public static void main String args ABMISpreadsheet bmiSpreadsheet new ABMISpreadsheet double computedBMI bmiSpreadsheet getBMI bmiSpreadsheet setHeight 1 77 bmiSpreadsheet setWeight 75 System out println computedBMI 22 OBJECTS VS PRIMITIVES public class BMISpreadsheetUser public static void main String args ABMISpreadsheet bmiSpreadsheet new ABMISpreadsheet double computedBMI bmiSpreadsheet getBMI bmiSpreadsheet setHeight 1 77 bmiSpreadsheet setWeight 75 System out println computedBMI Primitive Variable Primitive Value Object Variable Object Value 23 UNIITIALIZED PRIMITIVE VS OBJECT VARIABLES public class BMISpreadsheetUser public static void main String args ABMISpreadsheet bmiSpreadsheet double computedBMI bmiSpreadsheet setHeight 1 77 bmiSpreadsheet setWeight 75 System out println computedBMI Uninitialized Primitive Variable Uninitialized Object Variable 24 DEFAULT VALUES FOR VARIABLES Primitive Variables variables memory double computedBMI computedBMI 0 0 double weight weight 0 0 bmiSpreadsheet null Legal double values Object Variables ABMISpreadsheet bmiSpreadsheet Illegal ABMISpreadsheet value 25 INVOKING METHODS ON NULL bmiSpreadsheet getBMI null pointer exception Exception is an unexpected event error Guilty method will be terminated and exception reported Will see other exceptions later 26 OVERLOADING Look at that plane fly Two different words with the same name The fly is bothering me Operation Definitions Context of actual parameters public void println String val String double
View Full Document
Unlocking...