11
Functional Decomposition

Functional Decomposition

Embed Size (px)

DESCRIPTION

Functional Decomposition. At the beginning of this unit we discussed how most of your programs have been written like this: public class BigProgram { public static void main (String [ ] args) { statement; statement; statement; statement; statement; statement; - PowerPoint PPT Presentation

Citation preview

Page 1: Functional Decomposition

Functional Decomposition

Page 2: Functional Decomposition

• At the beginning of this unit we discussed how most of your programs have been written like this:public class BigProgram

{public static void main (String [ ] args){

statement;statement;statement;statement; statement;statement;statement;statement; statement;statement;statement;statement;

}}

Page 3: Functional Decomposition

• There are problems with writing all of the code in the main method:

– Programs can get very long. The longer they are the harder they are to manage.

– Human beings are not computers. We don’t see information as a whole like a computer.

Page 4: Functional Decomposition

• Human beings “chunk” data. For example, try memorizing the following number:

7743125039

Page 5: Functional Decomposition

• What is that number?

Page 6: Functional Decomposition

• Now, let’s divide that number into chunks:

774 – 312 - 5039

• Try memorizing that number now.

Page 7: Functional Decomposition

• What is that number?

Page 8: Functional Decomposition

• Chances are you were able to memorize it that time because you “chunked” the data.

• Programmers do the same thing when they design and develop programs.

• Functional Decomposition:– The process of breaking a problem into

smaller pieces.

Page 9: Functional Decomposition

• Functional Decomposition has advantages in programming:

– The best way to solve a complex problem is to break it down into smaller problems.

– Using methods, specific parts of a problem can be written at one time. You don’t have to worry about the whole program.

– Methods allow the program to be broken up so multiple people can work on ONE program.

Page 10: Functional Decomposition

• Again, this is what a program looks like when it is broken up into smaller methods: public class DividedProgram

{public static void main (String [ ] args){

statement;statement;

}

public static void method2( ){

statement;statement;

}

public static void method3( ){

statement;statement;

}

public static void method4( ){

statement;statement;

}

}

main method

method 2

method 3

method 4

Page 11: Functional Decomposition

• Here is an example of a main method that uses functional decomposition to call other methods:

public static void main(String[] args) throws IOException { int intNumDays = 30; // Number of days of sales String strFileName; // The name of the file to open double dblTotalSales; // Total sales for period double dblAverageSales; // Average daily sales

// Get the name of the file. strFileName = getFileName();

// Get the total sales from the file. dblTotalSales = getTotalSales(strFileName);

// Calculate the average. dblAverageSales = calcAverageSales(dblTotalSales, intNumDays);

// Display the total and average. displayResults(dblTotalSales, dblAverageSales);

System.exit(0); }