Write the program described below.
| Number of excess days | Fine per day (Rs.) |
|---|---|
| 1 to 5 | 2.00 |
| 6 to 10 | 3.00 |
| Above 10 | 5.00 |
Show answer
Answer
Official answer keyclass Compute extends Library
{
int d; // number of days taken in returning the book
double f; // fine
Compute(String name, String author, double p, int d)
{
super(name, author, p);
this.d = d;
f = 0.0;
}
void fine()
{
int excess = d - 7;
if (excess <= 0)
f = 0.0;
else if (excess <= 5)
f = excess * 2.0;
else if (excess <= 10)
f = excess * 3.0;
else
f = excess * 5.0;
}
void show()
{
super.show();
System.out.println("Number of days : " + d);
System.out.println("Fine : " + f);
System.out.println("Total amount : " + ((0.02 * p * d) + f));
}
}Explanation: Compute extends Library, so its constructor calls super(name, author, p) to initialise the inherited data members, then sets d. fine() computes the excess days beyond the free 7-day period and applies the given tiered rate (Rs.2/day for 1-5 excess days, Rs.3/day for 6-10, Rs.5/day beyond 10; 0 if not overdue). show() calls super.show() to print the book details, then prints the days, fine and total amount, calculated as (2% of price x total days) + fine. Tested (with a stub Library class) for price=500.0, d=12: excess=5, so fine=10.0 and total=(0.02x500x12)+10=130.0, matching manual calculation (verified by running the code).