Abstraction Packages & Exception Handling
Abstraction Packages & Exception Handling
1.1. Create a class called GeneralBank which acts as base class for all
banks. This class has functionality getSavingInterestRate and
getFixedInterestRate methods, which return the saving a/c rate of
interest and fixed account rate of interest the specific bank gives.
Since GeneralBank cannot say what percentage which bank would give, make
it abstract.
1.3. Create a main method to test the above classes. Try one by one and
absorb your finding.
a) ICICIBank object reference instantiated with ICICIBank class.
b) KotMBank object reference instantiated with KotMBank class.
c) GeneralBank object reference instantiated with KotMBank class.
d) GeneralBank object reference instantiated with ICICIBank class.
Interfaces
No. Hands-on Assignment Topics Covered Status
Any user who is less than 12 years of age will be registered as a child
and they can borrow a “Kids” category book for 10 days, whereas an adult
can borrow “Fiction” category books which need to be returned within 7
days.
4. The methods in the KidUser class should perform the following logic.
registerAccount function:
if age < 12, a message displaying “You have successfully registered under
a Kids Account” should be displayed in the console.
If(age>12), a message displaying, “Sorry, Age must be less than 12 to
register as a kid” should be displayed in the console.
requestBook function:
if bookType is “Kids”, a message displaying “Book Issued successfully,
please return the book within 10 days” should be displayed in the
console.
Else, a message displaying, “Oops, you are allowed to take only kids
books” should be displayed in the console.
5. The methods in the AdultUser class should perform the following logic.
registerAccount function:
if age > 12, a message displaying “You have successfully registered under
an Adult Account” should be displayed in the console.
If age<12, a message displaying, “Sorry, Age must be greater than 12 to
register as an adult” should be displayed in the console.
requestBook function:
if bookType is “Fiction”, a message displaying “Book Issued successfully,
please return the book within 7 days” should be displayed in the console.
Else, a message displaying, “Oops, you are allowed to take only adult
Fiction books” should be displayed in the console.
6. Create a class “LibraryInterfaceDemo.java” with a main method which
performs the below functions,
Age
10
18
Set the book Type as specified in the below table and invoke the
requestBook method of the KidUser object,
BookType
“Kids”
“Fiction”
Age
5
23
Set the book Type as specified in the below table and invoke the
requestBook method of the AdultUser object
BookType
“Kids”
“Fiction”
Write a class called Veena which implements Playable interface. Let this
class be placed in a package music.string
Write a program that takes as input the size of the array and the
elements in the array. The program then asks the user to enter a
particular index and prints the element at that index.
This program may generate Array Index Out Of Bounds Exception. Use
exception handling mechanisms to handle this exception. In the catch
block, print the class name of the exception thrown.
Sample Input and Output 1:
Enter the number of elements in the array
3
Enter the elements in the array
20
90
4
Exception
Enter the index of the array element you want to access
2 Handling: Try-
2
catch
The array element at index 2 = 4
The array element successfully accessed
Write a program that takes as input the size of the array and the
elements in the array. The program then asks the user to enter a Exception
particular index and prints the element at that index. Index starts from Handling: Try-
3 zero. catch Use
multiple catch
This program may generate Array Index Out Of Bounds Exception or block
NumberFormatException . Use exception handling mechanisms to handle this
exception.
Write a Program with a division method who receives two integer numbers
and performs the division operation. The method should declare that it
5 throws
throws ArithmeticException. This exception should be handled in the main
method.
Write a Program to take care of Number Format Exception if user enters Exception
values other than integer for calculating average marks of 2 students. Handling:
6 The name of the students and marks in 3 subjects are taken from the user Throw & Used
while executing the program. Defined
In the same Program write your own Exception classes to take care of Exception
Negative values and values out of range (i.e. other than in the range of
0-100)
Write a program to accept name and age of a person from the command
prompt(passed as arguments when you execute the class) and ensure that Exception
handling: User
the age entered is >=18 and < 60.
8 Defined
Display proper error messages.
Exception &
The program must exit gracefully after displaying the error message in
throw
case the arguments passed are not proper. (Hint : Create a user defined
exception class for handling errors.)
Write a program that accepts 2 integers a and b as input and finds the
quotient of a/b.
This program may generate an Arithmetic Exception. Use exception handling
mechanisms to handle this exception. In the catch block, print the
message as shown in the sample output.
Also illustrate the use of finally block. Print the message “Inside
finally block”.
Sample Input and Output 1:
Exception
Enter the 2 numbers
9 Handling:
5
Finally block
2
The quotient of 5/2 = 2
Inside finally block
Sample Input and Output 2:
Enter the 2 numbers
5
DivideByZeroException caught
Inside finally block