December 1999
JP219 : JAVA PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 1

(a)

(i)Explain what is meant by the Java Virtual Machine .[2 marks ]
(i)The Java Virtual Machine is
•An imaginary machine;(1 mark)
•Which a real machine emulates in software.(1 mark)

(ii)Explain why the concept of the virtual machine adds to the portability of code.[2 marks ]
(ii)Portability is easier because
•Code is compiled for a generic machine specification;(1 mark)
•Thus removing the specifics of the hardware on which it is to be run.(1 mark)

 

[4]
(b)

Define what is meant by the terms:

(i)Encapsulation.[1 mark ]
(i)Encapsulation is abstracting the implementation of a data type from the interface to that data type.

(ii)Polymorphism.[1 mark ]
(ii)Polymorphism is the ability to use a data type where another is expected.

 

[2]

(c)

Java supports single inheritance .

(i)Explain what is meant by the term single inheritance .[1 mark ]
(i)Single inheritance is when a derived class can inherit from only one base class.

(ii)Name the construct which is supplied in order to allow programmers to employ multiple inheritance techniques.[1 mark ]
(ii)Interface classes.

 

[2]
(d)

(i)Briefly identify the difference between a class and an object .[2 marks ]
(i) •A class is a description of a data type.(1 mark)
•An object is a particular instance of a class .(1 mark)

(ii)State the purpose of a class constructor.[1 mark ]
(ii)A class constructor is responsible for allocating and initialising memory when an object is created.

(iii)State the purpose of a class destructor.[1 mark ]
(iii)A class destructor is responsible for cleaning up after an object when it goes out of scope.

 

[4]
(e)

Define a class,called Employee, which contains the following members:
•A public employee number,Number, which is an integer;
•A public employee name,Name ,which is a string;
•A public method,called PrintDetails ,which takes no parameters and prints out the values of Name and Number .
(e)a sample definition of Employee follows:

public class Employee {
 

public int Number;
public String Name;
public void PrintDetails(){


}

System.out.println(Name +Number);
}  

And the following marking scheme should be used:
•A correct public class declaration;(1 mark)
•Correct declarations of the two public member variables;(1 mark)
•A correct method signature for PrintDetails ;(1 mark)
•A correct print statement.(1 mark)

 

[4]
(f)

(i)Write a method,called Test ,the signature of which is given below,which takes in a single char parameter x .The method should return 0 if the parameter is equal to a ,1 if the parameter is equal to b ,and -1 otherwise.
int Test(char x); [4 marks ]
(i)A sample definition of Test follows:

int Test(char x)
{

switch(x){
case ’a ’:

return 0;

case ’b ’:

return 1;

default:

return -1;

}

And the following marking scheme should be used:
•Defining a suitable conditional statement which tests different values of a ; (1 mark)
•Including a suitable clause to return 0 if x ==’a ’;(1 mark)
•Including a suitable clause to return 1 if x ==’b ’;(1 mark)
•including a suitable default clause to return -1 in any other event.(1 mark)

(ii)Write an iterative method,called Sum ,the signature of which is given below, which takes a single integer parameter x ,and will return the sum of all the integers from 0 to x .You may assume that x will always be a positive integer.
int Sum(int x); [5 marks ]
(ii)A sample definition of Sum is given below:

int Sum(int x)
{

int i;
int sum=0;

for(i=0;i<x;i++){

sum=sum+i;

}
return sum;

}

And the following marking scheme should be used:

•Declaring the local variables i and sum ;(1 mark) •Declaring a suitable iterative structure;(1 mark) •Correctly bound by x ;(1 mark) •Incrementing sum by i on each iteration;(1 mark) •Returning the value of sum on termination of the loop.(1 mark)

(iii)In mathematics,N!(known as N factorial )can be computed using the formula N!=N*((N-1)!).
The base case,0!,i defined as being 1.
Write a recursive method,called Factorial ,the signature of which is given
below,which takes a single integer parameter N ,and returns N!.
int Factorial(int N); [5 marks ]
(iii)A sample definition of Factorial follows:

int Factorial(int N)
{

if (N==0){

return 1;

}else {

return (x *Factorial(N -1));

}

}

And the following marking scheme should be used:And the following marking scheme should be used:
•Declaring a suitable conditional statement to test for the base case;(1 mark)
•And returning 1 when the base case is reached;(1 mark)
•Recursively calling Factorial when it is not the base case;(1 mark)
•With N -1 as the parameter;(1 mark)
•Returning N * the result of the recursive call;(1 mark)

[14]