You are on page 1of 4

UNIVERSITY OF THE FREE STATE

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS


CSIQ1624 & CSIS1624

DATE: 18 October 2017 MARKS: 131


ASSESSORS: Mr. M.B. Mase & Mr. D.J. Wium TIME: 180 min
MODERATOR: Prof. P.J. Blignaut

SECTION A (15)
 Answer the following questions on the answer sheet that is provided.
 The computer must be switched off while you are completing Section A.
 This is not an open-book section.

1. Explain the meaning of the following terms:


1.1 Encapsulation (1)
1.2 Polymorphism (2)
2. What namespace must be added to a Visual Studio project to enable usage of the
StreamWriter and StreamReader classes? (1)
3. Differentiate between the File and FileInfo classes. (2)
4. Consider an integer array, A, a generic list of type int, B,and a Random object, r, that
were instantiated as follows:

int[] A = new int[3]; List<int> B = new List<int>(); Random r = new Random();

4.1. Write code that will assign values to all the elements in A using the Next method of r
that takes no parameters and returns integers. (3)
4.2. Write code that will add 5 integers to B using the same method as in 4.1. (3)
4.3. Write code that will swap the last value in A with the last value in B. (3)

[15]
SECTION B (116)
Instructions

 This is an open-book section (Section B). You may use the text book “Be Sharp with C#”
but no other sources, written or electronic, will be allowed.

1. Consider the following character string:

“Learning programming helps kids to gain advantages in thinking, processing and


communicating.”

1.1 Develop a console application and name your project Question 1. Your program
must be generic in the sense that a user must be able to enter any sentence and ask
for replacement of any word(s) with other(s). For instance, the application should
allow the user to replace all occurrences of "programming" with "coding" and "kids"
with "students". Note that there may be any number of words to be replaced.
1.2 Display the total number of replacements. For the example above, there are 2
replacements.

(18)

2. Develop a Windows Forms application and name your project Question 2. Create three
arrays of type double. Do a compile-time initialization and place any 10 doubles, each
rounded to one decimal place, in two of the arrays. The application should store the
product of the two arrays in the third array. Display the arrays in a list box as in the
example. Display all the numbers to two decimal places.
(16)
3. Develop a Windows Forms application and name your project Question 3. The
application should allow the user to enter and save animals’ (Reptiles or Mammals)
details.

3.1 Develop a form as in the example. (3)


3.2 Develop a class CAnimal. The class will be used as base class for all types of
animals and no objects of CAnimal will be instantiated. (2)
3.2.1 Provide a read/write property for the name of the animal. (2)
3.2.2 Provide a protected data member for the age of the animal (int). Provide a
read-only property for this data member. (6)
3.2.3 Provide a public method, SetAge, that will return no value and must be
overridden by sub-classes. The method should have one parameter that will
be used to assign a value to the age of the animal. (3)

3.3 Develop two sub-classes, CReptile and CMammal, that will inherit from
CAnimal. In both sub-classes, override the method SetAge. The methods should
enforce the rules that the age must be more than 0 and that Reptiles should not be
older than 3 years and Mammals should not be older than 5 years. If this is not true,
an age of 0 should be assigned and an appropriate message should be displayed.
(Hint: The MessageBox class is available in the System.Windows.Forms
namespace.) (12)

3.4 In the base class, CAnimal:


3.4.1 Develop a public instance method, Save, that will return a bool value to
indicate if the save operation was successful or not. The method should receive
the file name as a string parameter and save the object's data members to a
binary file. The first character in the file should be a 'R' or 'M' depending on
whether the instance belongs to a CReptile or CMammal sub-class.
Thereafter, the name and age of the animal should be saved. The method
should return false if there was a problem with the save operation or if the
animal’s age is 0. (16)
3.4.2 Develop a static method, Open, that will receive the file name as parameter
and return an object of CAnimal. The method should read the data of an object
from the file. If the first character is a 'R', it should instantiate and return an
instance of CReptile. If the first character is an 'M', it should instantiate and
return an instance of CMammal. In both cases, the name and age of the animal
must be assigned upon instantiation. (18)

3.5 In the form:


3.5.1 In the click event handler of the Save button. Add the code that will allow the
user to save animal information, for instance if one of the radio buttons is
checked, instantiate and save the appropriate animal in “Animal.txt” file.
Display an appropriate message to indicate whether the save operation
succeeded or not. (12)
3.5.2 In the click event handler of the Open button. Add the code that will allow the
user to open the “Animal.txt” file to fill the two text boxes with the animal's
information according to the selected type of the animal. (8)

[116]

Submission Procedure
When you are ready to submit, add all your project folders to a single folder and rename that
folder to your student number. Submit a .zip archive of that folder under the Assessment
Archive / Sick Test link on Blackboard.

You might also like