2020 Test 1
2020 Test 1
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).
Permutations are the possible combinations of a series of values. For example, for the
characters "abc", the permutations are "abc", "acb", "bac", "bca", "cab" and "cba".
interface IPermutations<T>
{
void Swap(ref T First, ref T Second);
void PrintPermutations(T[] list, int j, int k);
}
1.2 Within the class CPermutations, implement the method Swap to swap two values.
Note that the method should be generic and be able to swap values of any type. (5)
1.4 In the Program class, add a private generic method called SelectionSort, to sort
the elements in a generic array in ascending order. (15)
Hint: You cannot use > or < for generic types. Limit T to inherit from IComparable
and then use the CompareTo method.
1
Use the following code to test your application. You may not change this code but you may
comment out lines that you cannot get to work.
//Write elements
Console.WriteLine("\tNumbers");
Console.Write("\t");
foreach (int v in nums)
Console.Write(v + ", ");
//Get permutations
Console.Write("\n\n\tThe permutations of the numbers are: \n\t");
CPermutations<int> permNums = new CPermutations<int>();
permNums.PrintPermutations(nums, 0, nums.Length - 1);
//Write elements
Console.WriteLine("\n\n\tCharacters");
Console.Write("\t");
foreach (char v in chars)
Console.Write(v + ", ");
//Get permutations
Console.Write("\n\n\tThe permutations of the characters are: \n\t");
CPermutations<char> permChars = new CPermutations<char>();
permChars.PrintPermutations(chars, 0, chars.Length - 1);
2
Question 2 (Operator overloading) [22]
The built-in class double does not have a binary operator to raise a number to a specific
power. Create a new class real that will behave similar to double but also include a
binary operator ^ that can be used in, for example 23 = 8, written as 2 ^ 3. (1)
2.1 A private date member, value (type double), to contain the value of the number. (1)
2.2 A constructor (2)
2.2 An implicit operator to cast double to real. (5)
2.3 An explicit operator to cast real to double. (5)
2.4 An overloaded operator ^. The signature must contain a real parameter, num, and a
double exponent, e. You can use the following code to calculate the power:
2.5 Override the default ToString method to return the value displayed as a string. (3)
You may test your class with the following Main method:
z = x ^ (double)y;
Console.WriteLine(x + " ^ " + y + " = " + z.ToString());
3
Question 3 (Delegates and events) [64]
Consider the scenario of a messaging service with three classes, namely a client, server
and a program that manages the first two. The server class serves as a container for
clients and may connect to many clients. Every client connects to a single server.
Use the following delegate declaration and interfaces. You may not change this code but
you may comment out lines if you do not get the code to work.
The Server class acts as a container class for clients and contains a private list
List<Client> lstClients. Events must be triggered in the RegisterClient,
UnregisterClient and BroadcastMessage methods. See the output below for what is
expected. In the RegisterClient method, add the client to a private list and invoke an
event that a client with the specific name is registered. Invoke an alternative event if the
client is already registered. When unregistering a client, remove the client from the list
and dispose it. Again, invoke applicable messages if the client was successfully removed
or not. The Server class may not contain a reference to the System namespace. The
server class must also contain a GetEnumerator method and an indexer. (41)
In the Client class, register a client with the server (call server.RegisterClient) and
subscribe to the server event when a new client is instantiated. All events must be
handled in the Client class (display the server message). Unsubscribe when a client is
disposed. (23)
Test your program with the following main method. You may not change this code but
you may comment out lines.
4
class Program
{
static void Main()
{
//Black text on white background
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
Console.WriteLine();
//Register server
Server server = new Server();
//Unregister clients
Console.WriteLine("\n\tUnregister clients");
server.UnregisterClient(client2);
server.UnregisterClient(client2);
//List clients
Console.WriteLine("\n\tList of clients");
foreach (Client client in server)
Console.WriteLine("\t" + client);
Console.WriteLine();
//Find client
Console.WriteLine("\tFind client");
Console.WriteLine("\t" + server["123"]);
} //Main
} //class
Marks allocation
class Server 1 class Client 1
Members 4 Members 3
Constructor 2 Constructor 9
GetEnumerator 5 Dispose 3
Indexer 6 ToString 3
RegisterClient 9 OnMessage 4
UnregisterClient 10
BroadcastMessage 4
5
POSSIBLE SOLUTIONS
Question 1 (38)
interface IPermutations<T>
{
void Swap(ref T First, ref T Second);
void PrintPermutations(T[] list, int j, int k);
} //interface IPermutations
} //class CPermutations
//Swap
T temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
} //for i
return arr;
} //SelectionSort
6
Question 2 (22)
class real
{
private double value;
//Constructor
public real(double number_)
{
value = number_;
}
//Override ToString
public override string ToString()
{
return value.ToString();
}
} //real
7
Question 3 (64)
public Server()
{
lstClients = new List<Client>();
} //Constructor
} //class Server
} //class