TM03-Abstraction-Packages-Exception Handling
TM03-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.2. Create 2 subclasses of GeneralBank called ICICIBank and KotMBank. Override the
methods from base class. ICICI - Savings 4% Fixed 8.5% and KotMBank. - Savings 6%
Fixed 9%
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.
Solution
import java.util.*;
abstract class GeneralBank
{
public abstract float getSavingInterestRate ();
public abstract float getFixedInterestRate ();
}
class ICICIBank extends GeneralBank
{
Solution
package com.automobile;
abstract class Vehicle {
public abstract String getModelName();
public abstract String getRegistrationNumber ();
public abstract String getOwnerName ();
}
public void display()
{
System.out.println(gender + " "+name+" "+email);
}
}
package com.automobile.TwoWheeler;
class Hero extends Vehicle {
public String getModelName()
{
return "Hero";
}
public String getRegistrationNumber ()
{
return "100";
}
public String getOwnerName ()
{
return "Owner1";
}
public int getSpeed()
{
return 10;
}
public void radio()
{
System.out.println("Radio Control Facility");
}
}
import com.automobile.TwoWheeler;
public class automobilemain
public static void main(String []args){
Hero h1=new Hero();
h1. getModelName();
h1.getRegistrationNumber ();
h1.getOwnerName ();
h1.getSpeed();
h1.radio();
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,
Test case #1:
Create an instance of KidUser class.
Set the age as specified in the below table and invoke the registerAccount
method of the KidUser object
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”
Solution
interface LibraryUser
{
void registerAccount();
void requestBook();
}
class KidUsers implements LibraryUser
{
int age;
String booktype;
public KidUsers(int age, String booktype)
{
this.age=age;
this. booktype= booktype;
}
public void registerAccount()
{
if(age<12)
System.out.println("You have successfully registered under a Kids
Account");
else
System.out.println("Sorry, Age must be less than 12 to register as a
kid");
}
public void requestBook()
{
if(booktype.equals("Kids"))
System.out.println("Book Issued successfully, please return the book
within 10 days ");
else
System.out.println("Oops, you are allowed to take only kids books ");
}
}
Exercise 3 : Write a program that takes input as the size of the array
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. Index starts from zero.
This program may generate Array Index Out Of Bounds Exception or NumberFormatException . Use exception handling mechanisms to
handle this exception.
import java.util.*;
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
Solution
import java.util.*;