Question: C# Lab : Your task is to modify the code given below to change the QueueLab class into a generic class. Also add statements to
C# Lab :
Your task is to modify the code given below to change the QueueLab class into a generic class. Also add statements to the TestQueueLab class to create another instance of TestQueueLab. This instance will be able to hold 5 characters. Try to enqueue the letters A, B, C, D, E and F and also dequeue until it is empty.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QueueAssignment
{
class QueueLab
{
private int[] elements; // array that stores queue elements
private int numElements; // number of elements in the queue currently
private int front; // location of the first element in the queue
private int back; // location of the last element in the queue
// constructors
public QueueLab()
: this(10)
{
}
public QueueLab(int QueueSize)
{
if (QueueSize > 0)
{
elements = new int[QueueSize];
}
else
{
elements = new int[10]; // default queue size is 10
}
numElements = 0;
front = 0;
back = -1;
}
public void Enqueue(int enqueueValue)
{
if (numElements == elements.Length) // queue is full
{
throw new FullQueueException("Queue is full, cannot enqueue new element");
}
back = (back+1) % elements.Length;
elements[back] = enqueueValue;
numElements++;
}
public int Dequeue()
{
if (numElements == 0) // queue is empty
{
throw new EmptyQueueException("Queue is empty, cannot dequeue element");
}
int oldFront = front;
front = (front+1) % elements.Length;
numElements--;
return elements[oldFront];
}
}
}
This QueueLab class has two methods: Enqueue and Dequeue. Equeue adds an element at the rear end of the queue if the queue is not full, while Dequeue removes the element at the front of the queue if the queue is not empty.
There are exception classes to handle situations such as trying to enqueue while the queue is full or to dequeue while the queue is empty.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QueueAssignment
{
class FullQueueException : Exception
{
public FullQueueException(string exception)
: base(exception)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QueueAssignment
{
class EmptyQueueException : Exception
{
public EmptyQueueException(string exception)
: base(exception)
{
}
}
}
A TestQueueLab class is created to test our queue implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QueueAssignment
{
class TestQueueLab
{
private static QueueLab intQueue;
static void Main(string[] args)
{
intQueue = new QueueLab(4);
TestEnqueueInt();
TestDequeueInt();
}
static void TestEnqueueInt()
{
int[] testVales = new int[] { 11, 12, 13, 14, 15};
try
{
Console.WriteLine("Enqueue elements");
foreach (var element in testVales)
{
Console.Write("{0} ", element);
intQueue.Enqueue(element);
}
}
catch (FullQueueException exception)
{
Console.WriteLine();
Console.WriteLine("Message: " + exception.Message);
}
}
static void TestDequeueInt()
{
try
{
Console.WriteLine("Dequeue elements");
int dequeueValue;
// remove all elements from queue
while (true)
{
dequeueValue = intQueue.Dequeue();
Console.Write("{0} ", dequeueValue);
}
}
catch (EmptyQueueException exception)
{
Console.WriteLine();
Console.WriteLine("Message: " + exception.Message);
}
}
}
}
The following shows what you will see if you run the program:
Enqueue elements
11 12 13 14 15
Message: Queue is full, cannot enqueue new element
Dequeue elements
11 12 13 14
Message: Queue is empty, cannot dequeue element
Press any key to continue . . .
The following is the expected output after the code change:
Enqueue elements
11 12 13 14 15
Message: Queue is full, cannot enqueue new element
Dequeue elements
11 12 13 14
Message: Queue is empty, cannot dequeue element
Enqueue elements
A B C D E F
Message: Queue is full, cannot enqueue new element
Dequeue elements
A B C D E
Message: Queue is empty, cannot dequeue element
Press any key to continue . . .
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
