Title: Comp 110 Recitation 3
1Comp 110Recitation 3
- Instructor Sasa Junuzovic
2Agenda
- Stepwise refinement high-level to lower-level
- Internal vs. external calls
- Eclipse Introduction
- Creating a basic project
- Add class to project
- Multiple function calls
- Stepthrough using Eclipse
3Algorithm
- 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
4Pound Inch BMI Calculator
Weight in pounds
Height in inches
5Steps 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
62nd 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
73rd 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
8Stepwise Refinement
Programming Language Algorithm public class
APoundInchBMICalculator () public double
calculateBMI( double weightInLbs,
double heightInInches) return (new
ABMICalculator()).calculateBMI(
weightInLbs/2.2, heightInInches2.54/100)
Does not reflect 3rd-level algorithm instead of
a single function, we need a function for each
level of the algorithm
9External Method Invocation Syntax
Caller and callee methods are in different objects
Example from an instance of class B call
methodInA() in an instance of class A
(new A()).methodInA (p1, p2)
Target Object
Method Name
Actual Parameters
10Internal Method Invocation Syntax
Caller and callee methods are in the same object
Example from an instance of class A call
methodInA() in an the same instance of class A
this.methodInA (p1, p2)
Target Object
Method Name
Actual Parameters
11External vs. Internal Method Invocation Syntax
External
(new A()).methodInA (p1, p2)
Actual Parameters
Method Name
Target Object
Internal
methodInA (p1, p2)
12Multi-Level Code (Edit)
public class APoundInchBMICalculator public
double calculateBMI( double weightInLbs, double
heightInInches)
13Multi-Level Code (Edit)
public class APoundInchBMICalculator
public double calculateBMI( double weightInLbs,
double heightInInches) return (new
ABMICalculator()).calculateBMI( toKgs(weightInLb
s), toMetres(heightInInches))
public double toMetres(double heightInInches)
return toCentiMetres(heightInInches)/100
public double toCentiMetres(double
heightInInches) return heightInInches2.54
public double toKgs(double weightInLbs)
return weightInLbs/2.2
14Eclipse