Home / Programs / Define a class called Library with the following description:
Programming Example

Define a class called Library with the following description:

👁 93 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Define a class called Library with the following description:

Instance Variables/Data Members:
int accNum — stores the accession number of the book.
String title — stores the title of the book.
String author — stores the name of the author.

Member methods:

  1. void input() — To input and store the accession number, title and author.
  2. void compute() — To accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day.
  3. void display() — To display the details in the following format:
Accession Number    Title   Author

Write a main method to create an object of the class and call the above member methods.

Program Code

import java.util.Scanner;

public class Library
{
    private int accNum;
    private String title;
    private String author;
    
    void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter book title: ");
        title = in.nextLine();
        System.out.print("Enter author: ");
        author = in.nextLine();
        System.out.print("Enter accession number: ");
        accNum = in.nextInt();
    }
    
    void compute() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of days late: ");
        int days = in.nextInt();
        int fine = days * 2;
        System.out.println("Fine = Rs." + fine);
    }
    
    void display() {
        System.out.println("Accession Number\tTitle\tAuthor");
        System.out.println(accNum + "\t\t" + title + "\t" + author);
    }
    
    public static void main(String args[]) {
        Library obj = new Library();
        obj.input();
        obj.display();
        obj.compute();
    }
}

Explanation



How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.