You are on page 1of 23

Bank Account

Inheritance in Java is same as that of inheritance in real Life. A class which inherits another class
obtains all the latter's attributes and methods. The former is called Child class whilst the latter is called
Parent class. This phenomenon would be very promising in applications dealing with multiple classes
that are constituted by similar or more likely same attributes. You 'll get to know the importance of
inheritance from the following problem. All type of accounts in a bank have common attributes which
can be inherited from an Account class.
 
Create a class Account with the following protected attributes

Attributes Datatype
accName String
accNo String
bankName String
Include appropriate getters and setters.
Include the following protected methods.
Method Description
void display() This protected method displays the account details

Create a class CurrentAccount with following private attributes which extends Account class
Attributes Datatype
tinNumber String
Create default constructor and a parameterized constructor with arguments in
order CurrentAccount(String accName,String accNo,String bankName,String tinNumber).
Include appropriate getters and setters.
Include the following public methods.
Method Description
This method calls the super class display().
void display() This public method displays the TIN number.
Call this method with the reference of base class.

Create a class SavingsAccount with following private attributes which extends Account class
Attributes Datatype
orgName String
Create default constructor and a parameterized constructor with arguments in
order SavingsAccount(String accName,String accNo,String bankName,String orgName).
Include appropriate getters and setters.

Include the following public methods.


Method Description
This method calls the super class display().
void display()
This public method displays the Organisation Name.
Call this method with the reference of base class.

Create a driver class named Main to test the above class.

Note:
  Strictly adhere to the Object-Oriented Specifications given in the problem statement.All class names,
attribute names and method names should be the same as specified in the problem statement.

Input Format:
The first input corresponds to choose current or savings account
The next line consists of account name,account number,bank name,org name or tin number (according
to chosen account type)

Output Format
The output consists of account details  and TIN number or Organisation name
Refer sample output for formatting specifications.

Sample Input/Output-1:
Choose Account Type
1.Savings Account
2.Current Account
1
Enter Account details in comma separated(Account Name,Account Number,Bank Name,Organisation
Name)
Morsh,033808020000879,Baroda,Amphisoft
Account Name:Morsh
Account Number:033808020000879
Bank Name:Baroda
Organisation Name:Amphisoft

Sample Input/Output-2:
Choose Account Type
1.Savings Account
2.Current Account
2
Enter Account details in comma separated(Account Name,Account Number,Bank Name,TIN Number)
Krish,131231451,ICICI,798902
Account Name:Krish
Account Number:131231451
Bank Name:ICICI
TIN Number:798902
super() method
So far you have learned about basics of inheritance and created 2 child class for a parent class and
displayed the details. Now let's go for simple manipulation along with super class. This will be needed
in our application as some class share common attributes so they can be grouped as child classes of
some super class.

To try this let's create a parent class Event with following attributes,


Attributes Datatype
name String
detail String
type String
ownerName String
costPerDay Double

Then create child class Exhibition that extends Event with the following attribute,
Attributes Datatype
noOfStall Integer

And create another child class StageEvent that extends Event with the following attribute,
Attributes Datatype
noOfSeats Integer

Add suitable constructor (with super() if necessary) and getters/setters for the classes.

Get starting and ending date of the event from user and calculate the total cost for the event. Then
calculate GST for the event according to the event type.

GST is 5% for Exhibition and 15% for StageEvent.

Refer sample input/output for other further details and format of the output.

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:

Enter your choice:


1.Exhibition event
2.Stage event
1
Enter the details of exhibition:
Science Fair,Exciting experiments,Fair,John,10000.00,10
Enter the starting date of the event:
03-01-2018
Enter the ending date of the event:
06-01-2018
The GST to be paid is Rs.1500.0

Sample Input/Output 2:

Enter your choice:


1.Exhibition event
2.Stage event
2
Enter the details of stage event:
Movie Award Function,Awards for all category,Award function,Joe,100000,10000
Enter the starting date of the event:
07-01-2018
Enter the ending date of the event:
09-01-2018
The GST to be paid is Rs.30000.0
Super() keyword - Constructor

Like explained earlier, we use Inheritance to group classes under some common Class for code
reusability. In our application, many exhibitions and Stage show may happen. Since they have common
attributes like name, type, organiser, those attributes are declared in Event class and both the Exhibition
and Stage event inherit them. You'll understand the difference between 'super' and 'this' keywords in
this problem. Let's implement them.

Create an Event class with protected attributes

Attributes Datatype
name String
detail String
type String
organiserName String

Create an Exhibition class with following private attributes

Attributes Datatype
noOfStalls Integer

Create a StageEvent class with following private attributes

Attributes Datatype
noOfSeats Integer

Now include appropriate getters and setters for all the classes.
Include a default constructor and parameterized constructor for all the classes.

The format for Parameterized constructors:

public Event(String name, String detail, String type, String organiserName)


public Exhibition(String name, String detail, String type, StringorganiserName, Integer noOfStalls)
public StageEvent(String name, String detail, String type, StringorganiserName, Integer noOfSeats)

Create a driver class called Main. In the Main method, obtain input from the user and
create StageEvent or Exhibition objects accordingly. At last print the details of the object appropriately
(Refer Sample I/O).
Note: If choice other than specified is chosen, print "Invalid choice" and terminate.
[Strictly adhere to the Object-Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem
statement.]

[All text in bold corresponds to the input and rest corresponds to output]
Sample Input/Output 1:

Choose Event type


1.Exhibition
2.StageEvent
1
Enter the details in CSV format
Book expo,special sale,Academics,Mahesh,10
Event Name:Book expo
Detail:special sale
Type:Academics
Organiser Name:Mahesh
Number of stalls:10

Sample Input/Output 2:

Choose Event type


1.Exhibition
2.StageEvent
2
Enter the details in CSV format
JJ magic show,New Year Special,Comedy magic,J Jegadeesh,100
Event Name:JJ magic show
Detail:New Year Special
Type:Comedy magic
Organiser Name:J Jegadeesh
Number of seats:100

Sample Input/Output 3:
Choose Event type
1.Exhibition
2.StageEvent
3
Invalid choice
Account Details
So far we have studied about inheritance of a class by another. Now take it to the next level by having
multilevel inheritance. This is inheriting a class which has already inherited another. So before going to
our application taking this fresh concept, let's try it out in a simple example first. We can create 3
classes where one is the parent, next is the child and the third is the child of the child class. Take an
example of account handling application, where you get details from the user in comma separated
format and save them as objects and then display them.

Create a class Account with the following protected attributes,


Attributes Datatype
accountNumber String
balance Double
accountHolderName String

Create a class SavingAccount which extends Account with the following protected attributes,


Attributes Datatype
minimunBalance Double

Create a class FixedAccount which extends SavingAccount with the following private attributes,


Attributes Datatype
lockingPeriod Integer

Create a class AccountBO with the following methods,


Method Name Description
This method takes the account detail String as the value, then split the
public FixedAccount
string and get all the details, create an object for FixedAccount and
getAccountDetail(String detail)
return the FixedAccount object.

Get the FixedAccount detail from the user as a comma seperated value, the account detail should be
given in the below format,
accountNumber,balance,accountHolderName,minimunBalance,lockingPeriod
Split the details and create an object for FixedAccount and Display the details in the below format,
System.out.format("%-20s %-10s %-20s %-20s %s\n","Account Number","Balance","Account holder
name","Minimum balance","Locking period")

Create a driver class Main to test the above classes.

[Note: Strictly adhere to the object oriented specifications given as a part of the problem statement.Use


the same class names, attribute names and method names]

Input Format
The first line of the input is an Integer corresponds to the fixed account detail.
The account detail should be given in the below format,
accountNumber,balance,accountHolderName,minimunBalance,lockingPeriod
Output Format
The output consists of fixed account detail.
Refer sample output for formatting specifications.

[All text in bold corresponds to input and rest corresponds to output] 


Sample Input/Output:
Enter account Detail:
ACC001,5456.45,Tony Blake,500,10
Account Details:
Account Number Balance Account holder name Minimum balance Locking period
ACC001 5456.45 Tony Blake 500.0 10
Multilevel inheritance
Now we have gotten acquainted ourselves with multilevel inheritance, we can move on implementing
in it our application. Let's consider the domain 'Stall' and its types. There exists a multilevel
inheritance.

Create a class SilverStall with following protected attributes


 

Attributes Datatype
name String
detail String
owner String
cost Integer
Create default constructor and a parameterized constructor with arguments in order SilverStall(String
name, String detail, String owner, Integer cost).
Include appropriate getters and setters.
Total cost computes only the stall cost.

Create a class GoldStall which extends SilverStall with following private attributes
 

Attributes Datatype
tvSet Integer
Create default constructor and a parameterized constructor with arguments in order GoldStall(String
name, String detail, String owner, Integer cost, Integer tvSet).
Include appropriate getters and setters.
Total cost computed by stall cost and TV set cost. Each TV set costs 100Rs.

Create a class PlatinumStall which extends GoldStall (i.e Multilevel Inheritance) with following private
attributes
 

Attributes Datatype
projector Integer
Create default constructor and a parameterized constructor with arguments in
order PlatinumStall(String name, String detail, String owner, Integer cost, Integer tvSet, Integer
projector).
Include appropriate getters and setters.
Total cost computed by stall cost,TV set cost and projector cost where each TV set and projector costs
100Rs and 500Rs respectively.
Create a driver class named Main to test the above class.

Note:
  Strictly adhere to the Object-Oriented Specifications given in the problem statement.All class names,
attribute names and method names should be the same as specified in the problem statement.

Input Format:
The first input corresponds to choose from the menu which contains silver stall,gold stall and platinum
stall.
The next line of input corresponds to the details of the stalls in CSV format.

Output Format
The output consists of stall details.
Refer sample output for formatting specifications.

Sample Input/Output-1:
Choose Stall Type
1.Silver Stall
2.Gold Stall
3.Platinum Stall
1
Enter Stall details in comma separated(Stall Name,Stall Description,Owner Name,Stall Cost)
Fruits,Fruits are good for health,Kishore,2000
Stall Name:Fruits
Details:Fruits are good for health
Owner Name:Kishore
Total Cost:2000

Sample Input/Output-2:
Choose Stall Type
1.Silver Stall
2.Gold Stall
3.Platinum Stall
2
Enter Stall details in comma separated(Stall Name,Stall Description,Owner Name,Stall Cost,Number of
TV set)
Junk Foods,Tastier world,JM,5000,7
Stall Name:Junk Foods
Details:Tastier world
Owner Name:JM
TV Sets:7
Total Cost:5700

Sample Input/Output-3:
Choose Stall Type
1.Silver Stall
2.Gold Stall
3.Platinum Stall
3
Enter Stall details in comma separated(Stall Name,Stall Description,Owner Name,Stall Cost,Number of
TV set,Number of Projectors)
Vehicular,Engines are the best,Raizak,6000,5,3
Stall Name:Vehicular
Details:Engines are the best
Owner Name:Raizak
TV Sets:5
Projectors:3
Total Cost:8000

Sample Input/Output-4:
Choose Stall Type
1.Silver Stall
2.Gold Stall
3.Platinum Stall
4
Overloading-basic
  
Whenever you need to perform the same operation for different types of data, using different method
names for each type can be cumbersome. In these cases we use overloading. Method overloading is the
ability to create multiple methods with same name and different types of parameters. Calls to an
overloaded function will run a specific implementation of that method appropriate to the type of
arguments.
    

    For example, Consider Arrays.sort() method it is overloaded for different types of data like int,
float, double etc.
sort(double[] a)
sort(int[] a)

So now let's try out a simple example in our application. Consider stalls, they can be "gold",
"Diamond","Platinum" types. And they may or may not have a TV in them. So we are gonna overload a
method to find the cost of the stall. Create a class Stall with following attributes,
 
Attribute Datatype
name String
detail String
ownerName String

Mark them as protected and add appropriate default/parameterized constructor and getters/setters.   


Override the following method in it,
 
Method Description
public Double computeCost(String stallType, Integer calculate the cost of the stall based on
squareFeet) square feet
public Double computeCost(String stallType, Integer calculate the cost based on size and
squareFeet, Integer numberOfTV) number of TV
  
 Note: The price for various types of stalls is, Platinum = Rs.200 per sqft; Diamond = Rs.150 per sqft;
Gold = Rs.100 per sqft. And price for each tv is Rs.10000.

 Refer sample input/output for further details and format of the output.

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:
Enter the name of the stall:
ABC ltd
Enter the detail of the stall:
All electronics store
Enter the owner name of the stall:
XYZ
Enter the type of the stall:
Platinum
Enter the size of the stall in square feet:
1000
Does the hall have TV?(y/n)
y
Enter the number of TV:
4
The cost of the stall is 240000.0
Overloading makePayment()
Consider doing an extra feature for the stage show organisers. Bring up an interactive console
application for Billing so that our application looks unique from other competitors. Customers pay
using cash, online wallets, and credit card. For each category obtain necessary information from the
user. You also require a receipt for all the transactions which should be printed at the end of the
transaction. Let's increase our coding proficiency by implement Function overloading for the payments.
Hence write a program meeting all the above specification.

Create a class named TicketBooking with the following private attributes.


Attributes Datatype
stageEvent String
customer String
noOfSeats Integer

Include getters and setters for the class.


Include default and parameterized constructors.
Format for parameterized constructor is TicketBooking(String stageEvent, String customer, Integer
noOfSeats)

The TicketBooking class has the following methods.


Method Name Description
This method is for cash payment. This method accepts
public void makePayment(Double amount) amount as input and displays the transaction detail
(Refer sample I/O)
public void This method is for wallet payment. This method accepts
makePayment(StringwalletNumber ,Doublea amount and wallet number as input and displays the
mount) transaction detail (Refer sample I/O)
This method is for credit card payment. This method
public void
accepts credit card detail,ccv, card holder
makePayment(StringcreditCard,Stringccv,Stri
name ,and amount as input and displays the transaction
ng name,Double amount)
detail (Refer sample I/O)
 

 
Create a driver class called Main. In the Main method, obtain input from the user in CSV format and
call appropriate methods for transactions. If choice other than specified is chosen, print "Invalid
choice".
Note: display one digit after decimal point for double values.
Format for TicketBooking Input is stageEvent,customer,noOfSeats
[Strictly adhere to the Object-Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem
statement.]
[All text in bold corresponds to the input and rest corresponds to the output]
Sample Input/Output 1:

Enter the Booking details


Magic show,Mahesh,5
Payment mode
1.Cash payment
2.Wallet payment
3.Credit card payment
1
Enter the amount
500
Stage event:Magic show
Customer:Mahesh
Number of seats:5
Amount 500.0 paid in cash

Sample Input/Output 2:

Enter the Booking details


Motivational speech,Rajesh,10
Payment mode
1.Cash payment
2.Wallet payment
3.Credit card payment
2
Enter the amount
400
Enter the wallet number
AFG-456
Stage event:Motivational speech
Customer:Rajesh
Number of seats:10
Amount 400.0 paid using wallet number AFG-456

Sample Input/Output 3:

Enter the Booking details


Debate,Raja,2
Payment mode
1.Cash payment
2.Wallet payment
3.Credit card payment
3
Enter card holder name
Raja
Enter the amount
200
Enter the credit card type
Master
Enter the CCV number
9874-4758-9856
Stage event:Debate
Customer:Raja
Number of seats:2
Holder name:Raja
Amount 200.0 paid using Master card
CCV:9874-4758-9856
Area of shape
We had learned about function overloading in the last few problems, let's explore function overriding
with some examples. Unlike function overloading, the characteristics of the function do not change ie.
the function has the same number of arguments and the same type of arguments. The function which is
overridden has a different functionality than all of its counterparts. The following example would set
you up for what is function overriding.

Create a class Shape with the following protected attributes,


Attributes Datatype
area Double

Create the following method in the Shape class,


Method Name Description
public void computeArea() In this method assign area to 0.

Create a class Circle which extends Shape with the following private attributes,


Attributes Datatype
radius Double

Override the following method in the Circle class,


Method Name Description
 Here Calculate the area of the circle and assign the value to the area
public void computeArea() attribute.
Area of the circle = (22/7) * (radius * radius)

Create a class Rectangle which extends Shape with the following private attributes,


Attributes Datatype
length Double
breadth Double

Override the following method in the Rectangle class,


Method Name Description
Here Calculate the area of the rectangle and assign the value to the area
public void
attribute.
computeArea()
Area of the rectangle = (length * breadth)

Create a class Triangle which extends Shape with the following private attributes,


Attributes Datatype
base Double
height Double

Override the following method in the Triangle class,


Method Name Description
Here Calculate the area of the triangle and assign the value to the area
public void computeArea() attribute.
Area of the triangle = (1/2) * (base * height)

Get the option for the shape to compute area and get the attribute according to the shape option.
Calculate the area and print the area.
While printing round off the area to 2 decimal formats.

Create a driver class Main to test the above classes.

[Note: Strictly adhere to the object-oriented specifications given as a part of the problem statement.Use


the same class names, attribute names and method names]

Input Format
The first line of the input is an Integer corresponds to the shape.
The following inputs are Double which corresponds to the shape.
For Circle(Option 1) get the radius.
For Rectangle(Option 2) get the length and breadth.
For Triangle(Option 3) get the base and height.
Output Format
The output consists area of the shape.
Refer sample output for formatting specifications.

[All text in bold corresponds to input and rest corresponds to output] 


Sample Input/Output 1:
Enter the shape
1.Circle
2.Rectangle
3.Triangle
1
Enter the radius:
11
Area of circle is 380.29

Sample Input/Output 2:
Enter the shape
1.Circle
2.Rectangle
3.Triangle
2
Enter the length and breadth:
10
25
Area of rectangle is 250.00

Sample Input/Output 3:
Enter the shape
1.Circle
2.Rectangle
3.Triangle
3
Enter the base and height:
15
19
Area of triangle is 142.50

Sample Input/Output 4:
Enter the shape
1.Circle
2.Rectangle
3.Triangle
4
Invalid choice
Overriding-simple
Overriding is another concept that every application developer should know. Overriding is a runtime
polymorphism. The inherited class has the overridden method which has the samename as the method
in the parent class. The argument number, types or return types should not differ in any case. The
method is invoked with the object of the specific class ( but  with the reference of the parent class).

Now let's try out a simple overriding concept in our application. For this, we can take our original
example of Class Event, and its child classes Exhibition and StageEvent.

Create a parent class Event with following protected attributes,


Attributes Datatype
name String
detail String
ownerName String

Then create child class Exhibition that extends Event with the following attribute,


Attributes Datatype
noOfStall Integer

And create another child class StageEvent that extends Event with the following attribute,


Attributes Datatype
noOfShows Integer
noOfSeatsPerShow Integer

Add suitable constructor (with super() if necessary) and getters/setters for the classes. Add
method projectedRevenue() in parent and its child class.
Method Description
public Double calculate revenue and return the double value. In parent class return
projectedRevenue() just 0.0

Note: For Exhibition, each stall will produce Rs.10000 as revenue. For StageEvent, each
seat produces Rs.50 revenue.

Refer sample input/output for other further details and format of the output.

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:

Enter the name of the event:


Science Fair
Enter the detail of the event:
Explore Technology
Enter the owner name of the event:
ABCD
Enter the type of the event:
1.Exhibition
2.StageEvent
1
Enter the number of stalls:
65
The projected revenue of the event is 650000.0

Sample Input/Output 2:

Enter the name of the event:


Magic Show
Enter the detail of the event:
See Magic without Logic
Enter the owner name of the event:
SDFG
Enter the type of the event:
1.Exhibition
2.StageEvent
2
Enter the number of shows:
10
Enter the number of seats per show:
100
The projected revenue of the event is 50000.0
Reward calculation
A new announcement has been made by the Mayor, the Fair will be on for more than a month. For
rewarding customers who actively purchase in the fair, the developers are asked to compute reward
points for credit card purchasing. For a small demo implementation, we now compute reward points for
VISA card and HP VISA card. The reward points for VISA card is 1% of the spending for all kinds of
purchases. For HP Visa card, 10 additional points are given for fuel purchases. Also, include method
Overriding for the method computeRewardPoints() which computes the reward points for both types.
write a program using the above specification for computing the reward points.

Create a class named VISACard with the following private attributes


 

Attributes Datatype
holderName String
ccv String

the VISACard class has the following methods


 

Method name Method Description


public Double computeRewardPoints(String This method accepts the type and amount as inputs and
purchaseType, Double amount) displays the reward points which is 1% of the amount

Create a class named HPVISACard that extends VISACard with the following methods


 

Method name Method Description


public Double This method accepts the type and amount as inputs and
computeRewardPoints(String displays the reward points which is 1% of the amount. If the
purchaseType, Double amount) type is "Fuel" 10 more points are given.

Create a driver class called Main. In the Main method, obtain inputs from the user and compute the
reward points by calling appropriate methods. If choice other than specified is chosen, print "Invalid
choice"
Hint: Call Super() to access the computeRewardPoints in the derived class and add additional points if
given criteria qualifies.
Note: Display one digit after the decimal point for Double values.
[Strictly adhere to the Object-Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem
statement.]

[All text in bold corresponds to the input and rest corresponds to the output]
Sample Input/Output:

Enter the holder name


Mahesh
Enter the CCV number
9871-9874-4569
Enter the bill amount
1000
Mention the type of spending
Fuel
Choose card type
1.VISA card
2.HP VISA card
1
Holder name:Mahesh
CCV:9871-9874-4569
Reward points:10.0
Do you want to continue?(Y/N)
N

You might also like