Professional Documents
Culture Documents
W1-2-60-1-6
JOMO KENYATTA UNIVERSITY
OF
AGRICULTURE AND TECHNOLOGY
University Examinations 2016/2017
THIRD YEAR FIRST SEMESTER EXAMINATION FOR THE DEGREE
OF BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY
BIT 2207: WEB DESIGN AND DEVELOPMENT II
DATE: DECEMBER 2016 TIME: 2 HOURS
INSTRUCTIONS: ANSWER QUESTION ONE AND ANY OTHER TWO
QUESTIONS
a) State the function of each of the following three languages all web developers must
learn: (6 Marks)
(i) HTML
(ii) CSS
(iii) Javascript
c) With suitable examples, show where a javascript is positioned within a HTML page.
(4 Marks)
Sample string: ‘The quick brown fox jumps over the lazy dog’.
Check whether the said string contains the sting ‘jumps’.
d) Write a PHP script that selects the id, firstname and lastname columns from the
students table and displays it on the page as follows: (10 Marks)
id: 1 – Name: John Kamau
id: 2 – Name: Mary Mwajuma
id: 3_ Name: Julie Odogo
(i) Polymorphism
(iii) Encapsulation
< ? Php
Class Books {
/*Member variables*/
Var & price;
Var & title;
/*Member functions*/
function set Price ($ Par) {
$ this - > price = $ par;
}
function get Price ( ){
echo & this - > price. “<br/>”;
}
function set Title (& Par) {
& this - > title = & par ;
2
BIT 2207
}
function get Title ( ) {
echo & this - > tittle . “<br/>”;
}
}
?>
(ii) Demonstrate how to create three books Maths, Physics and Chemistry
using the new operations. (3 Marks)
(iii) Show how to set title and prices of the three books by calling member
functions. (6 Marks)
a) The javascript function in the program has an error find out the error and fix it.
(6 Marks)
<script>
func MyFunc {
document . getElementById (“clemo”)
innerHTML = “Hello World!” ;
}
MyFunction ( )
</script>
</body>
</html>
(iii) The getDay ( ) method returns the weekday as a number between 0 and 6.
(Sunday = 0, Monday = 1, Tuesday = 2 ……) use the weekday number to
calculate weekday name: (use switch statement). (6 Marks)
3
BIT 2207
below:
Degrees Celsius
equals
Degrees Fahrenheit
4
BIT 2207
Functions of the languages: (i) HTML: Used for structuring the content on web pages. (ii)
CSS: Used for styling and layout of web pages. (iii) Javascript: Used for adding
interactivity and dynamic features to web pages.
b) Javascript function: A JavaScript function is a block of reusable code designed to perform a
particular task.
c) JavaScript within an HTML page:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
// Your JavaScript code here
</script>
</head>
<body>
<!-- Your HTML content here -->
</body>
</html>
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
5
BIT 2207
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstName = $_POST['FirstName'];
$lastName = $_POST['LastName'];
$age = $_POST['Age'];
$conn->close();
?>
<?php
$string = 'The quick brown fox jumps over the lazy dog';
$search = 'jumps';
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " .
$row["firstname"] . " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Question Three
a) Explanation of PHP terms: (i) Polymorphism: Allows objects of different classes to be
treated as objects of a common super class. (ii) Data Abstraction: Hides complex
implementation details and shows only the necessary features. (iii) Encapsulation:
Bundling of data and methods that operate on the data into a single unit.
b) PHP code extract explained: The given code defines a Books class with member variables
price and title and member functions setPrice, getPrice, setTitle, and getTitle.
c) Create three books:
$maths = new Books();
$physics = new Books();
$chemistry = new Books();
$maths->setTitle("Maths Book");
$maths->setPrice(100);
$physics->setTitle("Physics Book");
$physics->setPrice(150);
$chemistry->setTitle("Chemistry Book");
$chemistry->setPrice(120);
7
BIT 2207
Question Four
a) Fixed JavaScript code:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunc() {
document.getElementById("demo").innerHTML = "Hello
World!";
}
myFunc();
</script>
</body>
</html>
c) (i) Function of a switch statement: Allows multiple conditions to be tested. (ii) Syntax:
switch(expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
(iii) Calculate weekday name:
var day = new Date().getDay();
var weekday;
switch(day) {
case 0:
weekday = "Sunday";
break;
case 1:
weekday = "Monday";
break;
// ... Continue for other days
}
console.log(weekday);
8
BIT 2207
Question Five
a) Object in JavaScript: An instance of a class, which can have properties and methods.
b) Ways to create a new object:
1. Object literal: var obj = { key: value };
2. Constructor function: function ObjectName() { this.key = value; } var obj = new
ObjectName();
3. Object.create(): var obj = Object.create(proto);
c) JavaScript function to convert Celsius to Fahrenheit:
function celsiusToFahrenheit(celsius) {
var fahrenheit = (celsius * 9/5) + 32;
return fahrenheit;
}
document.getElementById("celsiusInput").addEventListener("inpu
t", function() {
var celsius = parseFloat(this.value);
var fahrenheit = celsiusToFahrenheit(celsius);
document.getElementById("fahrenheitOutput").value =
fahrenheit.toFixed(2);
});