Activity 1: Exploring Exceptions and Assertions
Activity 1: Exploring Exceptions and Assertions
Activity Objective
Activity Description
Use the java api and the material given to understand the concepts of exception handling and complete the
activity given.
Hints:
charAt(int) – method in String Builder class that identifies with the character at the specified
position. For example : sb.charAt(2) – character at the second position .
toString() - Returns a string that contains the character sequence in the builder.
1
GreenField Foundation: Java Activity 1: Exploring Exceptions and Assertions
Participant Instructions:
Exception Handling
Create a class “Console_Exception” to accept arguments at run time and print the first argument
supplied. Handle the exception encountered.
Create a class “EvenNum_Exception” to accept “n” even numbers in an array. If an odd number is
supplied throw a customized exception,handle it appropriately and inform the user of the exit of the
program.
Create a class “ReverseString_Exception” to reverse the letters of a string variable initialized to
null.Handle the corresponding unchecked exception. (Hint: Use StringBuilder for the reverse
manuipulation.)
Create a class “Divide_Exception” to accept two integer numbers from the user for performing a
divide operation.Perform the following for the code:
a. If the second number is zero, throw a customized exception “NumberNotValidException”.
b. If user enters a letter, instead of a number, catch the appropriate Exception
c. Add the appropriate exception handlers when float numbers are entered instead of integers.
Create a class parent class Mammal , from which Human class is derived. Create instances of the
both the classes in a new class.Convert Mammal object to Human object by explicit casting and
remove compile time errors. Execute the program to handle the appropriate exceptions.
Using Assertions
Create a class “Average_Assert” to take name, marks of maths and science of a student and find
the average of the same.If the average is above 60, then print “pass” else print “fail”.
Create a class “NumberSquare_Assert” to calculate the square of a number which does not exceed
600.
Create a “WordLength_Assert” to enter a eight letter word. Using assertions, print out an
appropriate message if the length of the word is not as per requirement.