Computer Applications
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:
- 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 the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member methods.
Java
Java Classes
ICSE 2012
94 Likes
Answer
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();
}
}Output

Answered By
38 Likes
Related Questions
The correct statement to create an object named mango of class fruit:
- Fruit Mango= new fruit();
- fruit mango = new fruit();
- Mango fruit=new Mango();
- fruit mango= new mango();
Define a class with the following specifications:
Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amountMember methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:Time in (Years) Rate % Upto 1⁄2 9 > 1⁄2 to 1 year 10 > 1 to 3 years 11 > 3 years 12 void display () — display the details in the given format.
Principal Time Rate Amount XXX XXX XXX XXXWrite the main method to create an object and call the above methods.