English

Define a class Library having the following description: Class name : Library Data members String name: to store name of the book int price: to store the printed price of the book - Computer Applications

Advertisements
Advertisements

Question

Define a class Library having the following description:

Class name : Library
Data members
String name: to store name of the book
int price: to store the printed price of the book
int day : to store the number of days for which fine is to be paid
double fine: to store the fine to be paid
Member methods:
void input(): To accept the name of the book and printed price of the book
void cal(): Calculates the fine to be paid
void display(): Displays the name of the book and fine to be paid

Write a program to compute the fine according to the given conditions and display the fine to be paid.

Days Fine
First seven days 25 paise per day
Eight to fifteen days 40 paise per day
Sixteen to thirty days 60 paise per day 
More than thirty days 80 paise per day 
Code Writing

Solution

// EXECUTE input() method 
import java.util.*; 
class Library 
{String name; 
int price, day; 
double fine;

void input()
{Scanner ob = new Scanner(System.in);
System.out.println("**Input - Book Details**");
System.out.print("Name:")
name = ob.nextLine();
System.out.print("Printed Price:");
price = ob.nextInt();
System.out.print("No. of days for which fine is to be paid:");
day = ob.nextInt();
cal(); // method call
}

void cal()
{if (day <= 7)
fine = 0.25 * day;
else if (day >= 8 && day <= 15)
fine = (7 * 0.25) + 0.40 * (day - 7);
else if (day >= 16 && day <= 30)
fine = (7 * 0.25) + (8 * 0.40) + 0.60 * (day - 15);
else
fine = (7 * 0.25) + (8 * 0.40) + (15 * 0.60) + 0.80 * (day - 30);
display(); // method call
}

void display()
{System.out.println("\n\nName: " + name);
System.out.println("Fine in Rs.): " + (float) fine);
}}
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Class as the Basis of all Computation (Objects and Classes) - EXERCISES [Page 386]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 6 Class as the Basis of all Computation (Objects and Classes)
EXERCISES | Q VI. 7. | Page 386
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×