April 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

Do not award half marks. Do not deduct marks for trivial syntactic errors. Alternative correct answers should be given credit, unless otherwise indicated in the marking scheme.

(a)(i)Explain briefly what is meant by the term Encapsulation and how it is used in the Java programming language.[2 marks ]
(ii)Explain briefly what is meant by the term Polymorphism and how it is used in the Java programming language.[2 marks ]
(iii)Explain briefly what is meant by the term Inheritance and how it is used in the Java programming language.[2 marks ]
(iv)Explain briefly the reasons for dividing classes into public and private
areas.[2 marks ]
(v)Explain briefly the use ofthe keyword import in a Java source code
file.[2 marks ]
(i) •Encapsulation is where the a data and the operations which act on that data type are contained within the same structure.(1 mark)
•Java uses the class to implement encapsulation.(1 mark)
[2 marks ]
(ii) •Polymorphism is where one type can be used where another type would be expected.(1 mark)
•In Java,overloading is used to implement polymorphism.(1 mark)
[2 marks ]
(iii) •Inheritance is where a new data type can be declared which encapsulates
and expands on the properties of an existing data type.(1 mark)
•In Java,it is used by constructing classes inherit from previously de .ned
classes.(1 mark)
[2 marks ]
(iv)The distinction provides data hiding,
•As we can restrict access to data and methods (1 mark)
•To the object itselfor to other sets ofclasses.(1 mark)
[2 marks ]
(v)The import statement:
•Tells the Java compiler that the specified library class is used in this file;
(1 mark)
•And allows usage of those classes with their abbreviated names.(1 mark)
[2 marks ]

(b)(i)Define a public Java class Circle which contains the following:
•The co-ordinates of the center of the circle,x and y and the radius of the
circle r which are of type double and should only be available to the object
itself;
•A constructor function,which takes as parameters values for the center of
the circle and the radius,and initialises them appropriately. [4 marks ]
(ii)Pi is a famous mathematical constant which is often used in Geometry.Add to your class a declaration of the constant Pi which should have the value 3.14
Note that the value of Pi should not be allowed to change.[3 marks ]
(iii)The circumference of a circle is calculated using the formula 2 ×Pi ×radius Add to your class a definition of the public method Circumference which will
calculate and return the circumference of the circle.[3 marks ]
(i)A sample definition of Circle follows:
public class Circle {
   private double x, y, r;
   public Circle(double a, double b, double c) {
      x= a;
      y= b;
      r= c;
   }
}
And the following marking scheme should be used:
•Declaring a correct public class structure;(1 mark)
•Declaring the correct private double member variables;(1 mark)
•Declaring the correct signature ofthe constructor function,with parameter
list;(1 mark)
•Correctly initialising the members with the parameter list passed to the
constructor function.(1 mark)
[4 marks ]
(ii)A sample declaration of Pi follows:
final double Pi= 3.14;
And the following marking scheme should be used:
•Using final to prevent the value form being subsequently modified;
(1 mark)
•Declaring Pi to be either a double or a float (1 mark)
•Declaring it to have the value 3.14 (1 mark)
[3 marks ]
(iii)A sample definition of Circumference follows:
public double Circumference() {
   return (2 * Pi * r);
}
And the following marking scheme should be used:
•Correctly declaring the function signature as public double (1 mark)
•Correctly calculating the radius using the formula 2 *Pi *r (1 mark)
•Returning the result ofthis calculation.(1 mark)
[3 marks ]

(c)(i)Using a do /while loop,write a method,called XtoY the signature of which is given below,which takes two integer parameters,x and y The method should
return the sum of all the integers in the range x to y Should the initial value of x
be greater than that of y the method should just return x.
public int XtoY(int x, int y); [5 marks ]
(ii)Using a switch statement,write a method,called AtoI the signature of which is given below,which takes in a char parameter x and an int parameter y The
value of y should be returned as 1 if the character passed is a 2 if it is b and -1
otherwise.
public void AtoI(char x, int y); [5 marks ]
(i)A sample de .nition of XtoY follows:
public int XtoY(int x, int y) {
   int sum= 0;
   do {
      sum+= x;
      x++;
   } while (x<= y);
   return sum;
}
And the following marking scheme should be used:
•Declaring a local variable sum correctly initialised;(1 mark)
•A correctly structured do while loop;(1 mark)
•With a correct condition based on x<= y (1 mark)
•Correctly updating the values of sum and x in the body of the loop;(1 mark)
•Returning sum when the loop terminates.(1 mark)
[5 marks ]
(ii)A sample definition of AtoI follows:
public void AtoI(char x, int y) {
switch(x) {
   case ’a’:
      y= 1; break;
   case ’b’:
      y= 2; break;
   default:
      y= -1; break;
   }
}
And the following marking scheme should be used:
•Correct use of the switch keyword;(1 mark)
•Correctly testing case ’a’and setting the value of y if true;(1 mark)
•Correctly testing case ’b’and setting the value of y if true;(1 mark)
•Correctly testing the default condition,and setting the value of y if
reached;(1 mark)
•Correct use of the break statement in each condition;(1 mark)
[5 marks ]