Answers to Selected Exercises

Also, some concepts that were to be learned in preparation for the next .... The
following declaration contains only additional pre/post conditions required by the
implementation. .... Function: Deletes the element whose key matches item's key.

Part of the document


Answers to Selected Exercises
Chapter 1 Many of the questions in this chapter's exercise are "thought
questions." The answers given here are typical or suggested
responses, but are not the only possible answers. 1. Software engineering is a disciplined approach to the creation
and maintenance of computer programs throughout their whole life
cycle.
4. Some software tools used in developing computer programs are
text editors, compilers, assemblers, operating systems, and
debugging programs.
7. Goal 4 says, "Quality software is completed on time and within
budget."
(a) When a student does not complete a programming assignment
on time, it usually hurts his or her grade. Also, some concepts
that were to be learned in preparation for the next programming
assignment may not be mastered in time to be put into practice.
(b) When a team developing a highly competitive new software
product does not complete its work on time, the company must
continue to pay salaries, office rent, etc., increasing the
"development cost" of the product. The product may go on the
market late, after competing products have been released,
hurting its sales. This may have an effect on the team members'
professional evaluations (like a student's grade).
10. An object is an individual while a class is a description
of a group of objects with similar properties and behaviors.
Labrador dogs is an example of a class and Maggie is an example
of an object.
14. An expert understanding of your programming language saves
you time and debugging because (a) you can avoid syntax errors,
(b) you can more quickly identify and correct those syntax
errors that occur, and (c) you can avoid errors caused by a
misunderstanding of the way the language works.
17. The body of the While loop is not in brackets.
The comments include the call to Increment.
The parameter to Increment is not a reference parameter.
20. Unit testing is the testing of a single unit of the
program (for instance, a function). Integration testing is the
testing of groups of already tested units to make sure that they
interact correctly and that the whole program works according to
its specification.
23. (a) The functional domain consists of the whole numbers
from 0 to 100.
(b) It is possible to carry out exhaustive data coverage for
this program.
(c) Devise a test plan for this program.
Input: All values from 1 to 100.
Expected Output: For input 0-59 F
For input 60-69 D
For input 70-79 C
For input 80-89 B
For input 90-100 A
26. Life-cycle verification refers to the idea that program
verification activities can be performed throughout the
program's life cycle, not just by testing the program after it
is coded.
29. (a) num and denom are both just integer values. Each can
be either negative or positive. In a fraction the numerator
carries the sign. The denominator should always be positive.
(b) IsNotProper needs to be changed to use the absolute value
of the numerator. Function Initialize (or any constructor)
should check to be sure the denominator is strictly positive and
throw an exception if this is not the case.
(c) The following tests need to be added to the test plan.
|Operation to Be Tested and |Input |Expected Output |
|Description of Action |Values | |
|Initialize |-3,4 |Numerator: -3 |
| | |Denominator: 4 |
|IsNotProper | |Fraction is proper |
|Initialize |-13,4 |Numerator: -13 |
| | |Denominator: 4 |
|IsNotProper | |Fraction is improper |
|ConvertToProper | |Whole number is 3 |
| | |Numerator is -1 |
| | |Denominator is 4 |
Chapter 2 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 is encapsulated
through a set of operations specified to create, access, and
change the data. Data encapsulation is accomplished through a
programming language feature.
5. array, struct, union, and classes
8. The syntax of the component selector is the array name followed
by the index of the desired item:
array-name[index-expression]
11. (a)
typedef WeatherType WeatherListType[12];
WeatherListType yearlyWeather;
(b) yearlyWeather[6].actualRain = 1.05;
(c) 238
14. Member-length-offset table for StudentRecord.
|Field |Length |Offset |
|firstName |10 |0 |
|lastName |10 |10 |
|id |1 |20 |
|gpa |2 |21 |
|currentHours |1 |23 |
|totalHours |1 |24 |
17. (a) A square two-dimensional array
(b) struct or class
(c) struct or class containing two data members: the number of
quotations and an array of strings that holds them
(d)a one-dimensional integer array
(e) a two-dimensional integer array
(f) a three-dimensional integer array
(g) an array of structs or classes
(h) a one-dimensional integer array
20. The members of a class are private unless specified as
public. Client code cannot access private members.
23. Classes can relate to one another through inheritance,
containment (composition), and not at all.
28. (a)
SquareMatrix ADT Specification
Structure: An NxN square integer matrix.
Operations :
MakeEmpty(int n)
Function: Initializes the size of the matrix to n
and sets the values to zero.
Precondition: n is less than or equal to 10.
Postconditions: Matrix contains all zero values.
StoreValue(int i, int j, int value)
Function: Stores value into the i,jth position in
the matrix.
Preconditions Matrix has been initialized; i and j
are between 0 and the size minus 1.
Postconditions: value has been stored into the i,jth
position of the matrix.
Add(SquareMatrixType one, SquareMatrixType two, SquareMatrixType
result)
Function: Adds matrix one and matrix two and stores
the result in result.
Precondition: one and two have been initialized
and are the same size.
Postcondition: result = one + two.
Subtract(SquareMatrixType one, SquareMatrixType two,
SquareMatrixType result)
Function: Subtracts two from one and stores the
result in result.
Precondition: one and two have been initialized
and are the same size.
Postcondition: result = one - two.
Print(SquareMatrixType one)
Function: Prints the matrix on the screen.
Precondition: Matrix has been initialized.
Postcondition: The values in the matrix have been
printed by row on the screen.
Copy(SquareMatrixType one, SquareMatrixType two)
Function: Copies two into one.
Precondition: two has been initialized.
Postcondition: one = two.
(b)
The following declaration contains only additional pre/post
conditions 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.
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);
//