comp 110 style

97
COMP 110 STYLE Instructor: Jason Carter

Upload: dasha

Post on 22-Feb-2016

59 views

Category:

Documents


0 download

DESCRIPTION

Comp 110 Style. Instructor: Jason Carter. Interfaces and More Style. Define contracts between our users and implementers Optional – they may not be used Good style to use them Will study additional elements of style in this chapter. Two Ways of Doing the BMI Spreadsheet. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Comp 110 Style

COMP 110STYLE

Instructor: Jason Carter

Page 2: Comp 110 Style

2

INTERFACES AND MORE STYLE Define contracts between our users and

implementers Optional – they may not be used Good style to use them Will study additional elements of style in this

chapter

Page 3: Comp 110 Style

3

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

Page 4: Comp 110 Style

4

BMI SPREADSHEET

Page 5: Comp 110 Style

5

ABMISPREADSHEETABMISpreadsheet Instance

weight height

getWeight()

setWeight()

getHeight()

setHeight() getBMI()

ObjectEditor

calls

writes

weight

calls

reads

height

calls

reads writes

callsnew

weight

new height

reads

Page 6: Comp 110 Style

6

ANOTHERBMISPREADSHEETAnotherBMISpreadsheet Instance

weight height

getWeight()

setWeight()

getHeight()

setHeight() getBMI()

ObjectEditor

calls

writes

weight

calls

reads

height

calls

readswrites

callsnew

weight

new height

reads

bmi

Page 7: Comp 110 Style

7

METHODS THAT CHANGEABMISpreadsheet Instance

weight height

setWeight()

setHeight() getBMI()

ObjectEditor

writes

calls

writes

callsnew

weight

new height

reads

bmi

Page 8: Comp 110 Style

8

SETWEIGHT()ABMISpreadsheet Instance

weight

setWeight()

ObjectEditor

writes

calls new

weight

bmi

public void setWeight(double newWeight) {

weight = newWeight;bmi = weight / (height*height);

}

Page 9: Comp 110 Style

9

SETHEIGHT()ABMISpreadsheet Instance

height

setHeight()

ObjectEditor

writes

calls new

height

bmi

public void setHeight(double newHeight) {

height = newHeight;bmi = weight / (height*height);

}

Page 10: Comp 110 Style

10

GETBMI()ABMISpreadsheet Instance

getBMI()

ObjectEditor

reads

bmi

public double getBMI() {return bmi;

}

Page 11: Comp 110 Style

11

COMPLETE 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; }}

Page 12: Comp 110 Style

12

GRAPHICAL ALGORITHMABMISpreadsheet Instance

weight height

getWeight()

setWeight()

getHeight()

setHeight() getBMI()

ObjectEditor

calls

writes

weight

calls

reads

height

calls

readswrites

callsnew

weight

new height

reads

bmi

Page 13: Comp 110 Style

13

ENGLISH ALGORITHM Declare three instance variables

weight, height and, bmi Define three getter methods return values of the

three instance variables Define two setter methods to change weight and

height The setter methods assign new weight and

height values and compute and assign new BMI value to bmi

Page 14: Comp 110 Style

14

ALGORITHM Description of solution to a problem Can be in any “language”

graphical natural or programming language natural + programming language (pseudo code)

Can describe solution to various levels of detail

Page 15: Comp 110 Style

15

STEPWISE REFINEMENT

Declare three instance variables weight, height and, bmi

Define three getter methods return values of the three instance variables

Define two setter methods to change weight and height

The setter methods assign new weight and height values and compute and assign new BMI value to bmi

public void setWeight(double newWeight) {

weight = newWeight;bmi = weight/(height*height);

}

Natural Language

Programming Language

Page 16: Comp 110 Style

16

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; }}

Page 17: Comp 110 Style

17

OBJECTEDITOR USER INTERFACES

Page 18: Comp 110 Style

18

SIMILARITIES 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); }}

Page 19: Comp 110 Style

19

REAL-WORLD ANALOGY

manufactures

Corvette Specification

implements

implementsmanufactures

Page 20: Comp 110 Style

20

INTERFACE

Page 21: Comp 110 Style

21

public 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 INTERFACE Contract

Parameter names never

matter to Java

Page 22: Comp 110 Style

22

implements

implements

INTERFACE

ABMISpreadsheet

BMISpreadsheetABMISpreadsheet

instance

ABMISpreadsheetinstance

instance of

AnotherBMISpreadsheet

AnotherBMISpreadsheetinstance

AnotherBMISpreadsheetinstance

instance of

Page 23: Comp 110 Style

23

implements

implements

USING INTERFACES TO CLASSIFY

ABMISpreadsheet

BMISpreadsheetBMISpreadsheet

instance

BMISpreadsheetinstance

instance of

AnotherBMISpreadsheet

BMISpreadsheetinstance

BMISpreadsheetinstance

instance of

Page 24: Comp 110 Style

24

USING CAR SPECIFICATIONS TO CLASSIFY

manufactures

Corvette Specification

implements

implementsmanufactures

Corvette

Corvette

Corvette

Corvette

Page 25: Comp 110 Style

25

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

Page 26: Comp 110 Style

26

INTERFACE AS A SYNTACTIC SPECIFICATIONpublic 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); }}

Page 27: Comp 110 Style

27

INTERFACE AS A SYNTACTIC SPECIFICATIONpublic 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

Page 28: Comp 110 Style

28

INTERFACE REQUIRED Define interfaces for

All classes (that are instantiated) Some are not Include all public methods

Page 29: Comp 110 Style

29

DIFFERENCES 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); }}

Page 30: Comp 110 Style

30

ABMISPREADSHEET VS. ANOTHERBMISPREADSHEET

ABMISpreadsheet uses less space (variables) Getter methods of AnotherBMISpreadhseet are

faster Setter methods of ABMISpreadsheet are faster Usually getter methods are called more often

that setter methods e.g. when ObjectEditor refresh command is

executed Typically AnotherBMISpreadsheet will be faster,

overall

Page 31: Comp 110 Style

31

TIME-SPACE TRADEOFF

Time MiserSpace Miser

Space

Time

Page 32: Comp 110 Style

32

TIME-SPACE TRADEOFF

Space

Time

Time MiserSpace Miser

Page 33: Comp 110 Style

33

RELATING INTERFACE AND CLASS NAMESClass Name:

<Qualifier><Interface> - ABMISpreadsheet- ASpaceEfficientBMISpreadsheet- SpaceEfficientBMISpreadsheet

<Interface><Qualifier> Impl- BMISpreadsheetImpl- BMISpreadsheetSpaceEfficientImpl

Interface Name:

<ClassName>Interface- ABMISpreadsheetInterface

Page 34: Comp 110 Style

34

PROGRAMMING STYLE: THE ART OF PROGRAMMING

Science of programming Does the program work correctly?

Art programming Does the program follow “good” style rules?

Good style rules make it easier to Get the program working correctly Change the program

Page 35: Comp 110 Style

35

WRITING STYLE To the point Avoid repetition Good flow Illustrate ideasThere is more than one way to write a document

that conveys some facts: e.g. the influence of BMI on health

Page 36: Comp 110 Style

36

PROGRAMMING STYLE: THE ART OF PROGRAMMING

Define interfaces Make programs

efficient Space vs. time

Comment program Use appropriate

identifier names Use named constants

Variables vs. names Constants vs. magic

numbers

Avoid code repetition Give least privilege

No public instance variables

Implementation independent constants in interfaces

Initialize variables Independent code in

separate method

There is more than one way to write a program that correctly meets its specification: e.g.

implement the BMI user-interface

Page 37: Comp 110 Style

37

PROGRAMMING STYLE: THE ART OF PROGRAMMING

Use Interfaces

Efficiency

Comment Program

Avoid Code Repetition

Giving Least Privilege

Use Named Constants

Page 38: Comp 110 Style

38

COMMENTS

double bmi; //computed by setWeight and setHeight

Single line comments

/* This version recalculates the bmi when weight or height change, not when getBMI is called*/public class AnotherBMISpreadsheet {…}

/* recompute dependent properties */bmi = weight / (height * height);

Arbitrary comments

Page 39: Comp 110 Style

39

JAVADOC CONVENTIONS

/* This version recalculates the bmi when weight or height change, not when getBMI is called*/public class AnotherBMISpreadsheet {…}

/* This version recalculates the bmi* when weight or height change, not when* getBMI is called*/public class AnotherBMISpreadsheet {…}

You should use this convention

Page 40: Comp 110 Style

40

System.out.println(newHeight); /*debugging statement */

REMOVING DEBUGGING CODE

/* System.out.println(newHeight); /*debugging

statement */ */

/* System.out.println(newHeight); // debugging

statement */

Page 41: Comp 110 Style

41

WHAT TO COMMENT? Any code fragment needing explanation

ClassTop-level algorithm, author, date modified

Variable declarationPurpose, where used, how its value is computed

Method declarationparams, return value, algorithm, author, date modified

Statement sequenceExplanation

Debugging codeSummarizing vs. Elaborating Comments?

Page 42: Comp 110 Style

42

WHAT TO COMMENT?

double w; // weight

double weight; // weight

double weight;

double bmi; // computed by setWeight and setHeight

Bad variable name

Redundant

Self-commenting

Useful comment

Page 43: Comp 110 Style

43

JAVADOC TAGS

/* * @author Prasun Dewan * @param newWeight the new value of the property, weight. * sets new values of the variables, weight and bmi */public void setWeight (double newWeight) {

…}

/* * @author Prasun Dewan * @return the value of the variable, weight */public double getWeight () {

…}

JavaDoc tags

Page 44: Comp 110 Style

44

COMMENTING INTERFACES

/* * @param newWeight the new value of the property, weight */public void setWeight (double newWeight) { }

/* * @return the value of the variable, weight */public double getWeight () { }

Implementation independent comments

Commenting both interface and implementation?

Page 45: Comp 110 Style

45

PROGRAMMING STYLE: THE ART OF PROGRAMMING

Use Interfaces

Efficiency

Comment Program

Avoid Code Repetition

Giving Least Privilege

Use Named Constants

Page 46: Comp 110 Style

46

IMPROVING THE STYLEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ 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; }}

Code repetition

Assuming ABMICalculator does not exist

Page 47: Comp 110 Style

47

IMPROVING THE STYLE (EDIT)public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; }

double calculateBMI() { return weight/(height*height); }}

Page 48: Comp 110 Style

48

RE-USING CODEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } double calculateBMI() { return weight/(height*height); } ….}

Page 49: Comp 110 Style

49

CHANGING RE-USED CODE ONCEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); }

double calculateBMI() { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}

Declaring vs. calling methods?

Page 50: Comp 110 Style

50

PROGRAMMING STYLE: THE ART OF PROGRAMMING

Use Interfaces

Efficiency

Comment Program

Avoid Code Repetition

Giving Least Privilege

Use Named Constants

Page 51: Comp 110 Style

51

ONLY PUBLIC METHODS IN INTERFACE

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}

Not in interface

Page 52: Comp 110 Style

52

PRINCIPLE OF LEAST PRIVILEGE Do not give a user of some code more rights

than it needs Code is easier to change Need to learn less to use code Less likelihood of accidental or malicious damage to

program Like hiding engine details from car driver

ObjectEditor

ABMICalculator User ABMICalculator

getWeight() setWeight()

getHeight() setHeight()

getBMI() computeBMI()

Page 53: Comp 110 Style

53

IMPROVING THE STYLE

Use Interfaces

Efficiency

Comment Program

Use Named Constants

Avoid Code Repetition

Giving Least Privilege

Page 54: Comp 110 Style

54

IMPROVING THE STYLE

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}

Magic numbers

Page 55: Comp 110 Style

55

IMPROVING THE STYLE

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } …}

Named constants

Page 56: Comp 110 Style

56

DECLARING NAMED CONSTANTS

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } …}

Un-initializing declarationInitializing declaration

Page 57: Comp 110 Style

57

MORE ON VARIABLES VS. NAMED CONSTANTS

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... double lbsInKg = 2.2; double cmsInInch = 2.54; … double calculateBMI() { return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); } …}

Page 58: Comp 110 Style

58

ACCIDENTAL OR MALICIOUS MODIFICATION

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... double lbsInKg = 2.2; double cmsInInch = 2.54; … double calculateBMI() { lbsInKg = 22; return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); } …}

Violating least privilege

Page 59: Comp 110 Style

59

LITERALS VS. NAMED CONSTANTS VS. VARIABLES

Use constants for program values that do not change Use named constants for magic numbers

Page 60: Comp 110 Style

60

MORE CODE REPETITION

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } …}

Within same method and has the same value

Page 61: Comp 110 Style

61

REMOVING CODE REPETITION (EDIT)

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { double heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}

Page 62: Comp 110 Style

62

REMOVING CODE REPETITION

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { double heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}

Page 63: Comp 110 Style

63

LOCAL VS. GLOBAL VARIABLE

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = height*CMS_IN_INCH/100; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}

Page 64: Comp 110 Style

64

LOCAL VS. GLOBAL VARIABLEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = height*CMS_IN_INCH/100; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … public void setHeight(double newHeight) { heightInMeters = newHeight; bmi = calculateBMI(); } … double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}

Violating least privilege

Page 65: Comp 110 Style

65

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

SCOPE

heightInMeters scope

Not a scope

height scope

Page 66: Comp 110 Style

66

SCOPE OF PUBLIC ITEMS

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public double getWeight() { return weight; } …}

ObjectEditor

ABMISpreadsheet

getWeight() scope

Page 67: Comp 110 Style

67

IDENTIFIER SCOPE Region of code where the identifier is visible Arbitrary scopes not possible Least Privilege => Make scope as small as

possible

Page 68: Comp 110 Style

68

SCOPEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

heightInMeters scope

Page 69: Comp 110 Style

69

INITIALIZING DECLARATION

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 70: Comp 110 Style

70

UN-INITIALIZING DECLARATION

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres; heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 71: Comp 110 Style

71

UN-INITIALIZED VARIABLE

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 72: Comp 110 Style

72

INITIALIZING ALL VARIABLES

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 73: Comp 110 Style

73

INITIALIZING ALL VARIABLES (EDIT)

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 60, weight = 70, bmi = getBMI(); … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 74: Comp 110 Style

74

INITIALIZING ALL VARIABLES

public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 70, weight = 160, bmi = calculateBMI(); … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}

Page 75: Comp 110 Style

75

PRINTING PROPERTIESpublic 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); }}

Page 76: Comp 110 Style

76

PRINTING PROPERTIES (EDIT)

public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Height: “ + newHeight); height = newHeight; } double weight; … public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 77: Comp 110 Style

77

PRINTING PROPERTIESpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; … public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 78: Comp 110 Style

78

LESS CLUTTERED CODE (EDIT)public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; … public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 79: Comp 110 Style

79

LESS CLUTTERED CODEpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); height = newHeight; } double weight; … public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 80: Comp 110 Style

80

REMOVING DUPLICATIONpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); height = newHeight; } double weight; … public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 81: Comp 110 Style

81

REMOVING DUPLICATION (EDIT)public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties();height = newHeight; } double weight; … public void setWeight(double newWeight) { printProperties();weight = newWeight; } public double getBMI() { return weight/(height*height); } void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}

Page 82: Comp 110 Style

82

REMOVING DUPLICATIONpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties(); height = newHeight; } double weight; … public void setWeight(double newWeight) { printProperties(); weight = newWeight; } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}

Page 83: Comp 110 Style

83

SEPARATION OF CONCERNSpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties(); height = newHeight; } double weight; … public void setWeight(double newWeight) { printProperties(); weight = newWeight; } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}

Less cluttered

Page 84: Comp 110 Style

84

INDEPENDENT CODE = METHOD Separate method for:

Any independent piece of code Even if it is not duplicated If it is more than one line

public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); weight = newWeight;}

public void setWeight(double newWeight) { printWeight(); weight = newWeight;}

Page 85: Comp 110 Style

85

PRINTING BMI IN GETBMI()

public class ABMISpreadsheet implements BMISpreadsheet { double height; … public double getBMI() { printProperties(); return weight/(height*height); } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}

getBMI() never terminates

Recursive, calls itself indirectly

Infinite recursion

Avoid recursion for now

Page 86: Comp 110 Style

86

NON-PUBLIC INSTANCE VARIABLES

public class ABMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; …}

Page 87: Comp 110 Style

87

MAKING INSTANCE VARIABLES PUBLIC

public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight, bmi; …}

Other classes

Page 88: Comp 110 Style

88

HARD TO CHANGE

public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight; …}

Other classes

Page 89: Comp 110 Style

89

CONSISTENCY CONSTRAINTS VIOLATED

Inconsistent values

Page 90: Comp 110 Style

90

PRECONDITIONS CANNOT BE ENFORCED

More on this later

Page 91: Comp 110 Style

91

ENCAPSULATION PRINCIPLE Do not make instance variables public

Expose them through public methods

Page 92: Comp 110 Style

92

public final double CMS_IN_INCH = 2.54;

PUBLIC CONSTANTS

public interface BMISpreadsheet {

public final double CMS_IN_INCH = 2.54;

}

Inconsistent value cannot be stored

Implementation independent ABMISpreadsheet

AnotherBMISpreadsheet

Page 93: Comp 110 Style

93

PRINCIPLE Declare implementation-independent named

constants in interfaces implementing classes can access them

Page 94: Comp 110 Style

94

Page 95: Comp 110 Style

95

EXTRA SLIDES

Page 96: Comp 110 Style

96

STEPWISE REFINEMENT

Declare two instance variables weight and height

Define a getter method that computes the value of BMI and returns it

public double getBMI() {return weight/(height*height);

}

Natural Language

Programming Language

Page 97: Comp 110 Style

97

REAL-WORLD ANALOGY

manufactures

Corvette Specification

implements

implementsmanufactures