‹ Back to the paper
A superclass Train has been defined to store the details of a train for booking tickets. Define a…
A superclass Train has been defined to store the details of a train for booking tickets. Define a subclass Booking to compute the total cost of the tickets based on the number of passengers and any discount for group bookings.
The details of the members of both the classes are given below:
Class name : Train
Data members/instance variables:
trainName : to store the name of the train
trainNumber : to store the train number
ticketPrice : to store the price of a single ticket in decimal
Methods/Member functions:
Train(...) : parameterised constructor to assign values to data members
void displayDetails( ) : to display the train details
Class name : Booking
Data members/instance variables:
numPassengers : to store the number of passengers for booking
groupDiscount : to store the group discount percentage
totalCost : to store the total cost after applying the group discount in decimal
Methods/Member functions:
Booking(...) : parameterised constructor to assign values to data members of both the classes
void calCost( ) : to calculate the cost as per the formula (ticket price * numPassengers), apply a group discount of 10% if numPassengers is greater than 5 and calculate the total cost with discount, if any
void displayDetails( ) : to display the train details, number of passengers, group discount and the total cost with discount, if any
Assume that the superclass Train has been defined. Using the concept of Inheritance, specify the class Booking giving the details of the constructor(...), void calCost( ) and void displayDetails( ).
The superclass, main function and algorithm need NOT be written.
Answer
Answer
AIclass Booking extends Train
{
int numPassengers;
double groupDiscount;
double totalCost;
Booking(String tn, int tno, double tp, int np)
{
super(tn, tno, tp);
numPassengers = np;
groupDiscount = 0.0;
totalCost = 0.0;
}
void calCost()
{
totalCost = ticketPrice * numPassengers;
if (numPassengers > 5)
{
groupDiscount = 10.0;
totalCost = totalCost - (totalCost * groupDiscount / 100);
}
}
void displayDetails()
{
super.displayDetails();
System.out.println("Number of Passengers: " + numPassengers);
System.out.println("Group Discount: " + groupDiscount + "%");
System.out.println("Total Cost: " + totalCost);
}
}Explanation: Booking extends Train, so it inherits trainName, trainNumber and ticketPrice. Its constructor calls super(...) to initialise the inherited members and then sets its own. calCost() computes the base cost (ticketPrice * numPassengers) and applies a 10% group discount only if numPassengers > 5. displayDetails() calls super.displayDetails() to show the train details, then prints the booking-specific details. Tested (with a stub Train class and main): for ticketPrice=1500.0 and numPassengers=7 (>5), the program correctly computes groupDiscount=10.0% and totalCost = 1500*7 - 10% = 9450.0.From ISC 2025 Improvement Computer Science Paper 1, question 10.