COMP 401 INTERFACES Instructor Prasun Dewan INTERFACES Define contracts between our users and implementers Optional they may not be used Good style to use them 2 MOTIVATION TWO WAYS OF DOING THE BMI SPREADSHEET ABMISpreadsheet is one way to implement the spreadsheet user interface Let us create AnotherBMISpreadsheet to illustrate another way Difference is in number of variables used 3 BMI SPREADSHEET 4 ABMISPREADSHEET 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 5 ANOTHERBMISPREADSHEET AnotherBMISpreadsheet Instance weight reads height writes getWeight calls setWeight calls weight reads reads writes getHeight calls new weight bmi setHeight getBMI calls height new height ObjectEditor 6 SETWEIGHT ABMISpreadsheet Instance weight bmi writes setWeight calls new weight public void setWeight double newWeight weight newWeight bmi weight height height ObjectEditor 7 SETHEIGHT ABMISpreadsheet Instance height bmi writes setHeight public void setHeight double newHeight height newHeight bmi weight height height calls new height ObjectEditor 8 METHODS THAT CHANGE ABMISpreadsheet Instance weight height writes bmi reads writes setWeight setHeight calls getBMI calls new weight new height ObjectEditor 9 GETBMI ABMISpreadsheet Instance bmi reads getBMI public double getBMI return bmi ObjectEditor 10 COMPLETE CODE public class AnotherBMISpreadsheet double height weight bmi public double getHeight return height public void setHeight double newHeight height newHeight bmi weight height height public double getWeight return weight public void setWeight double newWeight weight newWeight bmi weight height height public double getBMI return bmi 11 GRAPHICAL ALGORITHM ABMISpreadsheet Instance weight reads height writes getWeight calls setWeight calls weight reads reads writes getHeight calls new weight bmi setHeight getBMI calls height new height ObjectEditor 12 OBJECTEDITOR USER INTERFACE public class AnotherBMISpreadsheet double height weight bmi public double getHeight return height public void setHeight double newHeight height newHeight bmi weight height height public double getWeight return weight public void setWeight double newWeight weight newWeight bmi weight height height public double getBMI return bmi 13 OBJECTEDITOR USER INTERFACES 14 SIMILARITIES IN THE TWO CLASSES public class AnotherBMISpreadsheet double height weight bmi public double getHeight return height public void setHeight double newHeight height newHeight bmi weight height height public double getWeight return weight public void setWeight double newWeight weight newWeight bmi weight height height public double getBMI return bmi 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 15 REAL WORLD ANALOGY en m le p im ts man ufact ured by Corvette Specification im pl em en ts man ufact ured by 16 INTERFACE 17 IMPLEMENTING AN INTERFACEContract public class AnotherBMISpreadsheet implements BMISpreadsheet double height weight bmi public double getHeight return height Parameter public void setHeight double newHeight names never height newHeight matter to Java bmi weight height height 18 INTERFACE implements ABMISpreadsheet BMISpreadsheet implements AnotherBMISpreadsheet AnotherBMISpreadsheet instance ABMISpreadsheet instance instance of ABMISpreadsheet instance instance of AnotherBMISpreadsheet instance 19 USING INTERFACES TO CLASSIFY implements ABMISpreadsheet BMISpreadsheet implements AnotherBMISpreadsheet BMISpreadsheet instance BMISpreadsheet instance instance of BMISpreadsheet instance instance of BMISpreadsheet instance 20 USING CAR SPECIFICATIONS TO CLASSIFY en m le p im ts man ufact ured by Corvette Corvette Corvette Specification im pl em en ts man ufact ured by Corvette Corvette 21 USING INTERFACES TO TYPE public class BMISpreadsheetUser public static void main String args BMISpreadsheet bmiSpreadsheet new ABMISpreadsheet 1 77 75 System out println bmiSpreadsheet getBMI bmiSpreadsheet new AnotherBMISpreadsheet bmiSpreadsheet setHeight 1 77 bmiSpreadsheet setWeight 75 System out println bmiSpreadsheet getBMI Interface as type Same variable assigned instances of two different classes 22 CANNOT INSTANTIATE SPECIFICATION Cannot order a car from a specification Must order from factory A car defined by Corvette specification ordered from factory implementing the specification Cannot instantiate interface Must instantiate class BMISpreadsheet instance created by instantiating class implementing interface 23 INTERFACE AS A SYNTACTIC SPECIFICATION public class ABMISpreadsheet implements BMISpreadsheet 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 24 INTERFACE AS A SYNTACTIC SPECIFICATION public class ABMISpreadsheet implements BMISpreadsheet 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 13450 Syntactic Contract Bombay Market Index 25 INTERFACE REQUIRED Define interfaces for All classes that are instantiated Some are not Include all public methods 26 IMPACT OF DIFFERENCES IN THE TWO CLASSES public class AnotherBMISpreadsheet double height weight bmi public double getHeight return height public void setHeight double newHeight height newHeight bmi weight height height public double getWeight return weight public void setWeight double newWeight weight newWeight bmi weight height height public double getBMI return bmi 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 27 ABMISPREADSHEET VS ANOTHERBMISPREADSHEET ABMISpreadsheet uses less space variables Getter methods of AnotherBMISpreadhseet are faster Setter
View Full Document
Unlocking...