- Arrays
An array contains similar elements in a large quantity. In Java Script,
they are numbered from 0 to n-1 where n is the length of the array.
If the array has the name "allemployees", then one can access the
elements the first element as "allemployees[0]", the next element
as "allemployees[1]" and so on. The length of the array can be
obtained by a method belonging to the array object as follows:
"n = allemployees.length" - the methods referring to the object
"allemployees" are all marked by adding a dot (".") and the name
of the method to the variable name "allemployees". In this case
the method has the name "length".
- Records
A record contains various datas belonging to the same object.
Accessing these data is in Java Script the same as accessing a
method - the only difference is that one can also write values
into the method what normally is not possible. The word "object"
refers to both: the abstract data type it represents and a
variable taking an instance of this data type. In this assignment,
the data type "employee" is defined by the following constructor
function "employee(fn,sl,io)":
structure:
function employee(fn,sl,io)
{ this.fullname = fn;
this.salary = sl;
this.isowner = io; }
The keyword "this" refers to the object created by the constructor
function and "this.fullname" then to the value the field
full name will have.
Now one can create an employee "emp" as follows:
var emp = employee("Gerald Gugelhupf",2233.44,false);
which will have then the full name "Gerald Gugelhupf", earn
SGD 2233.44 per month but not be considered to be an owner.
- Array of Records
The data structure in this assignment is an array of records.
It is initialized as follows.
allemployees = new Array(5);
allemployees[0] = new employee("Anneliese Aal",1324.35,false);
allemployees[1] = new employee("Boris Bratling",1122.44,false);
...
So allemployees consists of 6 entries with indices 0,1,2,3,4,5.
The data stored in the first two entries is the following:
- allemployees[0] --
Employee Name: Anneliese Aal; Salary: SGD 1324.35; Not an owner.
- allemployees[1] --
Employee Name: Boris Bratling; Salary: SGD 1122.44; Not an owner.
The following Boolean condition is true.
allemployees[1].fullname == "Boris Bratling"
So the "dot" and field name for the record comes after the
index-number to identify the correct array element.
- Get Started
Go into the directory public_html of your homepage if not already
done. Then download this homework.
wget http://www.comp.nus.edu.sg/~gem1501/assignment10.html
chmod 755 *.html
Make yourself familiar with the program by reading it.
- Add the age of the employees
Each employee should have an age. So change the function "employee"
such that it also assigns to "this.age" a number. This number should
be a parameter of the calling functions. Add for each employee a
value for the age; it should be some number between 25 and 65.
Adapt the function "employeeprint" such that it also prints
the age of the employee.
- Add a new employee
Modify the length of the array to 7. Furthermore, assign to the
new employee the name "Herbert Hering" and the salary
SGD 1100.22. His age is 35.
- Have two owners
Change the program such that Eberhard Ei and Claudia Creme are
both owners of the company. Both owners have a salary from their
own company (as already implemented).
- Write a loop which lists the owners
It should scan through the array "allemployees" and list those
employees which are owners. You can read the code for the function
to sum the salary for seeing how this is done.