Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.

Similar presentations


Presentation on theme: "Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is."— Presentation transcript:

1 Modularity Computer Science 3

2 What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is called modularity. To make your software more modular: –Functions with fewer inputs/outputs –Break up long functions –Break up code that is hard to understand

3 Why is Modularity Important? Benefits of modularity:  Individually test components  Smaller portions to understand at once  Modules can be reused For the benefits to happen, modules must:  Be small (rarely >25 lines, never >50)  Be simple (few inputs/outputs)  Have a clearly explainable function

4 Example: What does this do? Read in a list L of names REPEAT FOR each adjacent pair of Names in L DO Call the earlier N 1 Call the later N 2 IF N 2 is alphabetically before N 1 THEN Swap N 1 and N 2 END FOR UNTIL a pass with no swaps Write out L

5 How about this? Read in a list L of names Sort L alphabetically Write out L The 2nd algorithm is more modular – the sorting mechanics is moved to a module.

6 Testing Modules Modules are typically tested by calling them from specialized functions called drivers. –The module is tested in the form it will be used –I.e. do not add special code, otherwise the module may not work when it is removed –Don’t want to change a module after it has been tested

7 Summary Modular programs are divided up into smaller parts with clear interfaces –Easier to modify –Easier to understand Modules are tested with special programs called drivers Want to avoid changing a module after it is tested.

8

9 Java Standard Class Library Java provides a number of classes available for use. –These are called the Java Standard Class Library –A class library is a group of classes usable by Java programs. Details of these classes can be found at http://java.sun.com/j2se/1.4.1/docs/api/ http://java.sun.com/j2se/1.4.1/docs/api/ –In this case 1.4.1 is the latest version of Java –Can find documentation for earlier (or later) versions

10 Packages Class libraries are divided into groups of classes called packages –Packages are named by a series of words separated by periods. –The leftmost name is the name of the class library. –E.g., java.lang.string. Java is the class library To use a package, use import –Place import ; at the top of your file. E.g.: import java.io.*; –Can also import a single class instead of the *: import java.io.BufferedReader;

11 java.lang Most classes that you have been using are members of the java.lang package. E.g.: –String –All types of exceptions No need to import java.lang.*. All programs automatically import java.lang.

12 The Math class java.lang.Math provide some useful static methods: –Math.sqrt(x) finds the square root of x. –Math.pow(x,y) finds x y. –Math.random() returns a random double between 0 & 1 –Math.max(x,y) and Math.min(x,y) –sin, cos, tan, log, abs, round –Lots more. Look them up if necessary.

13 The Random Class Import from java.utils.* Enables you to seed a psuedo-random number generator –Random() seeds it using date/time –Random(seed) lets you seed it (useful for testing). –Random.nextInt(n) gives you a random number between 0 and n-1.


Download ppt "Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is."

Similar presentations


Ads by Google