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
92 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
37 Likes
Related Questions
What is meant by private visibility of a method?
Fill in the blanks:
Primitive data types are also called as _________ data types.
Define a class 'COMMISSION' as described below:
String name — stores the employee's name
int emp_no — stores the employee number
int sal — store monthly sales valueMember methods
void input() — to input and store employee's name, number and monthly sales value.
void compute( ) — to calculate commission on sale as per the tariff given below:Sale Commission Up to ₹50,000 5% on sales value More than ₹50,000 and up to ₹80,000 8% on sales value More than ₹80,000 and up to ₹1,00,000 10% on sales value More than ₹1,00,000 12% on sales value void display( ) — to display the details of employee's name with commission.
Write a main method to create object of a class and call the above member methods.
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();