हिंदी

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 - Computer Science (Theory)

Advertisements
Advertisements

प्रश्न

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.

कोड लेखन
Advertisements

उत्तर

/public class Passenger extends Flight {
  // Instance variables
  private String id;     // Passenger ID
  private String name;   // Passenger name
  private double tax;    // Calculated tax amount
  private double tot;    // Total amount to be paid
                         (base fare + tax)
  public Passenger(String fno, String dep, String arr,
  double fare, String pid, String pname) {
  // Call superclass constructor to initialize Flight
  details
  super(fno, dep, arr, fare);
  this.id = pid;
  this.name = pname;
  this.tax = 0.0;
  this.tot = 0.0;
}
public void cal() {
  // Calculate tax as 5% of base fare (from superclass)
  this.tax = 0.05 * super.base fare;
  //Calculate total amount (base fare + tax)
  this.tot = super.base fare + this.tax;
}
/**
  *Method to display flight details, passenger details,
  and total amount
  *Overrides show() method from Flight superclass
  */
public void show() {
     // Display flight details using superclass show()
     method
     super.show();
     // Display passenger details
     System.out.println("Passenger ID: " + this.id);
     System.out.println("Passenger Name: "+ this.
     name);
     // Display calculated amounts
     System.out.println("Tax Amount: " + this.tax);
     System.out.println("Total Amount Payable: "+
     this.tot);
   }
}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (March) Official Board
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×