Professional Documents
Culture Documents
MODULE TEST 1
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).
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.
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.
Submission:
Zip all your answers into a single file and submit on Blackboard.
2
MEMORANDUM
Question 1 (20)
3
Question 2 (40)
//2.7.1
public delegate void delInvalid(string msg);
//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
5
Question 3 (40)
//3.1 Add (4)
public void Add(T element)
{
lstElements.Add(element);
Count++;
} //Add
//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;