Advertisements
Advertisements
Question
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:
| Room type | Additional Amount |
| Executive | 10% of room rent |
| Suite | 20% of room rent |
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) x 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.
Code Writing
Advertisements
Solution
class Customer extends Hotel {
String cname;
int days;
String type;
double surcharge;
double amt;
Customer(String hname, double roomrent, String cname, int days, String type) {
super(hname, roomrent);
this.cname = cname;
this.days = days;
this.type = type;
}
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: " + surcharge);
System.out.println("Total Amount: " + amt);
}
}shaalaa.com
Is there an error in this question or solution?
