- Philadelphia University Jordan

Philadelphia University
Faculty of Information Technology
Department of Software Engineering
Examination Paper
Lecturer : Dr. Samer Hanna
Internal Examiner: Dr. Mourad Maouch
Coordinator: Dr. Samer Hanna
Software Construction
(0721420 ) Section 1
Quiz 2
First Semester of 2014/2015
Date: Sunday, Jan. 25 , 2015-------- Time: 50 min.
th
Q1) (5 marks)
Consider a method with the following specifications:
Input: a student id of a student in Philadelphia University.
Output: one of two possibilities; either “exists” if the student is currently studying at Philadelphia or “does not exist”
if the student is not in Philadelphia.
The method should loop through the ArrayList in order to search for the student with the given id of the input.
Note:
a. Assume that there is an ArrayList that contains objects of type Student containing all the information of a
given Philadelphia student.
b. Assume that the student ids in Philadelphia should be between 2005 and 2015.
1. Write the code of this method in C#. (1 marks)
2. Apply assertion as a defensive programming technique for the code of the method. (1 mark)
3. Apply exceptions as a defensive programming technique for the code of the method. (1 marks)
Apply the following error handling technique for the method
4.
5.
6.
7.
8.
9.
10.
Return a neutral value. (0.5 mark)
Substitute the closest legal value. (0.5 mark)
Log a warning message. (0.5 mark)
Return an error. (0.5 mark)
Call an error processing routine as. (0.5 mark)
Display an error message(0.5 mark)
Shutdown(0.5 mark)
Solution:
1.
public enum status {exists, doesNotExist}
public status Search (int id)
{
Foreach (Student current in studs)
If (current.Id == id) retrun Status.exists;
Return status.doesNotExist
}
2.
Debug.Assert(id>=2005 && id<=2015, “Invalid id”);
3.
try
{
Int id = Console.ReadLine( );
1
}
Catch (Exception e)
{
Console.Writeline (e.Message)
}
4.
public status Search (int id)
{
if (id <2005 || id>2015)
Id=0;
Foreach (Student current in studs)
If (current.Id == id) retrun Status.exists;
Return status.doesNotExist
}
5.
Change the iff statement in 4. to
if (id <2005)
Id=2005;
if (id >2005)
Id=2015;
6.
if (id <2005 || id>2015)
//store a proper message in a log file
7.
public enum status {exists, doesNotExist}
public status Search (int id)
{
Foreach (Student current in studs)
If (current.Id == id) retrun Status.exists;
Return status.doesNotExist
}
8.
if (id <2005 || id>2015)
error_processor(id);
9.
if (id <2005 || id>2015)
Console.WriteLine(“Invalid Id”);
if (id <2005 || id>2015)
Application.Exit( );
2