Assignment 2 - Posted 09/22/15

advertisement
CSC 325
PROGRAM 2
Due October 25, 2015
Delegates and Lambdas
I. Requirements: Create a C# program that uses delegates and lambda
expressions to find the minimum element in a generic array.
1. Create a method with the signature: T findMin<T>(T[] arr, Func<T, T,
bool> comparison). This method should find and return the minimum
element in the generic array arr passed as the first parameter to the
method.
2. The method’s second parameter comparison must be a generic delegate
that takes two parameters and returns a bool value. All instances that
implement this delegate must return true when the delegate’s first
parameter is smaller (precedes) than the second parameter - otherwise
the delegate instances must return false.
3. The code inside the findMin method should use the delegate instance
passed as the second parameter to compare array elements and to select
the minimum one.
4. To test your code create two structures:
a. Person with at least two data fields – string Name and int Age;
b. Student with at least two data fields – string StudentID and double
GPA.
5. In your Main() method create two arrays – one of type Person and the
other of type Student.
6. Populate each array with at least five unique elements.
7. Using lambda expressions generate three delegate instances that follow
item 2 requirements. Then call the findMin method with proper
delegate parameters, display the values returned by this method, and
verify that the minimum elements were actually found correctly. Use
the following combinations of arrays/lambdas as parameters to the
findMin method:
a. Array of type Person and lambda that compares Person instances
using the Age field.
b. Array of type Student and lambda that compares Student instances
using the StudentID field.
c. Array of type Student and lambda that compares Student instances
using the GPA field.
II. Bonus task (can add up to 30% to the assignment grade):
1. Code a generic method int findLast<T>(T[] arr, T target, Func<T, T,
bool> equality) that searches the array arr for the element equal to the
target parameter and returns the index of the last found element or -1 if
the search fails.
2. The method’s third parameter equality must be a generic delegate that
takes two parameters of the same type and returns a bool value. All
instances that implement this delegate must return true when the
delegate’s first parameter is equal to the second parameter - otherwise
the delegate instances must return false.
3. The code inside the findLast method should use the delegate instance
passed as the third parameter to check if at least one array element is
equal to the target parameter.
4. Test the findLast method with at least 3 delegates using guidelines
described in requirements 4 – 7.
IV. Submit your lab report that includes all items described in the
Laboratory/Program Report Specifications document.
V. Notes:
1. When creating project using a computer in the Computer Science
Department laboratories (MH 202/209/210), always make sure that the
project and all associated files are on the disk drive that you will be able
to access next time. This drive may be h: drive on the Computer
Science Department LAN, USB flash drive, etc. Never store any files
on the local drives c: or d: as these files will be automatically deleted
after you log off.
2. When submitting lab or programming assignments, always follow
the home work submission guidelines described in the
Laboratory/Program Report Specifications document
CS325Report.doc that can be downloaded from my Web site
http://cs.salemstate.edu/brikman/csc325.php.
3. Save your work regularly, especially at the end of each class.
4. Keep a detailed record of all steps performed.
5. Use the sample code from a solution
chapter8\DelegatesAndEventsSamples.sln (projects
BubbleSorter\BubbleSorter.csproj and
LambdaExpressions\ LambdaExpressions.csproj) as templates for your
design.
VI. To download sample source code from the textbook:
1. Go to http://cs.salemstate.edu/brikman/.
2. Click on the link labeled “CSC 325” and then on the link “Source code
(7-Zip)”. Download the file source.7z to your computer.
3. Using WinZip, 7-Zip or any other utility unzip the source.7z file.
Download