Philadelphia University Faculty of Information Technology Department of Software Engineering Lecturer : Dr. Samer Hanna Examination Key

Software Construction (0721420 ) Section 1 First Exam’s Key First Semester of 2015/2016 Date: Wednesday, Nov. 25th, 2015------Time: 50 min.

Answer the following: 1. What is the purpose of inheritance in software construction? (give example) (2 marks)

Sol. Inheritance means that the behavior and data associated with child classes are always an extension (that is, a larger set) of the properties associated with parent classes. A subclass will have all the properties of the parent class, and other properties as well. The purpose is to reuse attributes and methods of the parent class. For example, when a Student class extends a Person class it borrows all the attributes in that class such as name and ssn, etc. and adds the data related only to students such as student id, etc.

2. What is the purpose of using interface in software construction? (give example) (2 marks) A way to obligate a group of classes to implement a certain methods contained in an interface. For example, we can create an interface for the collection datatypes such as List, ArrayList, etc. that includes the needed method for collections such as add item, remove item etc. after that any class that wants to be a collection datatype must implement this interface.

3. What is the difference between inheritance and interface? (1 mark) In Java and C#, a class can extend (inherit from) only one class but can implement more than one interface.

1 Q2)

1. Consider the following class diagram for a university application:

1. Map the class diagram to code in Java (6 marks) 2. Write the code of the method findDept in class Faculty, where this method has the following spec. findDept searches for a given department in a Faculty given a department id, it return an object of the founded department or a null in case that it does not exists in the Faculty. (2 marks) 3. Write a method that removes a department from a faculty after making sure that this department already exists. (2 marks) ------Solution

1. package a; public class Course { private int courseNo; private String courseName; public int getCourseNo() { return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Course(int courseNo, String courseName) {

this.courseNo = courseNo; this.courseName = courseName; 2 }

} package a; public class Department { private int id; private String name; private int numStudents; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumStudents() { return numStudents; } public void setNumStudents(int numStudents) { this.numStudents = numStudents; } public Department(int id, String name, int numStudents) {

this.id = id; this.name = name; this.numStudents = numStudents; }

} package a; import java.util.*; public class Faculty { private int id; private String name; private int numStudents;

ArrayList depts = new ArrayList();

public Faculty(int id, String name, int numStudents, ArrayList depts) { this.id = id; this.name = name; this.numStudents = numStudents; this.depts = depts; }

public int getId() { return id; }

public void setId(int id) { this.id = id; }

3 public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getNumStudents() { return numStudents; }

public void setNumStudents(int numStudents) { this.numStudents = numStudents; }

public ArrayList getDepts() { return depts; }

public void setDepts(ArrayList depts) { this.depts = depts; }

public void addDept (Department d) { depts.add(d); }

public Department findDept(int myId) { for (Department current : depts) { if (current.getId()==myId) return current; }

return null; }

} package a; public interface Marking {

public abstract float findAccumelatedAvg();

} package a; public class Student extends UniversitMember implements Marking {

private int id; private String name;

public Student(int i, String n, int ssn) { super(ssn); id=i; name=n; }

public int getId() { return id; 4 } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(int ssn, int id, String name) { super(ssn); this.id = id; this.name = name; }

public float findAccumelatedAvg() { return 1.1f; }

} package a; public class UniversitMember {

protected int ssn;

public int getSsn() { return ssn; }

public void setSsn(int ssn) { this.ssn = ssn; }

public UniversitMember(int ssn) { super(); this.ssn = ssn; }

} Solution 2. public Department findDept(int myId) { for (Department current : depts) { if (current.getId()==myId) return current; }

return null; }

------Solution 3.

5 Q3) (4 marks) 1. public class Testing { public static void main(String[] args) { Department dept1 = new Department (1, "SE", 250); Department dept2 = new Department (2, "CS", 150); Department dept3 = new Department (3, "MIS", 80);

ArrayList depts = new ArrayList<>(); depts.add(dept1); depts.add(dept2); depts.add(dept3); Faculty faculty = new Faculty(1, "IT", 400, depts);

Department d = faculty.findDept(2);

if (d!= null) { System.out.println(d.getName()); } else { System.out.println("department does not exist"); }

Department d2 = faculty.findDept(5);

if (d2!= null) { System.out.println(d2.getName()); } else { System.out.println("department does not exist"); }

}

}

6 7