Computer Applications

Define a class Library having the following description:

Class name : Library

Data MembersPurpose
String nameto store name of the book
int priceto store the printed price of the book
int dayto store the number of days for which fine is to be paid
double fineto store the fine to be paid
Member functionsPurpose
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.

DaysFine
First seven days25 paise per day
Eight to fifteen days40 paise per day
Sixteen to thirty days60 paise per day
More than thirty days80 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