Slide 1InterfacesMotivation: Two Ways of Doing the BMI SpreadsheetBMI SpreadsheetABMISpreadsheetAnotherBMISpreadsheetsetWeight()setHeight()Methods that ChangegetBMI()Complete CodeGraphical AlgorithmObjectEditor User Interface?ObjectEditor User InterfacesSimilarities in the Two ClassesReal-World AnalogyInterfaceImplementing an InterfaceInterfaceUsing Interfaces to ClassifyUsing Car Specifications to ClassifyUsing Interfaces to TypeCannot Instantiate SpecificationInterface as a Syntactic SpecificationInterface as a Syntactic SpecificationInterface RequiredImpact of Differences in the Two ClassesABMISpreadsheet vs. AnotherBMISpreadsheetTime-Space TradeoffTime-Space TradeoffRelating Interface and Class NamesImproving the StyleRe-using CodeChanging Re-used Code OnceOnly Public Methods in InterfacePrinciple of Least PrivilegeMore Code RepetitionRemoving Code RepetitionLocal vs. Global VariableLocal vs. Global VariablescopeScope of Public ItemsIdentifier ScopeFollowing Least PrivilegeNon-public Instance VariablesMaking Instance Variables PublicHard to ChangeConsistency Constraints ViolatedPreconditions ViolatedEncapsulation PrincipleConstants typically should be publicPrincipleCOMP 401INTERFACESInstructor: Prasun Dewan2INTERFACESDefine contracts between our users and implementersOptional – they may not be usedGood style to use them3MOTIVATION: TWO WAYS OF DOING THE BMI SPREADSHEETABMISpreadsheet is one way to implement the spreadsheet user-interfaceLet us create AnotherBMISpreadsheet to illustrate another wayDifference is in number of variables used4BMI SPREADSHEET5ABMISPREADSHEETABMISpreadsheet InstanceweightweightheightheightgetWeight() setWeight() getHeight() setHeight() getBMI()ObjectEditorcallswritesweightcallsreadsheightcallsreads writescallsnew weightnew heightreads6ANOTHERBMISPREADSHEETAnotherBMISpreadsheet InstanceweightweightheightheightgetWeight() setWeight() getHeight() setHeight() getBMI()ObjectEditorcallswritesweightcallsreadsheightcallsreadswritescallsnew weightnew heightreadsbmibmi7SETWEIGHT()ABMISpreadsheet InstanceweightweightsetWeight()ObjectEditorwritescallsnew weightbmibmipublic void setWeight(double newWeight) {weight = newWeight;bmi = weight / (height*height);}8SETHEIGHT()ABMISpreadsheet InstanceheightheightsetHeight()ObjectEditorwritescallsnew heightbmibmipublic void setHeight(double newHeight) {height = newHeight;bmi = weight / (height*height);}9METHODS THAT CHANGEABMISpreadsheet InstanceweightweightheightheightsetWeight() setHeight() getBMI()ObjectEditorwritescallswritescallsnew weightnew heightreadsbmibmi10GETBMI()ABMISpreadsheet InstancegetBMI()ObjectEditorreadsbmibmipublic double getBMI() {return bmi;}11COMPLETE CODEpublic 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; }}12GRAPHICAL ALGORITHMABMISpreadsheet InstanceweightweightheightheightgetWeight() setWeight() getHeight() setHeight() getBMI()ObjectEditorcallswritesweightcallsreadsheightcallsreadswritescallsnew weightnew heightreadsbmibmi13OBJECTEDITOR 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; }}14OBJECTEDITOR USER INTERFACES15SIMILARITIES IN THE TWO CLASSESpublic 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); }}16REAL-WORLD ANALOGYmanufactured byCorvette SpecificationCorvette Specificationimplementsimplementsmanufactured by17INTERFACE18public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight (double newHeight) { height = newHeight; bmi = weight/(height*height); } …IMPLEMENTING AN INTERFACEContractParameter names never matter to Java19implementsimplementsINTERFACEABMISpreadsheetBMISpreadsheetABMISpreadsheetinstanceABMISpreadsheetinstanceinstance ofAnotherBMISpreadsheetAnotherBMISpreadsheetinstanceAnotherBMISpreadsheetinstanceinstance of20implementsimplementsUSING INTERFACES TO CLASSIFYABMISpreadsheetBMISpreadsheetBMISpreadsheetinstanceBMISpreadsheetinstanceinstance ofAnotherBMISpreadsheetBMISpreadsheetinstanceBMISpreadsheetinstanceinstance of21USING CAR SPECIFICATIONS TO CLASSIFYmanufactured byCorvette SpecificationCorvette Specificationimplementsimplementsmanufactured byCorvetteCorvetteCorvetteCorvetteCorvetteCorvetteCorvetteCorvette22USING INTERFACES TO TYPEpublic 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()); }}Same variable assigned instances of two different classesInterface as type23CANNOT INSTANTIATE SPECIFICATIONCannot order a car from a specificationMust order from factoryA car defined by Corvette specification ordered from factory implementing the specificationCannot instantiate interfaceMust instantiate classBMISpreadsheet instance
View Full Document