Philadelphia University

Lecturer: Dr. Samer Hanna

Internal Examiner: Dr. Mohammad Taye

Coordinator: Dr. Samer Hanna

Software Construction

(0721420 ) Section 1 Second Exam’s Key First Semester of 2016/2017

Date: Monday, Dec. 26th, 2016------Time: 50 min.

Q1) (4 marks)

Answer the following:

1.  Name and give examples of two differences between writing code in Java vs. C# programming languages. (2 marks)

Solution:

1.  To inherit from a class, we use the keyword “extends” in Java while in C# we use colon (:)

//Java

class A extends B { }

//C#

class A : B { }

2.  To implement (realize) an interface, we use the keyword “implements” in Java while in C# we use colon (:)

//Java

class A implements Interface1 { }

//C#

class A : Interface1 { }

3.  Java uses the keyword “package” to group classes while C# uses “namespace”

4.  Java uses independent methods to get and set an attribute while in C# get and set are included inside properties.

5.  Java uses super while C# uses base to refer to the class we inherited from.

2.  Discuss two differences between C# and JavaScript programming languages when building a Web application. (2 marks)

Solution:

1.  JavaScript is client side while C# is server side

2.  When running a C# program we use the compiled version while in JavaScript the script itself is executed whenever it is needed.

3.  In C3 we must provide a datatype for a variable (int x) while in JavaScript we can declare a variable without specifying a datatype (var x)

4.  JavaScript code is executed faster because it does not have to use a network.

5.  The validation done by JavaScript is easy to be circumvented by hackers because it’s on the client side, this is not possible with C#.

Q2) (8 marks)

Map the following specifications to Java code depending on the class diagram in Page 2.

1. set-number is a method that sets the number attribute of a Section after making sure that this number is between 1 and 10, the number attribute is set to 0 otherwise. (0.5 mark)

2. get-number is a method that returns the section number of a given section. (0.5 mark)

3. Map the relation between the Section and Student class to code. (1 mark)

4. Map the relation between the Student and Department class to code. (1 mark)

5. search-Student is a method that search for a given student in a Section given this student’s id, if the student exists then the method returns all the information of that student, otherwise it returns null. (2 marks)

6. Write the pseudo code of a method that prints the information of all the students in a given Section with id greater than 201500000 and the then convert the pseudo code to Java code. (2 marks)

7. Write the needed Java code to test the search-Student method you wrote in branch 5. (1 mark)

Solution:

Inside the Section class

1. (0.5 mark)

Inside the Section class

public void setNumber(int sectionNum) {

if (sectionNum >=1 & sectionNum <=10)

this.number = sectionNum;

else

System.out.println("section number must be between 1 and 10");

}

2. (0.5 mark)

Inside the Section class

public int getNumber() {

return number;

}

3. (1 mark)

Inside the Section class

import java.util.*

ArrayList<Student> studs = new ArrayList<Student>( );

4. (1 marks)

Inside the Student class

Department d = new Department( );

5. (2 marks)

public Student searchStudent(int id)

{

for (Student current : studs)

{

if (current.getId()== id)

return current;

}

return null;

}

6. (2 marks)

public void printStuds()

{

//loop through all the students in a section

for (Student current : studs)

{

//print the information of the students with id>201500000

if (current.getId( ) > 201500000)

System.out.println(current.getId( ));

System.out.println(current.getName( ));

}

}

}

7. (1 mark)

Section section1 = new Section (1, 17, "Software Construction");

Department se = new Department ("Software Engineering", "Dr. Moayad Al-Adami", "IT");

Student s1 = new Student(20102211, "Yousif Maqusi", se);

Student s2 = new Student(20114444, "Osama Shata", se);

Student s3 = new Student(20122555, "Odai Faori", se);

section1.addStudent(s1);

section1.addStudent(s2);

section1.addStudent(s3);

//student that is existed

Student found = section1.searchStudent(20122555);

if (found!= null)

System.out.println(found.getName());

else

System.out.println("Does not exists");

//student that is not existed

Student found2 = section1.searchStudent(11111);

if (found!= null)

System.out.println(found2.getName());

else

System.out.println("Does not exists");

Q3) (8 marks)

Suppose that you are asked to build a Web page to allow a student at Philadelphia University to insert his id, name, and the number of hours he already passed.

1. Write the HTML of this page (2 marks)

2. Write the needed JavaScript function to validate that the number of passed hours inserted by a student is between 0 and 132 (proper error message should be printed otherwise) (2 marks)

3. Repeat the same validation in branch 2 but using C# server side code. (2 marks)

4. Write the needed CSS to:

a. Give a width and height to the page you created (1 mark)

b. Display the page in the middle of the browser. (1 mark)

Solution:

1.  (2 marks)

html xmlns="http://www.w3.org/1999/xhtml">

head runat="server">

titlePhiladelphia Students</title

</head

body

form id="form1" runat="server">

div id="wrap">

h2Philadelphia Students</h2

Student Id: asp:TextBox ID="txtId" runat="server"</asp:TextBox

br />

br />

Student name: asp:TextBox ID="txtName" runat="server"</asp:TextBox

br />

br />

Passed Hours: asp:TextBox ID="txtHours" runat="server" TextMode="Number"</asp:TextBox

br />

br />

asp:Button ID="btnOk" runat="server" Text="OK" />

</div

</form

</body

</html

2.  (2 marks)

function validate() {

var h = document.getElementById("txtHours").value;

if (h < 0 || h > 132)

alert("invalid number of hours");

}

3.  (2 marks)

protected void btnOk_Click(object sender, EventArgs e)

{

int h = int.Parse(txtHours.Text);

if (h < 0 || h > 132)

Response.Write("invalid hours");

}

4. 

#wrap {

width: 400px;

height: 400px; /*1 mark*/

margin: auto; /*1 mark*/

}

5