icse-promo

Question

 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:
(i) void input() – To input and store the accession number, title and author.

(ii)void compute() – To accept the number of days late, calculate and display the fine charged at the rate of Rs.2 per day.

(iii) 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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					
import java.util.Scanner;

public class Library
{
    int acc_num;
    String title;
    String author;
    Scanner sc=new Scanner(System.in);
    public Library()
    {
        acc_num=0;
        title="";
        author="";
    }

    public void input()
    {
        System.out.print("Enter accession number: "); 
        acc_num =sc.nextInt();
        sc.nextLine();
        System.out.print("Enter title: "); 
        title =  sc.nextLine();
        System.out.print("Enter author: ");
        author = sc.nextLine(); 

    } 

    public void compute()
    {
        System.out.print("Enter number of days late: "); 
        int late = sc.nextInt();
        int fine = 2 * late;
        System.out.println("Fine is Rs " + fine); 
    } 

    public void display() 
    {
        System.out.println("Accession Number\tTitle\tAuthor"); 
        System.out.println(acc_num + "\t" + title + "\t" + author);
    }

    public static void main(String[] args) 
    {
        Library ob1 = new Library();
        ob1.input(); 
        ob1.compute();
        ob1.display();
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *