‹ Back to the paper
A superclass Flight has been defined to store the details of a flight. Define a subclass Passenger…
A superclass Flight has been defined to store the details of a flight. Define a subclass Passenger to calculate the fare for a passenger.
The details of the members of both the classes are given below:
Class name : Flight
Data members/instance variables:
flightno : to store the flight number in string
dep_time : to store the departure time in string
arr_time : to store the arrival time in string
basefare : to store the base fare in decimal
Methods/Member functions:
Flight(...) : parameterised constructor to assign values to the data members
void show( ) : to display the flight details
Class name : Passenger
Data members/instance variables:
id : to store the ID of the passenger
name : to store the name of the passenger
tax : to store the tax to be paid in decimal
tot : to store the total amount to be paid in decimal
Methods/Member functions:
Passenger(...) : parameterised constructor to assign values to the data members of both the classes
void cal( ) : to calculate the tax as 5% of base fare and total amount (base fare + tax)
void show( ) : to display the flight details along with the passenger details and total amount to be paid
Assume that the super class Flight has been defined. Using the concepts of Inheritance, specify the class Passenger giving the details of constructor(...), void cal( ) and void show( ).
The super class, main function and algorithm need NOT be written.
Answer
Answer
AIclass Passenger extends Flight
{
int id;
String name;
double tax, tot;
Passenger(String fn, String dt, String at, double bf, int i, String n)
{
super(fn, dt, at, bf);
id = i;
name = n;
tax = 0.0;
tot = 0.0;
}
void cal()
{
tax = 0.05 * basefare;
tot = basefare + tax;
}
void show()
{
super.show();
System.out.println("Passenger ID: " + id);
System.out.println("Passenger Name: " + name);
System.out.println("Tax: " + tax);
System.out.println("Total Amount: " + tot);
}
}Explanation: Passenger extends Flight. Its constructor calls super(...) to initialise the inherited Flight data members, then sets its own. cal() computes tax as 5% of basefare (inherited from Flight) and the total as basefare+tax. show() calls super.show() to print the flight details, then prints the passenger details and total. Tested (with a stub Flight class and main) - correctly prints flight details, tax = 250.0 and total = 5250.0 for a base fare of 5000.0.From ISC 2025 Computer Science Paper 1, question 10.