Answers to Selected Exercises Chapter 1



Download 0.49 Mb.
Page1/7
Date18.07.2017
Size0.49 Mb.
#23634
  1   2   3   4   5   6   7

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 Description of Action

Input Values

Expected Output

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);

// Post: self = two

private:


int size; // Dimension of the matrix.

int matrix[10][10];

};

(c)


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)



{

for (int row = 0; row < size; row++)

for (int col = 0; col < size; col++)

result.matrix[row][col] = matrix[row][col] + two.matrix[row][col];

}

void SquareMatrixType::Subtract(SquareMatrixType two, SquareMatrixType result)



{

for (int row = 0; row < size; row++)

for (int col = 0; col < size; col++)

result.matrix[row][col] = matrix[row][col] - two.matrix[row][col];

}

void SquareMatrixType::Print()



{

for (int row = 0; row < size; row++)

for (int col = 0; col < size; col++)

cout << matrix[row][col];

}

void SquareMatrixType::Copy(SquareMatrixType two)



{

for (int row = 0; row < size; row++)

for (int col = 0; col < size; col++)

matrix[row][col] = two.matrix[row][col];

}

(d)


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

execute and print

execute and print


2
0


50

0 0


0 0

no output

50x50 matrix of zeros


StoreValue

MakeEmpty(2)

store

store


store

print


1,1,2


1,2,3

2,2,4

2 3

0 4


Add

General Case

Create a second matrix
Add to first one created and print
End Case (size 0)

Add two empty matrices and print

End Case (size 50)

Create a 50x50 matrix of ones

Create a 50x50 matrix of twos

Add and print



1 2


3 4

3 5

3 8
no output


50x50 of threes



Subtract

General Case

Subtract the first from the second and print

End Case (size 0)

Subtract two size 0 matrices and print

End Case (size 50)

Subtract all ones from all twos





-1 -1


3 0
no output
50x50 of ones

Copy

Copy first and print

Copy a size 0 matrix and print

Copy a size 50 matrix of twos and print





2 3


0 4

no output

50x50 of twos

31. If functions GetMonthAsString and Adjust are called frequently, then the first version is more efficient because access to the string or number of days would be O(1). On the other hand, if these functions are called only once, it would be better to calculate these values and store them. The second alternative would require two new class variables, one to hold the string and one to hold the number of days, but the auxiliary arrays would no longer be need.




Download 0.49 Mb.

Share with your friends:
  1   2   3   4   5   6   7




The database is protected by copyright ©ininet.org 2024
send message

    Main page