Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 139 - Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define.

Similar presentations


Presentation on theme: "CS 139 - Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define."— Presentation transcript:

1 CS 139 - Algorithm Development Modularity

2 Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define the necessary modules so that each module solves a single logical subtask Determine if each module shall be a procedure or a function Determine the information that modules must share Determine, for each such piece of shared information, the type of parameter

3 Modularity Most algorithms solve complicated problems. need to break the algorithm into smaller pieces. We write subalgorithms to simplify the algorithm. This is the very core of good design. Focus on meaning rather than implementation Easy to understand and fix This wasn’t always true, thus have had to overcome bad precedents and habits.

4 Advantages of Modularity Details of the algorithm are hidden in the subalgorithms -- enhances understanding by hiding obscurant details Makes the algorithm easier to write by reducing the complexity Saves time, space, and effort --modules can be called from many places within an algorithm Permits reuse of logic -- if done properly, modules can be reused across different algorithms

5 Hierarchy of Abstraction Each subalgorithm handles a single logical “chunk” of the solution. “main” algorithm coordinates the overriding logic. multiple levels (task, subtask, etc. ) details hidden within the subtask, etc.) Task 2 Main Task 3Task 1 Task 4

6 Example: Compute Income Tax Main Task compute taxable income compute tax due compute refund or tax owed Subtask: Compute Taxable Income compute income compute deductions compute taxable income = income - deductions

7 Things you should know How to break up an algorithm into modules How to use parameters and arguments Should you use a function or procedure? How to write a function or procedure How to invoke a function or procedure

8 Parameters Special kind of variable that allows a data value to be passed between a module and the algorithms that use it Formal parameter – in the declaration Specify type and data type Actual parameter – specified when the module is called Association is positional Must conform to formal list in number and type

9 Types of Parameters Input – allows a value to be passed to a module (from calling algorithm) Output – allows a value to be passed from a module (to calling algorithm) Input/Output – allows in and out communication

10 Two Types of Modules  Function: expression of a single value, as in Math  May not read input or print output Procedure: may produce any number of values, modify variables, read and print We can tell which to use (by thinking abstractly about what it does) before trying to write it.

11 import java.io.*; class Comparison { void demo1 (int a, int b) { System.out.println (a+b); } int demo2 (int a, int b) { return (a+b); } public static void main (String args []) { int num1=8; int num2=4; int num3; Comparison c=new Comparison (); c.demo1 (num1, num2); num3=c.demo2 (num1, num2); } Java Example

12 Which Kind of Subalgorithm? Why? algorithm Drive_Car procedure Open_Door procedure Turn_Ignition function Is_Fuel_Low procedure Turn_Wheel procedure Accelerate function Speed procedure Decelerate procedure Fill_Up function Miles_Per_Gallon endalgorithm //Drive_Car

13 Function Definition and Use Definition: (outside main algorithm) function Cube returnsa Num (in_value isoftype in Num) // Purpose: returns in_value raised to the 3 rd power Cube returns (in_value * in_value * in_value) endfunction Use: (inside main algorithm) box_side isoftype Num volume isoftype Num volume <- Cube (box_side)

14 Procedure Def. and Use Definition: (outside main algorithm) procedure Print_Heading (name isoftype in String, section isoftype in Num) // Purpose: prints an informational heading print (“Name: ”, name) print (“Section: ”, section) endprocedure Use: (inside main algorithm) my_name isoftype String my_section isoftype Num Print_Heading (my_name, my_section)

15 Calling functions vs procedures A function is used as an expression A procedure is used as a statement algorithm Do_Stuff //dummy alg to show module use this_Num isoftype Num that_Num isoftype Num read(this_Num) that_Num <- Cub e(this_Num) Do_Fancy_Outpu t(this_Num,that_Num) this_Num <- that_Num + this_Num print(this_Num, that_Num) endalgorithm //Do_Stuff

16 Example: Area algorithm WasherArea (declare big_radius, small_radius, area, big_area, small_area to be type Num) print (“Enter the outside radius”) read (big_radius) big_area <- CircleArea (big_radius) print (“Enter the inside radius) read (small_radius) small_area <- CircleArea (small_radius) area <- big_area - small_area print (“The area of the washer is ”, area) endalgorithm function CircleArea returnsa Num (radius isoftype in Num) PI is 3.14 CircleArea returns (PI * radius * radius) end function

17 Example: Average procedure GetInput (name isoftype in/out String, grade isoftype in/out Num) print (“Enter student’s name”) read (name) print (“Enter student’s grade”) read (grade) endprocedure function Average returnsa Num (first isoftype in Num, second isoftype in Num) Average returns (first + second) / 2 end function

18 algorithm AverageScore (declare name1, name2 (String) average_score, grade1, and grade2 (Num)) GetInput (name1, grade1) GetInput (name2, grade2) average_score <- Average (grade1, grade2) print (“The average score of the students is”, average_score) endalgorithm


Download ppt "CS 139 - Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define."

Similar presentations


Ads by Google