Java Questions
Java Questions
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.
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.
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.
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:
Sample Input/Output 2:
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.
Attributes Datatype
name String
detail String
type String
organiserName String
Attributes Datatype
noOfStalls Integer
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.
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:
Sample Input/Output 2:
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.
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")
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.
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
[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 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:
Sample Input/Output 2:
Sample Input/Output 3:
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.
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.
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.
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.
[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:
Sample Input/Output 2:
Attributes Datatype
holderName String
ccv String
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: