Csc253 Advanced C# Programming Lab 12 Generics Complete Solutions Correct Answers Key Find The Solution at
Csc253 Advanced C# Programming Lab 12 Generics Complete Solutions Correct Answers Key Find The Solution at
O BJECTIVES
GOALS
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:
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________
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:
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________
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
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________
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
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________
// constructors
public QueueLab()
: this(10)
{
}
numElements = 0;
front = 0;
back = -1;
}
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)
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QueueAssignment
{
class TestQueueLab
{
private static QueueLab intQueue;
}
}
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
_____________________________________________________________________________________________________________________________ _________________________________________________________________________________________
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
PROGRAM 1
PROGRAM 2