KnowledgeBoat Logo

Computer Applications

Define a class called BookFair with the following description:

Data MembersPurpose
String Bnamestores the name of the book
double pricestores the price of the book
Member MethodsPurpose
BookFair( )Constructor to initialize data members
void input( )To input and store the name and price of the book
void calculate( )To calculate the price after discount. Discount is calculated as per the table given below
void display( )To display the name and price of the book after discount
PriceDiscount
Less than or equal to ₹10002% of price
More than ₹1000 and less than or equal to ₹300010% of price
More than ₹300015% of price

Write a main method to create an object of the class and call the above member methods.

Java

Java Constructors

ICSE 2016

133 Likes

Answer

import java.util.Scanner;

public class BookFair
{
    private String bname;
    private double price;
    
    public BookFair() {
        bname = "";
        price = 0.0;
    }
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter name of the book: ");
        bname = in.nextLine();
        System.out.print("Enter price of the book: ");
        price = in.nextDouble();
    }
    
    public void calculate() {
        double disc;
        if (price <= 1000)
            disc = price * 0.02;
        else if (price <= 3000)
            disc = price * 0.1;
        else
            disc = price * 0.15;
            
        price -= disc;
    }
    
    public void display() {
        System.out.println("Book Name: " + bname);
        System.out.println("Price after discount: " + price);
    }
    
    public static void main(String args[]) {
        BookFair obj = new BookFair();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class called BookFair with the following description: Write a main method to create an object of the class and call the above member methods.

Answered By

58 Likes


Related Questions