Previous Section
 < Free Open Study > 
Next Section


Chapter 2

  1. Data abstraction refers to the logical picture of the data-what the data represent rather than how they are represented.

  2. Data encapsulation is the separation of the physical representation of data from the applications that use the data at a logical (abstract) level. When data abstraction is protected through encapsulation, the data user can deal with the data abstraction but cannot access its implementation, which is encapsulated. The data user accesses data that are encapsulated through a set of operations specified to create, access, and change the data. Data encapsulation is accomplished through a programming language feature.

    1. Application level (e.g., College of Engineering's enrollment information for 2003)

    2. Abstract level (e.g., list of student academic records)

    3. Implementation level [e.g., array of records that contain the members studentID (Integer), lastName (a string of characters), firstName (a string of characters), and so on]

    1. Applications of type Grocery Store include the Safeway on Main Street, the Piggly Wiggly on Broadway, and the Kroger's on First Street.

    2. User operations include SelectItem, CheckOut, PayBill, and so on.

    3. Specification of CheckOut operation:

    4. The customer does not need to know the procedure that the Grocery Store uses to check out a basket of groceries and to create a bill. The logical level (c) provides the correct interface, allowing the customer to check out without knowing the implementation of the process.

  1. Each array has a base address. The index and the base address are used to access the items in the structure.

    1. char name [20];

    2. 1009

  1. student.gpa = 3.87; Base + offset of GPA field = 100 + 21 = 121.

  2. Number of slots * cells/slot = 100 * 25 = 2,500 + 1 for Length = 2,501.

  1. In a struct, members are public by default and private only when marked. In a class, members are private by default and public only when marked.

  2. The members of a class are private unless specified as public. Client code cannot access private members.

  1. Classes can relate to one another through inheritance, containment (composition), or not at all.

  1. A base class is the class being inherited from; the derived class is the class doing the inheriting.

  2. No; a derived class does not have access to the private data members of the base class.

  3. All classes have access to public member functions of any class.

    1. The following declaration contains only additional preconditions and postconditions required by the implementation.

      class SquareMatrixType
      {
      public:
        void MakeEmpty(int n);
        // Pre:  n is less than or equal to 50.
        // Post: n has been stored into size.
        void StoreValue(int i, int j, int value);
        // Pre:  i and j are less than or equal to size - 1.
        // Post: matrix[i][j] = value
        void Add(SquareMatrixType two, SquareMatrixType result);
        // Post: result = self + two.
        void Subtract(SquareMatrixType two, SquareMatrixType result);
        // Post: result = self - two.
        void Print();
        // Post: The values in self have been printed by row on
        //       the screen.
        void Copy(SquareMatrixType two);
        // Post: self = two
      private:
        int size;        // Dimension of the matrix.
        int matrix[50][50];
      };
      
    2. void SquareMatrixType::MakeEmpty(int n)
      {
        size = n;
        for (int row = 0; row < size; row++)
          for (int col = 0; col < size; col++)
            matrix[row][col] = 0;
      }
      
      void SquareMatrixType::StoreValue(int i, int j, int value)
      {
        matrix[i][i] = value;
      }
      void SquareMatrixType::Add(SquareMatrixType two,
        SquareMatrixType result)
      {
        int row, col;
      
        for (row = 0; row < size; row++)
         for (col = 0; col < size; col++)
           result.matrix[row][col] = matrix[row][col] +
             two.matrix[row][col];
      }
      
      void SquareMatrixType::Subtract(SquareMatrixType two,
        SquareMatrixType result)
      {
        int row, col;
      
        for (row = 0; row < size; row++)
         for (col = 0; col < size; col++)
           result.matrix[row][col] = matrix[row][col] -
             two.matrix[row][col];
      }
      
      void SquareMatrixType::Print()
      {
        int row, col;
      
        for (row = 0; row < size; row++)
          for (col = 0; col < size; col++)
            cout << matrix[row][col];
      }
      
      void SquareMatrixType::Copy(SquareMatrixType two)
      {
        int row, col;
        for (row = 0; row < size; row++)
          for (col = 0; col < size; col++)
            matrix[row][col] = two.matrix[row][col];
      }
      
    3. This test plan is a black-box strategy with data values representing the end cases and a general case. For matrix operations, the end cases represent the size of the matrices, not the values stored in them. We assume that integer addition and subtraction are correct.

    Operation to Be Tested and Description of Action

    Input Values

    Expected Output

    MakeEmpty

       
    • execute and print

    2

    0 0

       

    0 0

    • execute and print

    0

    No output

    • execute and print

    50

    50 × 50 matrix of zeros

    StoreValue

       
    • MakeEmpty (2)

       
    • store

    1, 1, 2

     
    • store

    1, 2, 3

     
    • store

    2, 2, 4

     
    • print

     

    2 3

       

    0 4

    Add

       
    • General case

       
      • Create a second matrix

    1 2

     
     

    3 4

     
      • Add to first one created and print

     

    3 5

       

    3 8

    • End case (size 0)

       
      • Add two empty matrices and print

     

    No output

    • End case (size 50)

       
      • Create a 50 × 50 matrix of ones

       
      • Create a 50 × 50 matrix of twos

       
      • Add and print

     

    50 × 50 of threes

    Subtract

       
    • General case

       
      • Subtract the first from the second and print

     

    -1 -1

       

    3 0

    • End case (size 0)

       
      • Subtract two size-0 matrices and print

     

    No output

    • End case (size 50)

       
      • Subtract all ones from all twos

     

    50 × 50 of ones

    Copy

       
    • Copy first and print

     

    2 3

       

    0 4

    • Copy a size-0 matrix and print

     

    No output

    • Copy a size-50 matrix of twos and print

     

    50 × 50 of twos

    1. RelationType StrType::ComparedTo(StrType otherString)
      {
        int result;
      
        result = strcmp(letters, otherString.letters);
        if (result < 0)
          return LESS;
        else if (result > 0)
          return GREATER;
        else return EQUAL;
      }
      
    2. RelationType StrType::ComparedTo(StrType otherString) const
      {
        int count = 0;
        bool equal = true;
      
        while (equal && letters [count] ! = '\0')
        if (letters[count] != otherString.letters[count])
          equal = false;
        else
          count++;
        if (otherString.letters[count] == '\0' && equal)
          return EQUAL;
        else if (equal) // More characters in otherString
          return LESS;
        else if (letters[count] < otherString.letters[count])
          return LESS;
        else return GREATER;
      }
      
  4. void StrType::CopyString(StrType& newString)
    {
      int count = 0;
    
      do
      {
        newString.letters[count] = letters[count];
        count++;
      }
      while (letters[count-1] != '\0');
    }
    


Previous Section
 < Free Open Study > 
Next Section
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)