P h i l

advertisement
Philadelphia University
Faculty of Information Technology
Department of Software Engineering
Examination Key
Lecturer : Dr. Samer Hanna
Internal Examiner: Dr. Moayad Al-Adami
Coordinator: Dr. Samer Hanna
Software Construction
(0721420) Section 1
Second Exam’s Key
First Semester of 2015/2016
th
Date: Wednesday, Jan. 5 , 2016-------- Time: 50 min.
Q1) (4 marks)
Answer the following:
1. Discuss in details the meaning of defensive programming with examples. (2 marks)
Solution:
The idea of defensive programming is based on defensive driving. In defensive driving, you adopt
the mind-set that you are never sure what the other drivers are going to do. That way, you make sure
that if they do something dangerous you won't be hurt. You take the responsibility of protecting
yourself even when it might be the other driver's fault.
In defensive programming, the main idea is that if a routine is passed bad data, it won't be hurt, even
if the bad data is another routine's fault.
Examples of defensive programming are assertions and error-handling techniques.
2. Discuss three differences between assertions and error handling techniques. (2 marks)
Assertion
Error Handling Technique
An assertion is code that is used during development
Error handling techniques is code that is used
during development and after delivery
assertions for conditions that should never occur
error-handling code is used for conditions
you expect to occur
the corrective action is to change the program's source
code, recompile, and release a new version of a software.
the corrective action is merely to
handle an error gracefully
Q2) (8 marks)
Suppose that you are asked to develop a Windows Form application for Philadelphia University library with the
following specifications:
The needed attributes of the library are: location, number of English books, and number of Arabic books.
The needed attributes of the book in the library are: book name, author(s), number of authors, and year of publication.
The needed attributes of each author of a book, are: author name, id, and field.
1. Draw the class diagram corresponding to the above specifications. (1 mark)
1
2. Map the class diagram in branch 1 to code in C# (4 marks)
(Note: write a complete property for only one attribute in each class while other attributes must use auto
implemented properties)
3. Write a method in the Library class that searches for a given book in the library given this book name. (1 marks)
4. Write a method that can display the data of three books (choose any data for testing) in a DataGridView. (2 marks)
Solution:
1.
2.
namespace LibraryProject
{
class Author
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public int Id { get; set; }
public string Field { get; set; }
public Author(string name1, int id1, string field1)
{
Name = name1;
Id = id1;
Field = field1;
}
}
}
using System.Collections;
namespace LibraryProject
{
class Book
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public int NumAuthors { get; set; }
public int Year { get; set; }
public ArrayList Authors { get; set; }
public Book (string name1, int na1, int year1, ArrayList authors1)
{
Name = name1;
2
NumAuthors = na1;
Year = year1;
Authors = authors1;
}
}
}
using System.Collections;
namespace LibraryProject
{
class Library
{
private string location;
public string Location
{
get { return location; }
set { location = value; }
}
public int Arabic { get; set; }
public int English { get; set; }
public ArrayList Books { get; set; }
public Library (string loc1, int arabic1, int english1, ArrayList books1)
{
Location = loc1;
Arabic = arabic1;
English = english1;
Books = books1;
}
}
}
3.
public bool Search (string n)
{
foreach (Book b in Books)
{
if (b.Name == n)
return true;
}
return false;
}
4.
private void button1_Click(object sender, EventArgs e)
{
IList list = new ArrayList();
ArrayList authors = new ArrayList();
Author a1 = new Author("Samer Hanna", 1, "Web");
Author a2 = new Author("Saead Goal", 2, "SE");
authors.Add(a1);
authors.Add(a2);
Book b1 = new Book("Software Construction", 2, 2015, authors);
Book b2 = new Book("Software Testing", 2, 2014, authors);
Book b3 = new Book("Software Engineering", 2, 2013, authors);
3
list.Add(b1);
list.Add(b2);
list.Add(b3);
dataGridView1.DataSource = list;
}
Q3) (5 marks)
In Questions 2; suppose that the number of authors attribute, in the book class was supposed to be in the range of 1 to
10; apply the following defensive programming techniques with this attribute:
1. assertions (1 mark)
2. Return a neutral value. (1 mark)
3. Substitute the closest legal value. (1 mark)
4. Log a warning message. (1 mark)
5. Return an error code. (1 mark)
Solution:
1.
using System.Diagnostics;
public int findAuthorsNum()
{
Debug.Assert(NumAuthors >= 1 && NumAuthors <= 10, "invalid number of authors");
return NumAuthors;
}
2.
public int findAuthorsNum()
{
if (NumAuthors < 0 || NumAuthors > 10)
return 0;
else
return NumAuthors;
}
3.
public int findAuthorsNum()
{
if (NumAuthors
NumAuthors
if (NumAuthors
NumAuthors
<
=
>
=
0)
0;
10)
10;
return NumAuthors;
}
4.
using System.IO;
public int findAuthorsNum()
{
4
StreamWriter file = new StreamWriter("c:\\error.txt");
if (NumAuthors < 0 || NumAuthors > 10)
{
file.WriteLine("invalid number of authors");
}
return NumAuthors;
}
5.
public enum Status { Success, Failure};
public Status findAuthorsNum2()
{
if (NumAuthors < 0 || NumAuthors > 10)
return Status.Failure;
else
return Status.Success;
}
Q4) (3 marks)
1- Discuss the differences between correctness and robustness. ? (1 mark)
2- Name two error handling techniques that support correctness and two that support robustness. ? (1 mark)
3- In the library application in Question 2; do you think this application should prefer correctness or robustness
and why? (1 mark)
Solution:
1.
These terms are at opposite ends of the scale from each other. Correctness means never returning an
inaccurate result; returning no result is better than returning an inaccurate result. Robustness means always
trying to do something that will allow the software to keep operating, even if that leads to results that are
inaccurate sometimes.
2.
Robustness
Return neutral value
Closest legal value
Correctness
Shut down
5
Return an error code
3.
Since the application is not critical application then it should favor robustness
6
Download