August 2000
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) Java is an object oriented language. Give two main characteristics of an object oriented language. [2 marks]

(b) State the scope of members of a class if declared:
(i) public [1 mark]
(ii) protected [1 mark]
(iii) private [1 mark]

(c) Explain the difference between a class and an object. [2 marks]

(d) A Book is a type of class which contains a number of pages, which is an int, and an array of pages, which is a private class.
(i) Define the class Book such that it contains the private member NumberPages, an array of objects of type Page. These members should not be available to any other classes. [4 marks]
(ii) Write a constructor for your class which takes 3 parameters, the number of pages the Book is to contain, an array of Page objects and a boolean variable. The constructor should initialise the number of pages, initialise the array with 100 elements and copy the Page objects passed in if the boolean variable is set to true. [4 marks]

(e) Give the result of evaluating the following expressions:
(i) (true && false) [1 mark]
(ii) (3 >> 2) [1 mark]
(iii) ( (c % a) + b), where a= 4, b= 8, c= 12 [1 mark]
(iv) (a += b ), where a= 5 and b= 10 [1 mark]
(v) (b += ++a), where a= 5 and b= 10 [1 mark]

(f) (i) In order to calculate x^ y, we can recursively calculate x *( x ^ (y - 1) ) until we reach the base case x^ 1 = x. Write a recursive method, called Power, the signature of which is given below, which takes in two ints, a mantissa, and an exponent. The method should return the result of raising the mantissa to the power exponent, using the method of calculating this given above.
int Power(int mantissa, int exponent); [5 marks]
(ii) Write a method called Search, the signature of which is given below, which takes in a String s and a Character c, and searches the string for an occurrence of the character, returning true if one is found and false otherwise.
boolean Search(java.lang.String s, java.lang.Character c); [5 marks]