Monday, 11 November 2013

Lab 14 - Exception


You  will practice how to report and handle an error condition.
1. Create a custom class named DivideException that extend exception.
2. Create a class MyMath and type in the following code:

public class MyMath
{
public static void main(String[] args)

{
int quotient = divide(10, 5);
System.out.println(quotient);
quotient = divide(10, 0);
System.out.println(quotient);
quotient = divide(10, 1);
System.out.println(quotient);
}
public static int divide(int dividend, int divisor)
{

    return (int) dividend/divisor;
}
}



3.  Make changes to the divide  method to throw the custom DivideException with message of "Cannot divide lor"
4. Catch the specific error accordingly and print the error message.

Lab 14- Recursive

Using recursive style , create a class that receive from a user a string and a number   then  have recursive  methods that :

1.  reverse the string recursively.  eg prog  become gorp
2.  count  the number of character in the screen recursively  eg.  prog = 4
3.  using a number entered , create a fibonacci  recursive  that return the result of that particular position of the number you entered.  eg 0 = 0 , 2 = 1 , 3 =3