Advertisements
Advertisements
प्रश्न
A library issues books on rental basis at a 2% charge on the cost price of the book per day. As per the rules of the library, a book can be retained for 7 days without any fine. If the book is returned after 7 days, a fine will also be charged for the excess days as per the chart given below:
| Number of excess days | Fine per day (Rs.) |
| 1 to 5 | 2.00 |
| 6 to 10 | 3.00 |
| Above 10 | 5.00 |
A super class Library has been defined. Define a sub class Compute to calculate the fine and the total amount. The details of the members of both the classes are given below:
| Class name | Library |
| Data members/instance variables: | |
| name | to store the name of the book |
| author | to store the author of the book |
| p | to store the price of the book (in decimals) |
| Methods / Member functions: | |
| Library(... ) | parameterized constructor to assign values to the data members |
| void show( ) | displays the book details |
| Class name | Compute |
| Data members/instance variables: | |
| d | number of days taken in returning the book |
| f | to store the fine (in decimals) |
| Methods / Member functions: | |
| Compute(...) | parameterized constructor to assign values to the data members of both the classes |
| void fine( ) | calculates the fine for the excess days as given in the table above |
| void show() | displays the book details along with the number of days, fine and the total amount to be paid. Total amount is (2% of price of book * total no of days) + fine |
Assume that the super class Library has been defined. Using the concepts of Inheritance, specify the class Compute giving the details of constructor, void fine ( ) and void show ( ) functions.
The super class, main function and algorithm need NOT be written.
संक्षेप में उत्तर
Advertisements
उत्तर
class Compute extends Library
{
private int d;
private double f;
public Compute(String name, String author, double p, int d)
{
super(name, author,p);
this.d=d;
f=0.0;
}
public void fine()
{
int d1=d-7;
if(d1>=1 && d1<=5)
f=d1*2;
else if(d1>=6 && d1<=10)
f=d1*3;
else
f=d1*5;
}
public void show()
{
super.show();
System.out.println("Fine=" +f);
System.out.println("Total amount="+ ((0.02*p*d)+f));
}
}shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
