August 2000
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) Java is an object oriented language. Give two main characteristics of an object oriented language. [2 marks]
(a) Any two of classes, objects, polymorphism, encapsulation, or other sensible alternative correct answers. [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]
(b) (i) Available to all other classes which can see this class. [1 mark]
(ii) Available to derived classes, and others within the same package. [1 mark]
(iii) Available only to classes of the same type. [1 mark]

(c) Explain the difference between a class and an object. [2 marks]
(c) • A class is a definition of an abstract data type; (1 mark)
• An object is an instantiation of a class; (1 mark)

(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]
(d) (i) public class Book {
  private int NumberPages;
  private Page Pages[];
}
• Correct structure to the class declaration; (1 mark)
• Declaring the members private; (1 mark)
• Correct definition for the number of pages; (1 mark)
• Correct array definition; (1 mark)
[4 marks]
(ii) Book(int n, Page[] p, boolean b) {
  NumberPages= n;
  Pages= new Page[100];
  if(b) Pages= p;
}
• Creating the array elements; (1 mark)
• NumberPages initialisation; (1 mark)
• Conditional statement; (1 mark)
• Array initialisation; (1 mark)
[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]
(e) (i) false [1 mark]
(ii) 0 [1 mark]
(iii) 8 [1 mark]
(iv) 15 [1 mark]
(v) 16 [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]
(f) (i) int Power(int mantissa, int exponent) {
  if(exponent== 1) {
  return mantissa;
  } else {
  return mantissa * Power(mantissa, exponent-1);
  }
}
• Testing for the base case exponent== 1; (1 mark)
• Returning 1 in the event of the base case; (1 mark)
• Recursing when not the base case; (1 mark)
• With the correct parameters; (1 mark)
• And returning the mantissa * the recursive call; (1 mark)
[5 marks]
(ii) boolean Search(java.lang.String s, java.lang.Character c) {
  for(i= 0; i< s.Length(); i++) {
  if(s.charAt(i)== c)
  return true;
  }
  return false;
}
• A suitable iterative structure; (1 mark)
• Bound by s.Length(); (1 mark)
• Obtaining the correct character with s.charAt(); (1 mark)
• A suitable conditional statement; (1 mark)
• Returning the correct values; (1 mark)
[5 marks]