You are on page 1of 7

Kuwait

M150 Data, Computing and Information


Programming Exercises on Unit 9
Prepared by: Amal Naji & Amal Abdullah

Time allowed: 1 hour

1. A new Rectangle object type is to created. It has the following properties:


length: a whole number that will hold the length of the Rectangle.
width: a whole number that will hold the width of the Rectangle.
The Rectangle object type has the following methods:
area(): a method to calculate the area of the rectangle and display it.
circumference (): a method to calculate the circumference of the rectangle and display it.
Note that: Rectangle Area = length * width and Rectangle Circumference = 2 * (length * width).

Write Jave Script code to do the following:


1. Define the constructor function Rectangle(aLength, aWidth) which will set the properties
and methods of the newly created Rectangle objects according to the above description.
2. Create a Rectangle instance named myRectangle, and initialize its properties as follows:
length = 40, width= 25.
3. Calculate the area of myRectangle using the method area().
4. Calculate the circumference of myRectangle using the method circumference().

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

1 / 7
2. You are required to model a new user-defined object type, Circle. A Circle object has a
radius property and two methods, area and circumference.

a. Write a constructor function for the Circle object data type.

b. Implement a method area(), which gets the value of the radius and returns the area of the
circle, that is (π * radius 2 ) and π = 3.14. Note that you could use Math.PI instead of 3.14.

c. Implement a method circumference(), which gets the value of the radius and returns the
circumference of the circle, that is (2π * radius).
d. Implement a method display(), which displays in an alert box the information about the
object as shown below.

e. Test the constructor and the methods implemented in b, c and d on Circle objects of radius
values 2, 50 and 100.

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

2 / 7
3. A new Institute object type is to created. It has the following properties:
name: a string variable that will hold the name of the Institute.
location: a string variable that will hold the location (country name) of the Institute.
listOfDept: an Array of strings, which holds the names of the departments inside the Institute.

The Institute object type has the following methods:


addDept(): a method to fill in the array listOfDept. In this method, the user should be asked to
enter the names of the departments, one at a time.
showDept(): a method to display the names of the departments stored in the array listOfDept.

Write Jave Script code to do the following:

1. Define the constructor function Institute (aName, aLocation, aNoOfDept) which will
set the properties and methods of the newly created Institute objects according to the above
description. Note that aNoOfDept defines the number of elements to be stored in the array
listOfDept.
2. Ask the user about the number of departments inside an Institute.
3. Create an Institute instance named myInstitute, and initialize its properties as follows:
name = 'MIT', location= 'USA', noOfDept = number of departments entered in step 1.
4. Fill in the departments’ names of myInstitute using the method addDept().
5. Display the list of departments of myInstitute using the method showDept().

(This question is taken from M150B final exam, Fall 2009)

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

3 / 7
4. You are required to model a mountain with a new user-defined object type, Mountain. A
Mountain object has two properties, name and height (in meters).

a. Write a constructor function which takes two arguments and define two instance variables,
name and height.

b. Now implement a method toString(), which gets the values of the name and height
properties of a Mountain object and outputs them in an alert box.
c. Create a Mountain object which represents Mount Everest (name-->'Everest' and
height-->8850) and test the method you've created in (b).
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

4 / 7
5. Given the following JavaScript codes:
Case (a)
function f()
{
var b, c;
a = a * 2;
document.write ('Value2 of a is’ + a + ’<BR>’);
}

var a = 100;
document.write(' Value1 of a is ' + a + ’<BR>’);
f();
document.write ('Value3 of a is’ + a + ’<BR>’);

Case (b)
function f()
{
var a, b;
a = a * 2;
document.write ('Value2 of a is’ + a + ’<BR>’);
}

var a = 100;
document.write(' Value1 of a is ' + a + ’<BR>’);
f();
document.write ('Value3 of a is’ + a + ’<BR>’);

a. What is the output after executing the code in case (a)?


..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

b. What is the output after executing the code in case (b)?


..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
...............................................................................................................................................
.....................................................................................................................................
..........................................................................................................................................
(This question is taken from M150B final exam, Fall 2009)

5 / 7
6. What is the output of the following JavaScript code?
function function1() {
var a,b;
a =10;
b = 20;
document.write(a + '<BR>');
document.write(b + '<BR>') };

var b,c;
b = 30;
c = 40;

for (var i=1; i<3; i= i+1)


{
b = b + 1;
document.write(b + '<BR>');
function1()
}

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

7. What are the values of x, y, a and b after executing the following code?
var x = 22;
var y = x;
y = y + 1;
var a = new Array (10,20,30);
var b = new Array ('A','B','C');
a = b;
b[2] = 100;

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

6 / 7
8. What is the output of the following Java Script code?
var x = 50;
var y = x;
var date1 = new Date(2010,9,21);
var date2 = date1;
x = x + y;
date1.setMonth(11);
document.write(x +'<BR>');
document.write(y + '<BR>');
document.write(date1.getMonth()+'<BR>');
document.write(date2.getMonth()+'<BR>');

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................

9. What is the output of the following JavaScript code?


var a, b, c;
a = 2;
b = 5;
c = a + b;

document.write('For this first display the value of c is ' +


c + '<BR>');
AddTwoNumbers();
c = a + b;
document.write('For this third display the value of c is ' +
c + '<BR>');

function AddTwoNumbers()
{
var a = 7;
b = 4;
c = a + b;
document.write('For this second display the value of c
is ' + c + '<BR>');
}

..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
(This question is taken from M150B final exam, Spring 2009)

7 / 7

You might also like