DOC PREVIEW
UNC-Chapel Hill COMP 401 - COMP 401- INTERFACES

This preview shows page 1-2-3-25-26-27-28-50-51-52 out of 52 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 Dewan2INTERFACESDefine contracts between our users and implementersOptional – they may not be usedGood style to use them3MOTIVATION: 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 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 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


View Full Document

UNC-Chapel Hill COMP 401 - COMP 401- INTERFACES

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download COMP 401- INTERFACES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COMP 401- INTERFACES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COMP 401- INTERFACES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?