You are on page 1of 10

CSC253 ADVANCED C# PROGRAMMING LAB 12 GENERICS complete

solutions correct answers key


_______________________________________________________________________
FIND THE SOLUTION AT
HTTP://WWW.COURSEMERIT.COM/SOLUTION-DETAILS/15130/CSC253-ADVANCED-C-PROGRAMMING-LAB-12-GENERICS-
COMPLETE-SOLUTIONS-CORRECT-ANSWERS-KEY

O BJECTIVES

In this lab assignment, students will learn:


- To create generic methods to perform identical tasks on arguments of different types
- To apply type constraints on generic methods
- To create generic classes
- To use generic classes

GOALS

In this lab assignment, students will demonstrate the abilities to:


- Create generic methods to perform identical tasks on arguments of different types
- Apply type constraints on generic methods
- Create generic classes
- Use generic classes

DESCRIPTION

PROGRAM 1

Write a generic method that implements a linear search in an array. This method
should compare the search key with each element in an array until the search key is
found or until the end of the array is reached. If the search key is found, return its
location in the array; otherwise, return -1. Write a test application that inputs and
searches an int array and a double array. The test application should have a GUI
like this:
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 2


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

When the user clicks the “Create integers” button, the program generates 6 random
integers between 0 and 999. The user then enters a search key and presses the
“Search” button. The program will display the search result:
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 3


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

If the search key is not in the array, display “Value not found”:

If the “Create doubles” button is pressed, generate 6 random double values that have
two digits after the decimal point between 0.00 and 99.99. You can do this by
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 4


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

generating 6 random integers between 0 and 999 and divide them by 100. Do the
same search as the integers.

PROGRAM 2

This program is about generic classes. In Section 20.6 of the textbook there is an
example of how to write and use a generic stack class. We are going to do something
similar.

In Chapter 19 we learned what a queue is. Figure 19.16 uses a linked list to
implement a queue. Actually we can also implement a queue with an array. The
following is the code.

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
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 5


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

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");
}
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 6


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

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)
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 7


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

: 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);
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 8


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

}
}

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 . . .

Your task is to modify the code given above 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. The
following is the expected output:

Enqueue elements
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 9


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

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 BC DEF
Message: Queue is full, cannot enqueue new element
Dequeue elements
ABCDE
Message: Queue is empty, cannot dequeue element
Press any key to continue . . .

GRADING RUBRIC

[Please see next page]


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

CSC253 Lab 12 Page 10


_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________

PROGRAM 1

Category High Proficiency Medium Proficiency Low Proficiency


Creating generic Create generic Minor problems in Major problems in
method with method with creating generic creating generic
type constraint type constraint method with type method with type
[20 pts] correctly [20 constraint [10-19 pts] constraint [0-9 pts]
pts]
Using generic Using generic Minor problems in Major problems in
method [10 pts] method correctly using generic method using generic
[10 pts] [5-9 pts] method [0-4 pts]
Other C# code Write other C# Minor problems in Major problems in
[10 pts] code correctly writing other C# writing other C#
[10 pts] code [5-9 pts] code [0-4 pts]
Program Program Program compiles but Program fails to
compilation and compiles and does not generate compile [0-4 pts]
test runs [10 runs correctly correct output [5-9
pts] [10 pts] pts]

PROGRAM 2

Category High Proficiency Medium Proficiency Low Proficiency


Creating Create generic Minor problems in Major problems in
generic class method with type creating generic creating generic
[20 pts] constraint correctly method with type method with type
[20 pts] constraint [10-19 pts] constraint [0-9 pts]
Testing Testing generic Minor problems in Major problems in
generic class class with testing generic class testing generic class
with characters with characters [10- with characters [0-9
characters [20 successfully [20 19 pts] pts]
pts] pts]
Program Program compiles Program compiles but Program fails to
compilation and runs correctly does not generate compile [0-4 pts]
and test runs [10 pts] correct output [5-9
[10 pts] pts]

You might also like