Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Define a class named BookFair with the following description: [15]
Instance variables/Data members:
String Bname – stores the name of the book.
double price – stores the price of the book.
Member Methods:
(i) BookFair() – Default constructor to initialize data members.
(ii) void Input() – To input and store the name and the price of the book.
(iii) void calculate() – To calculate the price after discount. Discount is calculated based on the following criteria.
PRICE DISCOUNT
Less than or equal to Rs 1000 2% of price
More than Rs 1000 and less than or equal to Rs 3000 10% of price
More than Rs 3000 15% of price
(iv) void display() – To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.
Answer:
import java.util.Scanner;
public class BookFair {
private String Bname;
private double price;
public BookFair() {
Bname = null;
price = 0.0;
}
public void Input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter book name: ");
Bname = scanner.nextLine();
System.out.print("Enter price: ");
price = scanner.nextDouble();
}
public void calculate() {
double discountPercentage = 0;
if (price <= 1000) { 23 discountPercentage = 2; 24 } else if (price > 1000 && price <= 3000) { 25 discountPercentage = 10; 26 } else if (price > 3000) {
discountPercentage = 15;
}
price = price - (price * discountPercentage / 100);
}
public void display() {
System.out.println("Name: " + Bname);
System.out.println("Price after discount: " + price);
}
public static void main(String[] args) {
BookFair bookFair = new BookFair();
bookFair.Input();
bookFair.calculate();
bookFair.display();
}
}
Sample output
Enter book name: How to Java
Enter price: 2500
Name: How to Java
Price after discount: 2250.0
C. Sir| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016