December 1999
JP219 : JAVA PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 1

(a)

(i)Explain what is meant by the Java Virtual Machine .[2 marks ]

(ii)Explain why the concept of the virtual machine adds to the portability of code.[2 marks ]

 

[4]
(b)

Define what is meant by the terms:

(i)Encapsulation.[1 mark ]

(ii)Polymorphism.[1 mark ]

 

[2]

(c)

Java supports single inheritance .

(i)Explain what is meant by the term single inheritance .[1 mark ]

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

 

[2]
(d)

 (i)Briefly identify the difference between a class and an object .[2 marks ]

(ii)State the purpose of a class constructor.[1 mark ]

(iii)State the purpose of a class destructor.[1 mark ]

 

[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 .

 

[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 ]

(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 ]

(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 ]

[14]