Basics

Define a class called Library, Class and Objects base program

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 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 ar the rate of Rs. 2 per day..
  • void display( ) – to display the details in following format:

Accession Number                     Title                             Author

————————                     ———————-         ————————

Write a main( ) method where user will create an object of the class and call all the member functions.

import java.io.*;
import java.util.*;

class Library
{
	int acc_num;
	String title;
	String author;

	void input( )
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter Accession Number =");
		acc_num = sc.nextInt();
		System.out.println("Enter Title =");
		title = sc.nextLine();
		System.out.println("Enter Author =");
		author = sc.nextLine();
	}

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

	void compute( )
	{
		Scanner sc = new Scanner(System.in);
		int days, fine = 0;
		System.out.println("Enter days =");
		days = sc.nextInt( );
		fine = days*2;
		System.out.println("Total Fine ="+fine);
	}

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

Click to comment

Leave a Reply

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

To Top