Computer Applications
Define a class Library having the following description:
Class name : Library
Data Members | Purpose |
---|---|
String name | to store name of the book |
int price | to store the printed price of the book |
int day | to store the number of days for which fine is to be paid |
double fine | to store the fine to be paid |
Member functions | Purpose |
---|---|
void input() | To accept the name of the book and printed price of the book |
void cal() | Calculates the fine to be paid |
void display() | Displays the name of the book and fine to be paid |
Write a program to compute the fine according to the given conditions and display the fine to be paid.
Days | Fine |
---|---|
First seven days | 25 paise per day |
Eight to fifteen days | 40 paise per day |
Sixteen to thirty days | 60 paise per day |
More than thirty days | 80 paise per day |
Java
Java Classes
53 Likes
Answer
import java.util.Scanner;
public class Library
{
private String name;
private int price;
private int day;
private double fine;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
name = in.nextLine();
System.out.print("Enter printed price of the book: ");
price = in.nextInt();
System.out.print("For how many days fine needs to be paid: ");
day = in.nextInt();
}
public void cal() {
if (day <= 7)
fine = day * 0.25;
else if (day <= 15)
fine = (7 * 0.25) + ((day - 7) * 0.4);
else if (day <= 30)
fine = (7 * 0.25) + (8 * 0.4) + ((day - 15) * 0.6);
else
fine = (7 * 0.25) + (8 * 0.4) + (15 * 0.6) + ((day - 30) * 0.8);
}
public void display() {
System.out.println("Name of the book: " + name);
System.out.println("Fine to be paid: " + fine);
}
public static void main(String args[]) {
Library obj = new Library();
obj.input();
obj.cal();
obj.display();
}
}
Variable Description Table
Program Explanation
Output
Answered By
18 Likes
Related Questions
Hero Honda has increased the cost of its vehicles as per the type of the engine using the following criteria:
Type of Engine Rate of increment 2 stroke 10% of the cost 4 stroke 12% of the cost Write a program by using a class to find the new cost as per the given specifications:
Class name: Honda
Data Members Purpose int type To accept type of engine 2 stroke or 4 stroke int cost To accept previous cost Member Methods Purpose void gettype() To accept the type of engine and previous cost void find() To find the new cost as per the criteria given above void printcost() To print the type and new cost of the vehicle Define a class Telephone having the following description:
Class name : Telephone
Data Members Purpose int prv, pre to store the previous and present meter readings int call to store the calls made (i.e. pre - prv) String name to store name of the consumer double amt to store the amount double total to store the total amount to be paid Member functions Purpose void input() Stores the previous reading, present reading and name of the consumer void cal() Calculates the amount and total amount to be paid void display() Displays the name of the consumer, calls made, amount and total amount to be paid Write a program to compute the monthly bill to be paid according to the given conditions and display the output as per the given format.
Calls made Rate Up to 100 calls No charge For the next 100 calls 90 paise per call For the next 200 calls 80 paise per call More than 400 calls 70 paise per call However, every consumer has to pay ₹180 per month as monthly rent for availing the service.
Output:
Name of the customer Calls made Amount to be paid .................... .......... ................. .................... .......... .................
Define a class Interest having the following description:
Class name : Interest
Data Members Purpose int p to store principal (sum) int r to store rate int t to store time double interest to store the interest to be paid double amt to store the amount to be paid Member functions Purpose void input() Stores the principal, rate, time void cal() Calculates the interest and amount to be paid void display() Displays the principal, interest and amount to be paid Write a program to compute the interest according to the given conditions and display the output.
Time Rate of interest For 1 year 6.5% For 2 years 7.5% For 3 years 8.5% For 4 years or more 9.5% (Note: Time to be taken only in whole years)
Bank charges interest for the vehicle loan as given below:
Number of years Rate of interest Up to 5 years 15% More than 5 and up to 10 years 12% Above 10 years 10% Write a program to model a class with the specifications given below:
Class name: Loan
Data Members Purpose int time Time for which loan is sanctioned double principal Amount sanctioned double rate Rate of interest double interest To store the interest double amt Amount to pay after given time Member Methods Purpose void getdata() To accept principal and time void calculate() To find interest and amount.
Interest = (Principal*Rate*Time)/100
Amount = Principal + Interestvoid display() To display interest and amount