C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Class & Object - Library Program - Java Programs

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 auhor: stores the name of the author.


Member functions:
(i) void input(): to accept 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 functions.

import java.util.Scanner; class Library{ int accNum; String title; String author; public void input(){ Scanner in = new Scanner(System.in); System.out.print("Accession number: "); accNum = Integer.parseInt(in.nextLine()); System.out.print("Title: "); title = in.nextLine(); System.out.print("Author: "); author = in.nextLine(); } public void compute(){ Scanner in = new Scanner(System.in); System.out.print("Number of days late: "); int d = Integer.parseInt(in.nextLine()); double fine = d * 2; System.out.println("Fine: Rs. " + fine); } public void display(){ System.out.println("Accession Number Title\tAuthor"); System.out.println(accNum + "\t" + title + "\t" + author); } public static void main(String args[]){ Library obj = new Library(); obj.input(); obj.compute(); obj.display(); } }

Output

 
 OUTPUT : 
Accession number: 1001
Title: Programming in Java
Author: Daniel Y Ling
Number of days late: 5
Fine: Rs. 10.0
Accession Number                  Title                    Author
    1001                     Programming in Java    Daniel Y Laing