7. Modular design (モジュール 設計) Modular design is an important technique for designing good quality program systems. In this part, you will learn: (内容) The.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Reconfigurable Computing S. Reda, Brown University Reconfigurable Computing (EN2911X, Fall07) Lecture 07: Verilog (3/3) Prof. Sherief Reda Division of.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introduction to Computers and Programming Introduction to Methods in Java.
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
SE: CHAPTER 7 Writing The Program
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Procedural programming in Java Methods, parameters and return values.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm.
1 MT258 Computer Programming and Problem Solving Unit 4.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
int [] scores = new int [10];
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Classes - Intermediate
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
The need for Programming Languages
Coupling and Cohesion Rajni Bhalla.
FUNCTIONS In C++.
Building Java Programs
User-Defined Functions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
User Defined Functions
Building Java Programs
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Why did the programmer quit his job?
Classes and Objects.
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 6 Methods.
Week 4 Lecture-2 Chapter 6 (Methods).
PROGRAM DESIGN CONCEPTS AND TECHNIQUES
CS1100 Computational Engineering
Presentation transcript:

7. Modular design (モジュール 設計) Modular design is an important technique for designing good quality program systems. In this part, you will learn: (内容) The definition of module (モジュールの定義) Module strength or cohesion (モジュールの強度 又は 結合力 ) Module coupling (モジュール連結) Inter-module communication (モジュールの間の交 信) Information hiding (情報隠蔽)

7.1 The definition of module (モジュールの定義) There is no unique definition of module. Module is just a notion that represents a program unit with the following features: A module provides functional services. For example, given an input, a module performs a function or behavior. A module can be replaced by another module implementing the same functional specification, with little or no impact on the entire program. The idea of modular design is similar to that of designing other products, such as cars, houses, computers, and so on.

Specifically, a module can be interpreted as one of the following program units: A procedure in Pascal. A function in C. An operation in general. A process in DFDs. A method in Java. A unit that groups operations. A class (e.g., a Java class). These are primitive understanding of module. These are advanced understanding of module.

Examples (1)Primitive notion of module: a Java method: int Swap(int x, int y) { int a = 0; //local variable declaration a = x; //statement-1 x = y; //statement-2 y = a; //statement-3 }

(2) Advanced notion of module: module A; variable declarations; operation-1; operation-2; operation-3; … operation-n end-module A program unit that groups operations together.

A Java class: class Calculator { int reg; public Calculator() { initialize reg; } //constructor of the class public int Add(int i) { reg = reg + i; } public int Subtract(int i) { reg = reg – i; } public int Multiply(int i) { reg = reg * i; }

7.2 Module strength or cohesion (モジュールの強度 又は結合 力 ) We use the primitive notion of module in the discussions from now on in this part, since we will discuss the related issues based on the advanced notion of module later in this course. Description: module strength or cohesion is a measure of the extent to which the statements or functions in a module belong together. (モ ジュールの強度又は結合力は、文又は操作が 同じモジュールに所属する程度を測る尺度で ある)

In general, if statements are related closely in performing a single task or common tasks, they should belong to the same module. (同じタス クを完成するために必要な文又は操作が同じ モジュールに所属する訳) However, since there is no unique interpretation of the meaning of a single task and common tasks, there is no precise standard to define what statements should be used in the same module. There are some guidelines for designing modules.

Guidelines for designing modules モジュール設計の指針) The statements are all necessary to perform one particular task within the overall program structure. For example, Add, Subtract, and Mutiply in class Calculator can be individual modules. The statements encompass a variety of small unrelated tasks, but since they must be performed at the same time, for convenience they are grouped in a single module. For example, the initialization of global variables. The statements perform two or more related tasks that are always performed together and therefore have been placed in the same module. For example, writing data to files, at the end of an execution of a program.

7.3 Module coupling (モジュール連結) Description: module coupling is a measure of the interdependence between modules, within a program system. In general, the stronger the module coupling is, the more possible one of the modules is affected by changing the other module. For example, the small families and big family systems can help explain this issue. In general, module coupling through global variables are stronger than those that couple through arguments (parameters).

Example class Calculator { int reg; void Calculator() { initialize reg; } //constructor of the class int Add(int i) { reg = reg + i; } int Subtract(int i) { reg = reg – i; } int Multiply(int i) { reg = reg * i; } class Calculator { int reg; void Calculator() { initialize reg; } //constructor of the class int Add(int reg, int i) { reg = reg + i; } int Subtract(int reg, int i) { reg = reg – i; } int Multiply(int reg, int i) { reg = reg * i; } Use the same global variable reg. Use no common global variable

7.4 Inter-module communication (モジュールの間の交信) A program is usually composed of related modules. A module is related to other modules in the manner that either it is invoked (or called) by other modules or it invokes (or calls) other modules. When a module invokes another module, the communication by passing data items may be necessary. The problem is how modules can communicate with each other, and what are advantages and disadvantages of each communication method.

Communication methods ( 交信手段) Communication by arguments Communication by global (or external) variables.

Let’s take the following program as an example to explain the meaning of these two methods. Problem: input 10 non-zero integers and output the two numbers: (1) the number of integers that is greater than the average of the input integers; (2) the number of integers that is smaller than the average of the input integers. For example, input: 5, 78, 56, 89, 12, 24, 75, 98, 34, 68 (average = 539 / 10 = 53.9); output: count1 = 6 (the greater numbers: 78, 56, 89, 75, 98, 68) count2 = 4 (the smaller numbers: 5, 12, 24, 34)

Two Java-based pseudocode Program 1: import java.io.*; public class CountNumbers { //there is no global variables here public static void main(String args[]) { int numbers[] = new int[10]; int count1 = 0, count2 = 0; double average = 0.0; for (int i = 0; i < numbers.length; i++) INPUT numbers[i]; average = CalculateAverage(numbers); count1 = CountGreater(average, numbers); count2 = CountSmaller(average, numbers); OUTPUT count1 and count2; }

double CalculateAverage(int a[]) //obtain average { int total = 0; double ave = 0.0; for (int j = 0; j < a.length; j++) total = total + a[j]; ave = total / 10; return ave; }

int CountGreater(double ave, int a[]) { for (int k = 0; k < a.length; k++) if (a[k] > ave) count1 = count1 + 1; return count1; } int CountSmaller(double ave, int a[]) { for (int k = 0; k < a.length; k++) if (a[k] < ave) count2 = count2 + 1; return count2; }

Program 2: import java.io.*; public class CountNumbers { //there are four global variables here static int numbers[] = new int[10]; static double average = 0.0; static int count1 = 0, count2 = 0; public static void main(String args[]) { for (int i = 0; i < numbers.length; i++) for (int l = 0; l < numbers.length; l++) numbers[l] = l +1; CalculateAverage(); CountGreaterSmaller(); //compute count1 and count2 System.out.println("Count1:" + count1); System.out.println("Count2:" + count2); }

static void CalculateAverage() { // Obtain average int total = 0; for (int j = 0; j < numbers.length; j++) total = total + numbers[j]; average = total / 10; } static void CountGreaterSmaller() { for (int k = 0; k < numbers.length; k++) if (numbers[k] > average) count1 = count1 + 1; else count2 = count2 + 1; }

Advantages and disadvantages (利点と弱点) Communication by global variables strengthens module coupling, therefore decreases the independence of modules. This is not a good situation from the modularity point of view. However, the efficiency of communication may be high, because there is no need to copy any values when passing arguments to parameters of modules.

Communication by arguments reduces module coupling, therefore improve the independence of modules. This is usually regarded as a good quality of program, because the change of one module will produce less or no impact on other modules. But the efficiency of communication may be low, because there is a need to copy values when passing arguments to parameters of modules.

7.5 Information hiding ( 情報隠蔽) Information hiding is a quality that all of the implementation details, including local variables and algorithms, of a module are hidden from other modules or parts of the program. In other words, a module can only be used by other modules through invocation or calling of that module to perform its task, but cannot access the local variables and code of the algorithm of the module.

Example import java.io.*; public class CountNumbers { //there are four global variables here static int numbers[] = new int[10]; static double average = 0.0; static int count1 = 0, count2 = 0; public static void main(String args[]) { for (int i = 0; i < numbers.length; i++) for (int l = 0; l < numbers.length; l++) numbers[l] = l +1; CalculateAverage(); CountGreaterSmaller(); //compute count1 and count2 System.out.println("Count1:" + count1); System.out.println("Count2:" + count2); }