Skip to main content
Engineering LibreTexts

15.3: Encapsulation and Generalization

  • Page ID
    15251
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In Section 6.2, we presented a way of writing programs called incremental development. In this section we present another program development process called “encapsulation and generalization”. The steps are:

    1. Write a few lines of code in main or another method, and test them.
    2. When they are working, wrap them in a new method, and test again.
    3. If it’s appropriate, replace literal values with variables and parameters.

    The second step is called encapsulation; the third step is generalization.

    To demonstrate this process, we’ll develop methods that display multiplication tables. Here is a loop that displays the multiples of two, all on one line:

    int i = 1;
    while (i <= 6) {
        System.out.printf("%4d", 2 * i);
        i = i + 1;
    }
    System.out.println();
    

    The first line initializes a variable named i, which is going to act as a loop variable: as the loop executes, the value of i increases from 1 to 6; when i is 7, the loop terminates.

    Each time through the loop, we display the value 2 * i padded with spaces so it’s four characters wide. Since we use System.out.printf, the output appears on a single line.

    After the loop, we call println to print a newline and complete the line. Remember that in some environments, none of the output is displayed until the line is complete.

    The output of the code so far is:

       2   4   6   8  10  12
    

    The next step is to “encapsulate” this code in a new method. Here’s what it looks like:

    public static void printRow() {
        int i = 1;
        while (i <= 6) {
            System.out.printf("%4d", 2 * i);
            i = i + 1;
        }
        System.out.println();
    }
    

    Next we replace the constant value, 2, with a parameter, n. This step is called “generalization” because it makes the method more general (less specific).

    public static void printRow(int n) {
        int i = 1;
        while (i <= 6) {
            System.out.printf("%4d", n * i);
            i = i + 1;
        }
        System.out.println();
    }
    

    Invoking this method with the argument 2 yields the same output as before. With the argument 3, the output is:

       3   6   9  12  15  18
    

    And with argument 4, the output is:

       4   8  12  16  20  24
    

    By now you can probably guess how we are going to display a multiplication table: we’ll invoke printRow repeatedly with different arguments. In fact, we’ll use another loop to iterate through the rows.

    int i = 1;
    while (i <= 6) {
        printRow(i);
        i = i + 1;
    }
    

    And the output looks like this:

       1   2   3   4   5   6
       2   4   6   8  10  12
       3   6   9  12  15  18
       4   8  12  16  20  24
       5  10  15  20  25  30
       6  12  18  24  30  36
    

    The format specifier \%4d in printRow causes the output to align vertically, regardless of whether the numbers are one or two digits.

    Finally, we encapsulate the second loop in a method:

    public static void printTable() {
        int i = 1;
        while (i <= 6) {
            printRow(i);
            i = i + 1;
        }
    }
    

    One of the challenges of programming, especially for beginners, is figuring out how to divide up a program into methods. The process of encapsulation and generalization allows you to design as you go along.


    This page titled 15.3: Encapsulation and Generalization is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?