| 
       Q4. Define a class called Library with the following description: 
Instance variables/data members: 
- 
int acc_num – 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: 
- 
void input() – to input and store the accession number, title and author 
 
- 
void compute() – to accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day 
 
- 
void display() – to display the details in following format 
 
 
Accession Number     Title               Author
----------------     -----               ------ 
Write a main method to create an object of the class and call the above member methods. 
[15 marks] 
A4. 
import java.util.*;
class Library
{
    // instance variables, data members
    int acc_num;
    String title, author;
    // zero argument constructor
    public Library()
    {
        acc_num = 0;
        title = "";
        author = "";
    }
    // input
    public void input()
    {
        Scanner sc = new Scanner( System.in );
        String temp = "";
        System.out.print("Enter the accession number: ");
        acc_num = sc.nextInt();
        temp = sc.nextLine(); // for the trailing newline
        System.out.print("Enter the title: ");
        title = sc.nextLine();
        System.out.print("Enter the author: ");
        author = sc.nextLine();
    }
    // compute and display the fine charged
    public void compute(int nDays)
    {
        int fine = nDays * 2;
        System.out.println("Fine charged: " + fine);
    }
    // display the info
    public void display()
    {
        printInt(acc_num, 16+1); // print acc_num in width of 16
        printStr(title, 30+1);  // print title in width of 30
        printStr(author, 20); // print author in width of 20
        System.out.println();
    }
    // static method to print the header once only
    public static void displayHeader()
    {
        printStr("Accession Number", 16+1);
        printStr("Title", 30+1);
        printStr("Author", 20);
        System.out.println();
        printStr("----------------", 16+1);
        printStr("------------------------------", 30+1);
        printStr("--------------------", 20);
        System.out.println();
    }
    // main method
    public static void main(String[] args)
    {
        Library book1 = new Library();
        Library book2 = new Library();
        book1.input();
        book2.input();
        book1.compute(5);
        book2.compute(7);
        Library.displayHeader();
        book1.display();
        book2.display();
    }
    // print argument1 in a field of width argument2
    public static void printInt(int n, int width)
    {
        String s = "" + n;
        printStr(s, width); // call the String method
    }
    // print argument1 in a field of width argument2
    public static void printStr(String s, int width)
    {
        System.out.print(s);
        // print spaces to fill up the field width
        for(int i=s.length()+1; i<=width; i++)
          System.out.print(" ");
    }
}
    harshraj  
 |