19.
Write a class with the name Area using method overloading that computes the area of a
parallelogram, a rhombus, and a trapezium. Write a main method to call the overloaded methods.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)
Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance between the parallel sides)
20. A superclass Bank has been defined to store the details of a customer. Define a sub-class
Account that enables transactions for the customer with the bank. The details of both the classes
are given below:
Class name: Bank
Data members/instance variables:
name : stores the name of the customer
accno : stores the account number
P : stores the principal amount in decimals
Member functions/methods:
Bank(….) : parameterized constructor to assign values to the instance variables
void display () : displays the details of the customer
Class name : Account
Data member/instance variable:
Amt : stores the transaction amount in decimals
Member functions/methods:
Account(…) : parameterized constructor to assign values to the instance variables of
both the classes
void deposit() : accepts the amount and updates the principal as p=p+amt
void withdraw() : accepts the amount and updates the principal as p=p-amt
If the withdrawal amount is more than the principal amount, then display the message
“INSUFFICIENT BALANCE”.
If the principal amount after withdrawal is less than 500, then a penalty is imposed by using the
formula : p=p-(500-p)/10
void display() : displays the details of the customer
Assume that the superclass Bank has been defined.
Using the concept of Inheritance; specify the class Account giving details of the constructor(…),
void deposit(), void withdraw() and void display() The superclass and the main function need
not be written.
21. An interface Data is defined with a data member and a method volume( ) which returns
the volume of the implementing shape. A super class Base has been defined to contain the radius
of a geometrical shape. Define a sub class CalVol which uses the properties of the interface
Data and the class Base and calculates the volume of a cylinder.
The details of the members of the interface and both the classes are given below:
Interface name : Data
Data member:
double pi : initialize pi = 3.142
Member functions/methods:
double volume( ) :
Class name : Base
Data member/instance variable:
rad : to store the radius in decimal
Member functions/methods:
Base(...) : parameterized constructor to initialize the data member
void show( ) : displays the radius with an appropriate message
Class name : CalVol
Data member/instance variable:
ht : to store the height in decimal
Member functions/methods:
CalVol(...) : parameterized constructor to initialize the data members
of both the classes
double volume( ) : calculates the volume of a sphere by using the formula
( pi x radius2 x height )
void show( ) : displays the data members of both the classes and the
volume of the sphere with appropriate message
Assume that the interface Data and the super class Base has been defined. Using the
concept of inheritance, specify the class CalVol giving the details of the constructor(...),
double volume( ) and void show( ).
The interface, super class, main function, and algorithm need NOT be written.
22. program 6, page no 572 (Abstract class)
22. Write a Java program to create an abstract class BankAccount with abstract methods
printdata(). Create subclasses: SavingsAccount and CurrentAccount that extend the
BankAccount class and implement the printdata() methods for each account type.
Abstract class name : BankAccount
Data member:
String name : Stores the customer’s name
double balance : Stores balance of the customer
Member functions/methods:
void printdata : An abstract method to print the details
Class name : SavingsAccount
Data member/instance variable:
double withdraw : to store the money withdrawm
double deposit : To store the money deposited
Member functions/methods:
void read(…….) : Parameterized Method to initialized customer’s name, balance,
withdraw and deposit.
void callbalance( ) : Calculate the balance after withdraw or deposite
void printdata() : Print the account holder’s name and available balance.
Class name : CurrentAccount
Data member/instance variable:
double withdraw : to store the money withdrawm
double deposit : To store the money deposited
Member functions/methods:
void accept(…….) : Parameterized Method to initialized customer’s name, balance,
withdraw and deposit.
void callbalance( ) : Calculate the balance after withdraw or deposite
void printdata() : Print the account holder’s name and available balance.
Using the concept of inheritance, specify the class CalVol giving the details of the
constructor(...),
double volume( ) and void show( ).
The interface, super class, main function, and algorithm need NOT be written.