A superclass Hotel has been defined to store the details of a hotel. Define a subclass Customer to…
A superclass Hotel has been defined to store the details of a hotel. Define a subclass Customer to calculate the total bill for a customer as per the following criteria:
The details of the members of both the classes are given below:
Class name : Hotel
Data members/instance variables:
hname : to store hotel name
roomrent : to store the rent per day
Methods/Member functions:
Hotel(...) : parameterised constructor to assign values to its data members
void show( ) : to display hotel details
Class name : Customer
Data members/instance variables:
cname : to store customer name
days : to store the number of days of stay
type : to store the room type
surcharge : to store the additional amount
amt : to store the total amount
Methods/Member functions:
Customer(...) : parameterised constructor to assign values to data members of both the classes
void compute( ) : to calculate the surcharge based on the room type as given above. Also, to calculate the total amount as:
(room rent + surcharge) × days
void show( ) : to display hotel and customer details
Assume that the superclass Hotel has been defined. Using the concept of Inheritance, specify the class Customer, giving details of constructor(...), void compute( ) and void show( ).
The super class, main function and algorithm need NOT be written.
| Room type | Additional Amount |
| Executive | 10% of room rent |
| Suite | 20% of room rent |
Answer
Answer
AIWritten by AI (antigravity) - it can contain mistakes.
class Customer extends Hotel
{
String cname;
int days;
String type;
double surcharge;
double amt;
Customer(String h, double r, String c, int d, String t)
{
super(h, r);
cname = c;
days = d;
type = t;
surcharge = 0.0;
amt = 0.0;
}
void compute()
{
if (type.equalsIgnoreCase("Executive"))
{
surcharge = 0.10 * roomrent;
}
else if (type.equalsIgnoreCase("Suite"))
{
surcharge = 0.20 * roomrent;
}
else
{
surcharge = 0.0;
}
amt = (roomrent + surcharge) * days;
}
void show()
{
super.show();
System.out.println("Customer Name: " + cname);
System.out.println("Days of Stay: " + days);
System.out.println("Room Type: " + type);
System.out.println("Surcharge per day: " + surcharge);
System.out.println("Total Amount: " + amt);
}
}Explanation: Customer extends Hotel. The constructor invokes super(h, r) to initialise superclass members and initialises its own data members. compute() calculates surcharge based on room type and computes total amount as (roomrent + surcharge) * days. show() calls super.show() and displays customer details.From ISC 2026 Computer Science Paper 1, question 10.