You are on page 1of 6

UNIVERSITY OF THE FREE STATE

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS


CSIS 2614 & CSIQ 2614

MODULE TEST 1

DATE: 16 April 2021 MARKS: 100


ASSESSOR: Prof. P.J. Blignaut
MODERATOR: Prof. T.R. Stott TIME: 3 hours

 This is a limited open-book test. You may use ONLY the text book by Nakov. No
other sources are allowed. Internet access will not be available for the duration of
the assessment.
 You may use the tools available in Visual Studio such as IntelliSense.
 No mobile computing devices may be active for the duration of the assessment
(Mobile phones, tablets, etc).

Question 1 (Recursion, Delegates, Extension methods) [20]

Consider the given partial solution of a program that will allow the user to select an option and then
convert a value from degrees to radians or vice versa.

 Make sure that you enter your student number and name at the top of every file.
 You may not change the Main method in any way.

1.1 Develop an extension method to round a double value to a given number of decimals. For
example, if d = 16.09344, d.Round(1) should return 16.1. (5)
1.2 Develop a method, GetValue, that will recursively prompt the user until the user enters a
valid value. (6)
1.3 Declare a delegate, Fn, that will take a double parameter and return a double number. (3)
1.4 Complete the method, Convert, that will be called from the Main method. If the number of
digits (last parameter) is not specified, it is assumed to be 2. The method should display the
converted value as shown in the screen print below. Use the extension method to round the
converted value to the given number of decimals. (6)

Menu

Option A

Option B

1
Question 2 (Operator overloading, Events) [40]

Develop a class, Hex, that can be used to encapsulate operations with hexadecimal numbers. (1)

2.1 The class must have a private string field for the hexadecimal number. (1)
2.2 The constructor will assign a value to the private data field through a parameter. (2)
2.3 Override the ToString method to return the private data field. (2)
2.4 Provide a public method that will return the equivalent integer value. A hexadecimal number
can be converted to its integer equivalent through the method Convert.ToInt32(string
value, int fromBase). (2)
2.5 Provide a static method to return an instance of Hex which is the equivalent of an integer
value. An integer can be converted to hex through the ToString("X") method. (4)
2.6 Provide a static operation for the hexadecimal + operator. (7)
Hint: Convert the hex numbers to int, add, and then convert back to hex.

2.7.1 In the constructor, invoke an event with a message when the instantiation of a new instance
cannot be done. This will occur when (4)
2.7.2 an empty string is provided (3)
2.7.3 or when any of the characters is not in the interval '0'..'9', 'A'..'F'. (8)
2.7.4 In case of an invalid string, revisit Question 2.2 and ensure that no object is instantiated. (2)
2.7.5 In the Main method, subscribe an event handler to the event. The event handler should
display the message. (4)

Test your class with the given Main method. You may
not change the Main method (except for question
2.7.5), but you may comment out parts that you do not
get to run.

Question 3 (Container classes, Searching and Sorting) [40]

Consider the given partial solution of a generic container class that will allow the class user to search
and sort elements in the container. To test the generic container, a class CStudent is provided. The
Main method is also provided. You may not change the Main method, but you may comment out
parts that you do not get to run.

In the container class:


3.1 Add an Add method that will be required by Line 28 in the Main method. (4)
3.2 Add an indexer that will be required by Lines 31 and 36 in the Main method. (6)
3.3 Add a GetEnumerator method that will be used by the foreach statement in Line 42 of
the Main method. (6)
3.4 Complete the InsertionSort method to sort the data in the list on the given sort field.
Note 1: A helper method, GetProperty, is provided to obtain the property object by the
given name.
Note 2: If you cannot do it, you can opt to lose marks and use the following code to get your
program running and continue with the next question. (12)
lstElements = (from element in lstElements
orderby GetProperty(element, sortField)
select element).ToList();
3.5 Complete the FindBinary method to find a specific data element based on a search field
and a value. The method should make use of a binary search algorithm. (12)

Submission:
 Zip all your answers into a single file and submit on Blackboard.
2
MEMORANDUM

Question 1 (20)

1.1 public static class Extensions


{
public static double Round(this double d, int n = 2) 
{
return Math.Round(d, n); 
}
} //class Extensions

1.2 private static double GetValue()


{
Console.Write("\tValue to convert: "); 
if (double.TryParse(Console.ReadLine(), out double val)) 
return val; 
else
return GetValue();
} //GetValue

1.3 public delegate double Fn(double val1); 

1.4 private static void Convert(double val, Fn F, int n = 2) 


{
Console.WriteLine("\tConverted value : " + F(val).Round(n) ); 
Console.Write("\n\tPress any key to return to the menu ...");
Console.ReadKey();
} //Convert

3
Question 2 (40)
//2.7.1
public delegate void delInvalid(string msg); 

public class Hex 


{
//2.1
private string Value; 

//2.7.1
public static event delInvalid Invalid; 

//2.2 Constructor
public Hex(string value)
{
//2.7.2 Check for empty string
if (string.IsNullOrEmpty(value)) 
{
Invalid?.Invoke("String is empty"); 
return;  (2.7.4)
}
//2.7.3 Check that all characters are valid
bool isValid = true; 
string validChars = "0123456789ABCDEF"; 
for (int c = 0; c < value.Length; c++) 
if (!validChars.Contains(value[c].ToString()))
isValid = false;
if (!isValid)
{
Invalid?.Invoke("\"" + value + "\" contains invalid characters"); 
return;  (2.7.4)
}
//2.2 All good, proceed
Value = value; 
} //Constructor

//2.3 (2)
public override string ToString()
{
return Value; 
}

//2.4 (2)
public int ToInt()
{
return Convert.ToInt32(Value, 16); 
}

//2.5 (4)
public static Hex IntToHex(int i) 
{
Hex h = new Hex(i.ToString("X")); 
return h;
}

//2.6 (7)
public static Hex operator +(Hex h1, Hex h2) 
{
int i = h1.ToInt() + h2.ToInt();
return IntToHex(i); 
}
} //class Hex

4
//2.7.5
class Program
{
static void Main(string[] args)
{
Hex.Invalid += Hex_Invalid; 
...

} //Main

private static void Hex_Invalid(string msg) 


{
Console.WriteLine("\t" + msg); 
} //Hex_Invalid
} //class

5
Question 3 (40)
//3.1 Add (4)
public void Add(T element) 
{
lstElements.Add(element); 
Count++; 
} //Add

//3.2 Indexer (6)


public T this[int i] 
{
get { return lstElements[i]; } 
set { lstElements[i] = value; } 
} //Indexer

//3.3 Enumerator (6)


public IEnumerator<T> GetEnumerator()
{
foreach (T element in lstElements) 
yield return element; 
} //GetEnumerator

//3.4 InsertionSort
public void InsertionSort(string sortField)
{
int j; 
T temp; 
for (int i = 1; i < lstElements.Count; i++) 
{
temp = lstElements[i]; 
j = i; 

string temp_ = GetProperty(temp, sortField).ToString();


while (j > 0 && GetProperty(lstElements[j - 1],
sortField).ToString().CompareTo(temp_) > 0) 
{
lstElements[j] = lstElements[j - 1]; 
j -= 1; 
} //while
lstElements[j] = temp; 
}
} //InsertionSort

//3.5 Binary search (12)


public T FindBinary(string searchField, string value)
{
int lower = 0, upper = lstElements.Count - 1,
mid = (lower + upper) / 2; 
bool isFound = false; //1
while (lower <= upper && !isFound) 
{
if (value.CompareTo(GetProperty(lstElements[mid],
searchField).ToString()) < 0)
upper = mid - 1; 
else if (value.CompareTo(GetProperty(lstElements[mid],
searchField).ToString()) > 0)
lower = mid + 1; 
else
return lstElements[mid]; 
mid = (lower + upper) / 2; 
} // while (lower < upper && !isFound)
return default;
} //FindBinary

You might also like