You are on page 1of 3

Q : A library issues books on a 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
1 to 5 2.00
6 to 10 3.00
Above 10 days 5.00
Design a class Library and another class Compute to perform the task. The details of the 2 classes
are given below:
Class name : Library
Data members
name : name of the book
author : author of the book
p : price of the book in decimals
Member functions
Library(…) : parameterized constructor to assign values to data members.
void show() : display the book details
Class name : Compute
Data members
d : number of days taken in returning the book.
f : to store the fine.
Member functions
Compute(…) : parameterized constructor to assign values to data Members of both classes.
void fine() : calculate the fine for excess days.
void display() : displays the book details along with the number of days, fine and total amount
to be paid. Total amount is calculated as: (2% of price of book * total no. of
days) + fine
Specify the class Library giving details of the constructor all the member function of both the classes

Algorithm:-
1.Start
2.A base class library is created with given parameter of details.
3.All the parameters are initialized in the constructor.
4.Another class named compute is created which inherits its properties from the base class library.
5.The derived class gets it’s variables initialized along with the variables of the super class.
6.The keyword super is used to call the constructor of the super class and passs values to the
respective variables.
7.The calculation of the fine is done in void fine() function with the provided conditions.
8.Then the final amount is printed with all the information in void show().
9. Another class is created to create main for ease of access.
10. Object of class compute is created and the required functions are called.
11. Stop
Program:-
import java.util.*;
class library
{ String name; String author; double p;
library(String nn, String aa,double pp)
{ name=nn; author= aa; p=pp; }
void show()
{ System.out.println(" name of the book ="+ name);
System.out.println(" name of the author ="+ author);
System.out.println(" price of the book="+ p);
}
}
class compute extends library
{ int d; double f;
compute(String nn,String aa,double pp, int dd)
{ super(nn,aa,pp);
d=dd; f=0.0;}
void fine()
{ if(d<=7)
{ f=0.0;}
else
{ if((d-7)<=5)
{ f=2*(d-7);}
if((d-7)>5&&(d-7)<=10)
{f=(2*5)+(((d-7)-5)*3);}
if((d-7)>10)
{f=(2*5)+(3*5)+(((d-7)-10)*5);}
}
}
void show()
{ super.show();
System.out.println("No of days book was issued="+d);
System.out.println("Total fine on the issue="+f);
System.out.println("Total amt to be paid= "+((0.02*p*d)+f));
}
}
class calculation
{ public static void main(String args[])
{Scanner sc=new Scanner(System.in);
System.out.println("Enter book name");
String nr=sc.nextLine();
System.out.println("Enter author name");
String ar=sc.nextLine();
System.out.println("Enter price of the book");
double pr=sc.nextDouble();
System.out.println("Enter no of days book was issued for");
int dr=sc.nextInt();
compute ob=new compute(nr,ar,pr,dr);
ob.fine();
ob.show();
}
}

Output:-
Enter book name
The case book of Sherlock Holmes
Enter author name
Sir Arthur Conan Doyale
Enter price of the book
120
Enter no of days book was issued for
16
name of the book =The case book of Sherlock Holmes
name of the author =Sir Arthur Conan Doyale
price of the book=120.0
No of days book was issued=16
Total fine on the issue=22.0
Total amt to be paid= 60.4

Variable Description:-
Variable Type Description
name String To store name of the book
author String To store author of the book
p Double To store price of the book in decimals
f Double To store the calculated fine
d Int To store number of days taken in returning the book.
sc Object variable To accept a value from console
nr String To store name of the book
pr Double To store price of the book in decimals
ar String To store author of the book
dr Int To store number of days taken in returning the book.

You might also like