![Page 1: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/1.jpg)
COMP 110FUNCTIONS
Instructor: Jason Carter
![Page 2: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/2.jpg)
2
OUTLINE
Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise refinement Code Reuse Programming Style Variables, Named Constants, Literals Comments and Identifier Names
![Page 3: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/3.jpg)
3
GENERAL PURPOSE BMI CALCULATOR
Does not assume height or weight
Specialized could know my height
![Page 4: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/4.jpg)
4
BMI CALCULATOR SPECIALIZED FOR AN INDIVIDUAL’S HEIGHT
Need to enter only weight
Height is hard-coded into the program
weight
![Page 5: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/5.jpg)
5
A SOLUTION
public class AMyBMICalculator { public double calculateMyBMI(double weight) { }}
![Page 6: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/6.jpg)
6
A SOLUTION (EDIT)
public class AMyBMICalculator { public double calculateMyBMI(double weight) { }}
![Page 7: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/7.jpg)
7
A SOLUTION
public class AMyBMICalculator { public double calculateMyBMI(double weight) { return weight/ (1.94 * 1.94); }}
Relationship with ABMICalculator?
![Page 8: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/8.jpg)
8
CUSTOMIZED VS. GENERAL PURPOSE
public class AMyBMICalculator { public double calculateMyBMI(double weight) { return weight/ (1.94 * 1.94); }}
public class ABMICalculator { public double calculateBMI (double weight,
double height) {return weight/ (height * height);
}}
One vs. two parameters
Basic formula is the same (can cut and
paste)
![Page 9: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/9.jpg)
9
SHOULD REUSE!
public class AMyBMICalculator { public double calculateMyBMI(double weight) { return weight/ (1.94 * 1.94); }}
public class ABMICalculator { public double calculateBMI (double weight,
double height) {return weight/ (height * height);
}}
Should reuse code to avoid duplication of
effort and errors such as: (weight)/1.94
Particularly important for complex code
![Page 10: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/10.jpg)
10
HOW TO REUSE ABMICALCULATOR
Create an instance of ABMICalculator Invoke the method calculateBMI() on this
instance passing it my weight and my height as actual parameters
The value returned by the method is my BMI
![Page 11: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/11.jpg)
11
INTERACTIVE EXECUTION OF THE STEPS
Create an instance of ABMICalculator
Invoke the method calculateBMI() on this instance passing it my weight and my height as actual parameters
The value returned by the method is my BMI
![Page 12: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/12.jpg)
12
PROGRAMMING THE STEPS
Create an instance of ABMICalculator Invoke the method calculateBMI() on this
instance passing it my weight and my height as actual parameters
The value returned by the method is my BMI
public class AMyBMICalculator { public double calculateMyBMI(double weight) { }}
return
(new ABMICalculator())
.calculateBMI( );
weight, 1.94
![Page 13: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/13.jpg)
13
METHOD INVOCATION SYNTAX
(new ABMICalculator()).calculateBMI(weight, 1.94);
Target Object Method Name Actual Parameters
![Page 14: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/14.jpg)
14
FUNCTION COMPOSITION
public class AMyBMICalculator { public double calculateMyBMI(double weight) { return (new ABMICalculator()).calculateBMI(weight,1.94); }}
The body of the calling function calls (invokes) other functions to do its job
Passes the “buck” to the callee or called functions
calculateMyBMI() calls calculateBMI() Supports reuse
![Page 15: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/15.jpg)
15
CALL GRAPHS
calculateMyBMI(74.98)
calculateBMI(74.98,1.94)
19.92
19.92
![Page 16: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/16.jpg)
16
GRAPHICAL ILLUSTRATION OF THE CALCULATEMYBMI CALL
Class AMyBMICalculator
Class ABMICalculator
AMyBMICalculator
ABMICalculatorreturn (new ABMICalculator())
public double calculateBMI(double weight, double height) {
return weight/(height*height);}
.calculateBMI(weight, 1.94);
instance of
instance of
public double calculateMyBMI(double weight) {
}
![Page 17: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/17.jpg)
17
MATHEMATICAL INTUITION BEHIND FUNCTION INVOCATION
tan(x) = sin(x) / cos(x)
tan(90)
sin(90) cos(90)
1 0
![Page 18: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/18.jpg)
18
AVERAGEBMICALCULATOR
Weight 1Weight 2
![Page 19: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/19.jpg)
19
A SOLUTION
public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { }}
![Page 20: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/20.jpg)
20
A SOLUTION (EDIT)
public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { <edit here> }}
![Page 21: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/21.jpg)
21
A SOLUTION
public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { return ((new ABMICalculator()).calculateBMI(weight1, 1.94) +
(new ABMICalculator()).calculateBMI(weight2, 1.94))/2; }}
Creating a new instance of AMyAverageBMICalculator each time calculateBMI is to be called!
![Page 22: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/22.jpg)
22
INTERACTIVE EQUIVALENT
Instance 1 Instance 2
![Page 23: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/23.jpg)
23
A BETTER INTERACTIVE APPROACH
Instance 1
ObjectEditor window identifies the appropriate instance. Need way to name objects in a program.
![Page 24: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/24.jpg)
24
NAMING MEMORY LOCATIONS
Can name values in a program by using variables
Each program value stored in a memory location
Variable declarations name memory locations Have already seen variable declarations!
![Page 25: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/25.jpg)
25
FORMAL PARAMETERS AS VARIABLES
Formal parameters are special kinds of variables.
weight is name of memory location that stores the actual parameter passed by caller
public class AMyBMICalculator { public double calculateMyBMI(double weight) { return weight/ (1.94 * 1.94); }}
![Page 26: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/26.jpg)
26
INTERNAL METHOD VARIABLES
Like formal parameters are declared with type and name
Name in subsequent code refers to value stored in memory location
Declared in a method body rather than header Can be explicitly given initial values Which can be changed later Make program more efficient as an extra object is
not instantiatedpublic class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { ABMICalculator aBMICalculator = new ABMICalculator(); return (aBMICalculator .calculateBMI(weight1, 1.94) +
aBMICalculator.calculateBMI(weight2, 1.94))/2; }}
![Page 27: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/27.jpg)
27
MORE USE OF VARIABLES
bmi1 and bmi2 name memory locations that store the two intermediate results
Not really needed to make programs efficient
public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { ABMICalculator aBMICalculator = new ABMICalculator(); double bmi1 = aBMICalculator.calculateBMI(weight1, 1.94); double bmi2 = aBMICalculator.calculateBMI(weight2, 1.94); return bmi1 + bmi2; }}
![Page 28: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/28.jpg)
28
WHICH IS BETTER?public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { ABMICalculator aBMICalculator = new ABMICalculator(); return (aBMICalculator .calculateBMI(weight1, 1.94) +
aBMICalculator.calculateBMI(weight2, 1.94))/2; }}public class AMyAverageBMICalculator { public double calculateMyAverageBMI(double weight1, double weight2) { ABMICalculator aBMICalculator = new ABMICalculator(); double bmi1 = aBMICalculator.calculateBMI(weight1, 1.94); double bmi2 = aBMICalculator.calculateBMI(weight2, 1.94); return bmi1 + bmi2; }} First solution is more concise Second solution separates various steps, giving
names to each intermediate calculated value Hard to argue between them
Second solution makes it easier to single-step through code
![Page 29: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/29.jpg)
29
PROGRAMMING STYLE
More than one solution to a problem Some solutions arguably “better” than others
E.g. one solution allows reuse other does not. Programming style determines which solution is
chosen Style as important as correctness Good style often promotes correctness
![Page 30: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/30.jpg)
30
STYLE RULES
Elements of Style Support code reuse Other style rules?
![Page 31: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/31.jpg)
31
IMPROVING THE STYLE
public class AMyBMICalculatorWithReuse { public double calculateMyBMI(double weight) { return (new ABMICalculator()).calculateBMI(weight,1.94); }}
A Magic Number
A mysterious (at least to some) number in
code
![Page 32: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/32.jpg)
32
NAMED CONSTANTS
public class AMyBMICalculator { public double calculateMyBMI(double weight) { final double MY_HEIGHT = 1.94; return (new ABMICalculator()).calculateBMI(weight, MY_HEIGHT); }}
Like variables have type, name and value They must have an initial value
Initial value of a variable is optional The final keyword says value cannot be changed
later The name is all caps by convention
![Page 33: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/33.jpg)
33
NAMED CONSTANTS, LITERALS, CONSTANTS & VARIABLES
public class AMyBMICalculator { public double calculateMyBMI(double weight) { final double MY_HEIGHT = 1.94; return (new ABMICalculator()).calculateBMI(weight, MY_HEIGHT); }}
Literal A value directly specified in the program
Constant A fixed value Can be literal or named constant
Variable A potentially variable value
Literal Variable ConstantNamed
Constant
![Page 34: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/34.jpg)
34
POUND INCH BMI CALCULATOR
![Page 35: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/35.jpg)
35
POUND INCH BMI CALCULATOR
Weight in pounds
Height in inches
![Page 36: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/36.jpg)
36
STEPS FOR REUSING ABMICALCULATOR
![Page 37: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/37.jpg)
37
STEPS FOR REUSING ABMICALCULATOR
![Page 38: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/38.jpg)
38
STEPS FOR REUSING ABMICALCULATOR
Calculate weight in Kgs from weight in Pounds Calculate height in Metres from height in inches Call calculateBMI() of ABMICalculator with these
values Return the value returned by this call
![Page 39: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/39.jpg)
39
A SOLUTION
public class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) {
}}
![Page 40: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/40.jpg)
40
A SOLUTION (EDIT)
public class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) {
}}
![Page 41: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/41.jpg)
41
A SOLUTION (EDIT)public class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( weightInLbs/2.2, heightInInches*2.54/100); }}
![Page 42: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/42.jpg)
42
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 43: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/43.jpg)
43
REAL-WORLD ALGORITHM
Enter Class Distribute handouts Set up laptop projection. Revise topics learnt in the last class. Teach today’s topics. Leave Class
![Page 44: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/44.jpg)
44
ALGORITHM FOR REUSING ABMICALCULATOR
Calculate weight in Kgs from weight in Pounds Calculate height in Metres from height in inches Call calculateBMI() of ABMICalculator with these
values Return the value returned by this call
![Page 45: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/45.jpg)
45
2ND LEVEL ALGORITHM
Calculate weight in kgs from weight in Pounds Divide weight in Pounds by 2.2
Calculate height in Meters from height in inches Calculate height in centimeters from height in
inches and divide it by 100 to get height in meters Call calcuateBMI() of ABMICalculator with these
values Return the value returned by this call
![Page 46: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/46.jpg)
46
3RD LEVEL ALGORITHM
Calculate weight in kgs from weight in Pounds Divide weight in Pounds by 2.2
Calculate height in Metres from height in inches Calculate height in centimetres from height in
inches and divide it by 100 to get height in metres Multiply height in Inches by 2.54 to get height in
centimetres Call calcuateBMI() of ABMICalculator with these
values Return the value returned by this call
![Page 47: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/47.jpg)
47
STEPWISE REFINEMENTNatural Language Algorithm
•Calculate weight in Kgs from weight in Pounds•Calculate height in Meters from height in inches•Call calculateBMI() of ABMICalculator with these values•Return the value returned by this call
Programming Language Algorithm
public class APoundInchBMICalculator () {
public double calculateBMI( double weightInLbs, double heightInInches) {
return (new ABMICalculator()).calculateBMI( weightInLbs/2.2, heightInInches*2.54/100); }}
![Page 48: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/48.jpg)
48
STYLE PROBLEMSpublic class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( weightInLbs/2.2, heightInInches*2.54/100); }}
Unlike algorithm, code is single-level By defining functions for each algorithm level
we can create multi-level code Multi-level code would be more reusable as
there are more parts that can be used independently
![Page 49: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/49.jpg)
49
MULTI-LEVEL CODEpublic class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) {???
} public double toKgs(double weightInLbs) {
??? }}
Can be reused in contexts other than BMI (designing for reuse).
Design for Reuse vs. Reusing available code
![Page 50: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/50.jpg)
50
MULTI-LEVEL CODE (EDIT)
public class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) {???
} public double toKgs(double weightInLbs) {
??? }}
![Page 51: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/51.jpg)
51
MULTI-LEVEL CODEpublic class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) { return toCentiMetres(heightInInches)/100; } public double toCentiMetres(double heightInInches) { return heightInInches*2.54; } public double toKgs(double weightInLbs) { return weightInLbs/2.2; }}
![Page 52: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/52.jpg)
52
MULTI-LEVEL CALL GRAPH
calculateBMI(165,70)
toKgs (165)
75
toMetres (70)
toCentiMetres (70)
177.8
1.778
(new ABMICalculator). calculateBMI(75,1.77)
23.72
23.72
![Page 53: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/53.jpg)
53
EXTERNAL VS. INTERNAL METHOD INVOCATION SYNTAX
(new ABMICalculator()).calculateBMI(75,1.77));
Actual ParametersMethod
NameTarget Object
toKgs(165);
![Page 54: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/54.jpg)
54
EXTERNAL VS. INTERNAL METHOD INVOCATION SYNTAX
External Method Call Caller (calling method) and callee (called method)
belong to different objects calculateBMI() of APoundInchBMICalculator instance
calls calculateBMI() of ABMICalculator instance Internal Method Call
Caller and callee methods belong to same object calculateBMI() of APoundInchBMICalculator instance
calls toKgs() of APoundInchBMICalculator instance Target object optional in internal method call Target object needed because multiple objects
may have the same method When target object omitted caller’s object is
target object
![Page 55: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/55.jpg)
55
REUSABILITYpublic class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); } public double toMetres(double heightInInches) { return toCentiMetres(heightInInches)/100; } public double toCentiMetres(double heightInInches) { return heightInInches*2.54; } public double toKgs(double weightInLbs) { return weightInLbs/2.2; }}
Can be reused in other classes
(new APoundInchBMICalculator()).toKgs(165)
(new APoundInchBMICalculator()).toMetres(70)
(new APoundInchBMICalculator()).toCentiMetres(70)
![Page 56: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/56.jpg)
56
LACK OF REUSABILITYpublic class APoundInchBMICalculator { public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( weightInLbs/2.2, heightInInches*2.54/100); }}
A single method implements all three conversions
Cannot reuse each conversion independent of BMI calculation
![Page 57: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/57.jpg)
57
MAGIC NUMBERS REVISITEDpublic class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) { return toCentiMetres(heightInInches)/100; } public double toCentiMetres(double heightInInches) { return heightInInches*2.54; } public double toKgs(double weightInLbs) { return weightInLbs/2.2; }}
magic numbers?
![Page 58: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/58.jpg)
58
“WELL-KNOWN” VS. “OBSCURE” NUMBER
weightInLbs/2.2
Well-Known Number (Natural
constant)
(new ABMICalculator). calculateBMI(74, 1.77);
Obscure Number
![Page 59: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/59.jpg)
59
WHAT IS A MAGIC NUMBER?
Obscure number is a magic number Well-known number is not
A number defined by law of nature e.g number of centimeters in an inch Π
What is well-known depends on the audience e.g. number of centimeters in an inch
Numbers defined by law of nature may not be considered magic numbers
All other numbers should be considered magic numbers
![Page 60: C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise](https://reader034.vdocuments.site/reader034/viewer/2022051618/56649d2c5503460f94a023cc/html5/thumbnails/60.jpg)
60
REMOVING ALL POTENTIALLY MAGIC NUMBERSpublic class APoundInchBMICalculator {
public double calculateBMI( double weightInLbs, double heightInInches) { return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }
public double toMetres(double heightInInches) { final double CMS_IN_METRES = 100; return toCentiMetres(heightInInches)/ CMS_IN_METRES; } public double toCentiMetres(double heightInInches) { final double CMS_IN_INCH = 2.54; return heightInInches* CMS_IN_INCH; } public double toKgs(double weightInLbs) { final double LBS_IN_KG = 2.2; return weightInLbs/LBS_IN_KG; }}