You are on page 1of 8

UNIVERSITY OF THE FREE STATE

BLOEMFONTEIN & QWAQWA CAMPUSES

CSIS1624 & CSIQ1624


DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS
CONTACT NUMBER: 051 401 2754 / 058 718 5217

MAIN EXAMINATION: 2018

ASSESSORS: 1. MR D.J. WIUM


2. MR M.B. MASE

MODERATOR: 1. PROF. T.R. STOTT

DATE: 19 November 2018


TIME: 4 Hours MARKS: 140

SECTION A (20)
 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. For each code snippet, write down the number that will be printed to the console window:
1.1. int iSum = 0;
for (int i = 1; i <= 4; i++)
iSum += i;
Console.Write(iSum.ToString()); (2)

1.2. int iSum = 0;


for (int i = 1; i <= 4; i++)
if (i % 2 == 0)
iSum++;
Console.Write(iSum.ToString()); (2)

1.3. int iSum = 0;


int[] numbers = {-3, 4, -1, -7, 2, 8};
foreach (int i in numbers)
if (i < 0)
iSum++;
Console.Write(iSum.ToString()); (2)

1
1.4. int iSum = 0;
int[] numbers = {-3, 4, -1, -7, 2, 8};
foreach (int i in numbers)
if (i < 0)
iSum += i;
Console.Write(iSum.ToString()); (2)

1.5. int iSum = 0;


int[] numbers = {-3, 4, 3, -7, 2, 8};
int i = 0;
while (iSum < 3)
{
iSum += numbers[i];
i++;
}
Console.Write(iSum.ToString()); (2)
[10]

2. A two-dimensional array has been instantiated with the following line of code, where i and
j are two integer variables that do not necessarily contain different values:
int[,] numbers = new int[i,j];
Afterwards, all of the elements in this array were assigned values. Write a code snippet that
will first determine whether this array is square (whether it has an equal number of rows
and columns), and if it is square, will calculate the sum of the values on its main diagonal
(the main diagonal are the elements [0,0], [1,1], [2,2], etc.) and print it to the console
window. Keep in mind that this may be a very large array and that you thus want to do the
minimum amount of computations. Inefficient solutions will not be awarded full marks.
[5]

3. You are provided with an integer array called numbers that contains an unknown number
of integer values. Write a code snippet that will calculate the smallest value in numbers
and print it to the console window. You are not allowed to make any method calls in your
code snippet. [5]

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

2
SECTION B (120)
 Remember to add a comment block containing your student number, name and surname,
today's date and the name of the question at the top of every file. Marks will be deducted
for each file where the comment block is missing.
 Adhere to all programming and naming conventions as taught and ensure that your code
is aligned properly. Only employ defensive programming when asked to do so.
 This is not an open-book test.
 Save all your projects on the T: drive.

Question 1 (25)

Use Visual Studio to create a console application and name your application Question 1.
This application will allow two players to select hidden numbers from a grid with the player
accumulating the highest total winning the game.
Below are two screen prints that illustrate the beginning of the game and the end of the game.
Much of the code is provided in the ProvidedCode.txt file on Blackboard. Download the file
and copy the relevant code into the Program.cs class of your project. Then complete the
missing code according to the instructions that follow.

1. Add a class for the players of the game and name it CPlayer.cs. This class should contain
the following:
 A property named Board that belongs to the class and is used to store the values for
each of the squares. The size of Board is 4 × 4.
 A property named Score that represents the score of the player.
 A read-only property named Locations that is used to store all the locations on Board
that belongs to the player.
 A method named PlayMove() that accepts the location where the player selects to play
next as a parameter. This method adds this location to Locations and adds the value
of that location to the player’s Score.
When a new player is instantiated, its Score should be set to zero and its Locations to
an empty collection according to the data type that you chose for this property. (11)

3
2. Implement the GenerateBoard() method. It should assign a random value from zero to
99 to each of the squares of Board. (5)
3. Implement the DisplayBoard() method. It should display Board in the console window as
illustrated in the screen prints. Values of squares selected by Player 1 are displayed within
round brackets ( ), while values of squares selected by Player 2 are displayed within
square brackets [ ]. Squares that have not yet been selected are displayed as “----“. Note
that all values should be right-aligned (see for example the value 7 in row 3, column 4 in
the second screen print). (9)

4
Question 2 (25)
Use Visual Studio to create a console application and name your application Question 2.
Copy and paste the relevant code from ProvidedCode.txt into the Main() method of this
application.
The purpose of the application is to provide two classes, one for integer numbers and one for
decimal numbers (all non-integer real numbers). The screen print below is an illustration of
the output that is expected.

1. Add a class named CNumber.cs of which no object can be instantiated. This class should
contain an integer property named IntegerPart for the integer part (the part before the
decimal point) of a number and it should override the ToString() method so that it returns
this property, converted to a string. (4)
2. Add a class named CInteger.cs, used as a template for integer numbers, that inherits from
CNumber.cs. (13)
 This class should contain a read-only property named Factors that stores all the factors
of the integer.
 Furthermore, it should contain a method named GetFactors() that should return all the
factors of the integer. Remember that the modulus of a number and one of its factors is
zero. To score full marks, only test all numbers up to the square root of the integer in
order to determine all its factors. If all numbers up to the integer itself are tested, two
marks will be forfeited.
 When a new integer is instantiated, both of its properties should be assigned. Factors
should be assigned an object returned by GetFactors().
3. Add a class named CDecimal.cs, used as a template for decimal numbers, that inherits
from CNumber.cs. (8)
 This class should contain an integer property named DecimalPart for the decimal part
(the part after the decimal point) of the number.
 When a decimal number is instantiated, both of its properties should be assigned.
 The ToString() method should be overridden by calling the version in the base class
and adding what is necessary to yield the appropriate output, as shown in the screen
print.

5
Question 3 (40)
Use Visual Studio to create a Windows Forms application and name your application
Question 3. This application calculates and displays a multiplication table for a user-selected
integer, for all values from one up and to another user-selected integer. The screen print below
is an example of what is expected.

1. Your application will require two collections declared on class level, one for the range of
numbers with which the chosen number is multiplied (1 – 12 in the screen print) and another
for the answers (7 – 84 in the screen print). The number for which the multiplication table
is generated (7 in the screen print) should also be declared on class level, along with the
name of the file to which tables are to be saved to and loaded from, which is “Table.txt”.
Add using directives for all namespaces that might be required. (3)
2. When the Generate button is clicked, the two collections mentioned in step 1 should be
populated with the numbers and answers. The table should be displayed in the list box by
calling a method named DisplayTable(), which will be implemented in step 3. The Enabled
property of both numeric up-down controls should be set to false. (6)
3. Write a method named DisplayTable() that does not receive any parameters and does not
return anything. This method should clear the list box and then display the multiplication
table using the collections and number mentioned in step 1. (5)
4. When the Clear Table button is clicked, both collections named in step 1 should be cleared
along with the items of the list box. The Enabled properties of both numeric up-down
controls should be set to true. (3)
5. When the Save Table button is clicked, the current multiplication table should be saved to
“Table.txt”. Ensure that your application does not crash if this fails and display a message
to tell the user whether the table was saved successfully. If the file already exists, it should
be overwritten. (9)

6
6. When the Load Table button is clicked, the multiplication table stored in “Table.txt” should
be loaded and the collections and number mentioned in step 1 should be assigned the
values that are loaded. The loaded multiplication table should be displayed in the list box
and the numeric up-down controls’ values should match the displayed table. Their Enabled
properties should be set to false. Ensure that your application does not crash during the
course of this step and display a message informing the user whether the loading of the
table was successful. (14)

7
Question 4 (30)
Create a console application using Visual Studio and name your project Question 4. In this
application, you have to re-design an existing project according to the instructions that follow.

CHuman
+Work()
+Sleep()

The class diagram above shows the original design of the project, which models the work and
sleep behaviours of a human being. However, one member of the project team made a
startling discovery, namely that not all human beings exhibit the same work and sleep
behaviours. She found the following:
 Students sleep peacefully, but lecturers are unable to sleep because they are always
working.
 Lecturers have no problem working, but students are unable to perform work because they
are constantly sleeping.
It is your job as software developer to fix the design in order to accommodate this new
discovery as well as future discoveries that may follow. Ensure that your design adheres to
the instructions that follow. The code for your Main() method is provided in the
ProvidedCode.txt file. Copy it into your Main() method and leave it unchanged. The screen
print above illustrates what your output should be.
1. Remove the two behaviours from the CHuman class. Create an interface for each that can
be implemented by concrete behaviours. Add classes for the concrete behaviours,
implementing them in such a way that the desired output is produced. Make the return type
of all methods void and print the output to the console window by using
Console.WriteLine() statements. (16)
2. Add concrete classes for the different types of human beings mentioned here and ensure
that they inherit from a CHuman class of which no objects can be instantiated. As can be
deduced from the code in the Main() method, every human should contain methods that
allows it to execute the appropriate sleep and work behaviours, which has to be assigned
when the human being is instantiated. The execution of these behaviours should be
delegated to the appropriate elements among those created in step 1. (14)

Submission Procedure for Section B

1. Create a folder on the T: drive and rename it to your student number.


2. Move all your project folders to the folder you created in step 1.
3. Create a zip file of the folder you created in step 1 (right-click on the folder, then select
send to  Compressed (zipped) folder).
4. Submit the zip file you created in step 3 under the Assessment Archive  Main
Examination link on Blackboard.

You might also like