You are on page 1of 4

Assignment 3

DIRECTIONS: Type all answers. Keep a copy of your work. Include your name, student number, course number,
and course code on every page. Short answer questions are 4 points each. The programming assignment is 20
Week 7
7.1 What is encapsulation?
Object-oriented languages consists of data together with the procedures to operate on that data. Encapsulation
refers to the fact that an objects data can be accessed only through the methods (procedures) provided.
Encapsulation organizes programs into a collection of logically related code and data.
What is information hiding?
Information hiding is a key concept in data abstraction, that is, the details are hidden to include only the most
significant attributes. Information hiding is the ability to prevent certain aspects of the data from being accessible to its
Does every C++ class necessarily follow these two principles?
Yes, a class in C++ contains data members and member functions that are only accessed by instances of that class.
That is how a class encapsulates the data and these classes can hide the information with the private clause.
7.2 Compare the following languages in terms of how well they provide encapsulation and information hiding: Ada,
C++, C#.
Ada was one of the first languages to provide support for abstract data types. The encapsulating constructs are called
packages. Even though Ada provides these constructs to simulate data abstraction, C++ supports data abstraction
more directly.
C++ provides two constructs that support abstract data types, Classes and Structs. Data can be accessed through
instances of a class. Member functions can be private or protected to hide the information.
C# is based on both C++ and Java and includes some new constructs, internal and protected internal. One of the
differences between C# and C++ data abstraction constructs is the struct that in C# does not support inheritance.
Also structs in C# are value types instead of reference types.
7.3 How do Ada and C++ allow for generic abstract data types? How are their implementations different?
Abstract data types such as classes in C++ are types in contrast with Ada abstract data types packages are more
generalized encapsulations that can define any number of abstract data types. A program that gains visibility to an
Ada package can access any of its public entities directly by their names. In C++ that only happens thru an instance
of the class.
Week 8
8.1 What is the difference between physical and logical concurrency?
Physical concurrency is given the fact that more than one processor is available, several program units from the
same program execute simultaneously. Logical concurrency is the concept that allows the programmer and the
application software to assume that there are multiple processors providing concurrency, but the actual execution of
program takes place in a single processor.

8.2 What is a task? What are the different types of synchronization?

A task is a unit of a program, similar to a subprogram, that can be in concurrent execution with other units of the
same program. There are two types of synchronization; Cooperation, when a task must wait for another to complete a
specific activity. Competition is when two tasks require the use of the same resource that cannot be simultaneously

8.3 Suppose a certain program called keepScore has two tasks that both access the same variable, score, with an
initial value of 10. Task A contains the line:
score = score + 2;
Task B contains the line:
score = score - 2;
If these tasks are executed concurrently, it is possible the final value of x will not be 10. Explain. Show or describe
how you could use semaphores to avoid this situation.
Without Competition Synchronization different values are possible. This situation is called race condition because
two or more tasks are racing to use the same resource. Semaphore is a mechanism that can be used to provide
synchronization to tasks to avoid these problems. Using a semaphore to provide limited access to the variable score
placing guards around the code to allow the execution only when a condition is true.
8.4 Show or describe how you could use monitors to avoid the problem in the keepScore program of the previous
By using Monitors in a concurrent environment we encapsulate share data structures with their operations and hide
their representations. This solution can provide competition synchronization without semaphores by transferring the
responsibility to the run-time system.
8.5 How is cooperation synchronization provided for Ada tasks?
Cooperation synchronization in Ada is provided with accept and when clauses. We could use these clauses to create
guards around the data to provide access only when that data is open.
8.6 Describe how exceptions are raised and handled in languages without built-in support for exceptions. What are
the weaknesses of this approach?
For languages without built-in exception handling, user-defined software-detected exceptions could be implemented.
This kind of detection within a program unit is called the units caller or invoker.
Without exception handling the code required to detect error conditions can considerably clutter a program, since
many conditions have to be implemented.
8.7 Compare the exception handling style of C++ with that of Ada. Which is more flexible? Which is easier to read?
Is one style more likely to lead to reliable programs?
Ada - Exception handlers are usually local and do not required parameters. Ada uses program units or blocks to
specify the scope for exception handler. Handlers can be included in blocks or in subprograms, packages or tasks.

C++ - One major difference between exception handling in C++ and Ada is the absence of predefined exceptions in
C++ (other than its stand libraries). C++ exception are user or library-defined and explicitly raised. A special construct
with the reserved word try is used to specify the scope of exception handlers. Another reserved word catch is used
as an exception handler.
It looks like C++ is more flexible because libraries can be used to extend the language. It is also easy to read since it
uses a simple construct with the use of the reserved words try and catch.

8.8 What aspects of Java's exception handling are an improvement over C++?
The Javas exception handling is based on C++, but it is designed to be more in line with the object-oriented
language paradigm. Java also includes a collection of predefined exceptions that are implicitly raised by the Java
Virtual Machine.

Programming Assignment
IMPORTANT: This problem is a complete programming problem and will be graded according to the Guidelines for
Grading Programming Assignments found in your Student Handbook.
Write a program that determines the offset address for an n-dimensional array (0 < n <= 4). Assume an element size
of 2. The program should prompt the user to enter the following data:
row-major order or column-major order (R or C)
the number of dimensions for the array (a number)
the upper and lower bounds for each subscript
a set of subscripts
The program should output the total size of the array and the address offset for the selected subscript combination.
Allow the user to enter sets of subscripts until a -1 is entered. Terminate the program when a subscript of -1 is
entered. For example:
Enter (R)ow or (C)olumn major order (R or C): R
Enter the number of dimensions for the array: 3
Enter the upper and lower bounds for subscript 1: 1 4
Enter the upper and lower bounds for subscript 2: 1 3
Enter the upper and lower bounds for subscript 3: 2 6
Enter a set of subscripts: 2 3 4
Offset to 2, 3, 4 is: 54
Array size is
: 120
Enter a set of subscripts: -1 -1 -1
Successful termination of program
Mr Webb I wasnt able to complete the programming assignment as described in C++. I got confused when
combining multidimensional dynamic arrays, so I implement the with a fixed muti-dimentional to demonstrate Roll vs
Column major. The code is as follows:

#include <iostream>
using namespace std;
int main()
int a[5][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9,10},

// Row major order
for ( int i = 0; i < 5; i++ ) {
int sum = 0;
for ( int j = 0; j < 5; j++ )
sum += a[i][j];
cout<< sum <<'\t';
// Column major order
for ( int i = 0; i < 5; i++ ) {
int sum = 0;
for ( int j = 0; j < 5; j++ )
sum += a[j][i];
cout<< sum <<'\t';