Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

UNIVERSITY OF THE FREE STATE

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS

CSIQ1624 & CSIS1624

DATE: 4 September 2017 MARKS: 101


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

SECTION A (17)

 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 test.

1. Consider the following two-dimensional integer array, named A, and answer the
questions that follow:

4 7 3
𝐴 = [8 2 9 ]
6 1 5

1.1. The value of A[1,0] is 8. The values of A[2,0] and A[1,2] are ____ and ____
respectively. (2)
1.2. A was instantiated as follows:
A = new int[*,*];
The value of * is ____. (1)
1.3. Write down the declaration of A that preceded the instantiation in 1.2. (1)
1.4. Write a C# code snippet that would print the values of A to a console window in
the same formation as above (without the square brackets), with spaces between
values on the same line. Additionally, the code should calculate the sum of all
the elements of A and display this value as part of a message to the user after the
two-dimensional array has been displayed. (13)
[17]

NB. You must submit Section A before you may switch on the computer to do Section B.

1
SECTION B (84)

 Add a comment block containing your name and student number to every class that you
code in. Marks will be deducted for failure to do so.
 Adhere to all programming principles as taught. Marks will be deducted for neglect of
naming conventions, non-aligned code and inefficient code.

1. Develop a program as in the example that will allow the user to enter a sentence and then
count the number of words in the sentence. Note that words may be separated by any
number of spaces. You may not use the method Split().
Hint: You can count the words by counting the transitions from space to no-space. [12]

2. Develop a console application that will calculate the square roots of integers entered by
the user. The application must first allow the user to specify the length of an array that will
be used to store the integers. Create and call three methods. In first method, the user should
be prompted for each of the integers. The second method should take the array, Numbers,
as parameter and return the square roots of the numbers as another array. The third method
should return void, take both the original numbers and their square roots as parameters
and display both, as in the screen print below. Note that the integers’ values should be in
ascending order. [27]

2
3. Develop a Windows Forms application that stores information about different
toothbrushes. The user must be able to add toothbrushes to the application, which should
keep a list of all toothbrushes added thus far. The information stored for each toothbrush is
its brand name, whether it is an electrical toothbrush, its cost price and retail price.

3.1. Design your form as in the screen print below. (3)

3.2. Add a class named CToothbrush to your project. Add the following to the class: (1)
3.2.1. Properties for the four pieces of information mentioned above. All properties
should be read-write. The set accessor of the retail price’s property should be
private.. Add code to this property to ensure that the retail price cannot be less
than the cost price. If a smaller value is entered, make the retail price equal to
the cost price. Auto-implemented properties may be used. Add private data
fields where necessary and use suitable data types. (10)
3.2.2. A constructor that takes the values for the four properties as parameters and
assigns them to the properties. (4)
3.2.3. A method named GetString that returns the information of a toothbrush as a
string, as shown in the list box in the screen print. Do not spend much time on
the spacing and alignment of the data in the string. (3)

3.3. Add the following code to your form class:


3.3.1. Use a list or array to store all toothbrushes that are added by the user. (1)
3.3.2. Write a method named FilterToothBrushes that will return a list or an array of
all toothbrushes that match the filter specified by the user (by selecting one of
the radio buttons below the list box). You may use any means at your disposal
to do the filtering. (9)

3
3.3.3. Write a method named ListToothBrushes that updates the display of the
toothbrushes in the list box. Call FilterToothBrushes to filter the toothbrushes
that are displayed. The GetString method should be called for each of those
toothbrushes to obtain the string that should be added to the list box. (4)
3.3.4. When the user clicks the “Add” button, the application should attempt to
instantiate a new toothbrush according to the values entered in the text boxes
and the radio button that is checked. Use defensive programming to prevent run-
time errors and update the display in the list box. (7)
3.3.5. Ensure that the display of toothbrushes in the list box is immediately updated
whenever the user selects another filter. Use a single event handler for the three
radio buttons to accomplish this. (3)
[45]

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 / Module Test 1 link on Blackboard.

You might also like