August 2000 QUESTION 3 Total Marks: 15 Marks |
Click here to access
other questions
SUGGESTED SOLUTIONS |
(a) State what is meant by an exception
in a Java program, and give an example of a situation which might
reasonably be expected to result in an exception. [2 marks] (b) Define a method, called Divide,
which takes as parameters two doubles, x and y. The method should
first ensure that y is non zero, returning the result of dividing
x by y if it is not, and throwing the relevant ArithmeticException
if it is. [5 marks] (c) Define a method, called FooBar,
which will call the method Divide with the parameters a and b. Should
the calling of Divide result in an exception, FooBar should indicate
this by printing out the appropriate error message. If the method
has attempted to call Divide, it should also print out a message stating
this, regardless of success or failure. [4 marks] (d) Declare an exception class called
MyArithmeticException, which extends the standard ArithmeticException.
The class should contain the member string Reason, and a constructor
function which takes as a parameter a string, and initialises Reason
appropriately. [4 marks] |