Examples

Example of High Coupling: Below is a program based on a game 
that children often play: A player picks a color, type of car, and 
a number, and another player uses this information to make up
 a "fortune" for the first player. Trace the code below:



start
declare variables ----[ char color
[ char car
[ num a Number
get Inputs
display Fortune
end

get Inputs()
print "Enter your favorite color."
get color
print "Enter your dream car."
get car
print "Enter your favorite number."
get a Number
return

display Fortune()
print "You are going to live in a "
print color, " house, drive a "
print car, " and have ", number, " kids!
return


How re-usable are the modules in this program? If we tried to 
use the display Fortune() module in another program, we would
 have to make sure that this program used global variables called
 "color", "car", and "a Number". This is not a very re-usable 
program! Also, what if you wanted to add or change one of the 
variables? You would have to make the addition or change in 
more than one location in this program, plus make the same 
change in all the other programs where you use this module!

back to top

Example of  Data Coupling: 

back to top

Example of Common coupling: 

back to top

Example of  Control coupling:

Example 1:

Type of Record flag:
1 means get next master record
2 means get next transaction record
3 means get both

 

Example 2:
Suppose we have written a common module which adds, 
modifies, deletes, prints, and displays records from a file. 
We can only do one of the above five activities at a given time. 
From the calling module, a flag will have to be passed to indicate 
the type of activity to be done. This logic can be applied to an 
Interest computation module, which calculates simple and 
compound interest. A flag could be passed to indicate the type 
of interest to be computed.

back to top

Example of  Stamp coupling:

 back to top

Example of Coincidental cohesion:
FILE PROCESSING
OPEN EMPLOYEE UPDATE FILE
READ EMPLOYEE REC
PRINT_PAGE_HEADING
OPEN MASTER FILE EMPLOYEE)
SET PAGE COUNT TO 1
SET ERR FLAG TO FALSE
END

*no meaningful relationship to each other, just coincidently 
fall in same module.

back to top

Example of  Logical cohesion:

Example1:
READ_ALL_FILES
CASE OF FILE_CODE
1: READ CUST_TRAN REC
IF NOT EOF
INCREMENT CUST_TRAN_COUNT
END IF
2: READ CUST_MASTER REC
IF NOT EOF
INCREMENT CUST_MASTER_COUNT
END IF
3: READ PRODUCT MASTER REC
IF NOT EOF 
INCREMENT PRODUCT_MASTER_COUNT
END IF
END CASE
END

*'READ_ALL_FILES' is the class - three functions group together 
due to certain class of activities 

Example 2:

back to top

Example of Temporal cohesion:

Example 1:
A group of statements in the Form_Load() event of your VB 
project would be grouped there because they must occur as the 
program is loading.

Example 2:
Another example of temporal modularization could be the 
modules by end of periods, end of day, end of year.

                                                                                                             back to top

Example of Procedural cohesion:

Example 1:

   

Example 2:

Example of "DOALL_GUEST_PREP" module
CLEAN DISHES
PREPARE DINNER
MAKE PHONE CALL
TAKE SHOWER
SET TABLE
Related by execution order, not purpose


Example 3:
READ_STUD_REC_AND_TOTAL_STUDENT_Ages
NO_OF_Rec = 0
TOTAL_AGE = 0
READ STUD_REC
DO WHILE MORE_REC_EXIST
ADD AGE TO TOTAL_AGE
ADD 1 TO NO_OF_REC
READ STUD_REC
ENDDO
END

*'AND' word means  which module perform more than 1 function

back to top    

Example of Communicational cohesion:

Example 1:

back to top

Example of Sequential cohesion:

Example 1:



Example 2:
PROCESS PURCHASES
TOTAL_PURCHASES = 0
READ NO_OF_PURCHASES
DO LOOP_INDEX = 1 TO NO_OF_PURCHASES
GET PURCHASES
ADD PURCHASES TO TOTAL_PURCHASES
ENDDO
SALES_TAX = TOTAL_PURCHASES * SALE_TAX_PERCENT
AMT_DUE = TOTAL_PURCHASE + SALE_TAX
END

*the module calculates TOTAL_PURCHASES first, then use the 
variable TOTAL_PURCHASES in the subsequent calculation of 
AMT_DUE 

back to top

Example of  Functional cohesion:

Example 1:
CALCULATE_SALES_TAX
IF PRODUCT IS SALES_TAX EXEMPT THEN
SALES_TAX = 0
ELSE
IF PRODUCT_PRICE < $50 THEN
SALES_TAX = PRODUCT_PRICE * 0.25
ENDIF
ENDIF

*all statements relate to CALCULATE_SALES_TAX function 

Example 2:

back to top

Example of Structure chart:

back to top

Example of Structured English:

Example 1:

Retrieve the purchase order
For each item on the purchase order


if quantity received = quantity ordered
then item code is 'match'


If item code = 'match' for all items in the purchase order


then purchase order receipt code is "match"


If purchase order receipt is "match"


then approve payment for order
else return material received 

Example 2:
For each MEMBER ID in the MEMBERS file repeat the steps 
below If RETURN DATE is greater than DUE DATE
Calculate FINE for the number of extra days
print the Fine Bill

back to top

Example of Flowchart:

back to top

Hosted by www.Geocities.ws

1