C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Class & Object - Book Fair Program - Java Programs

Define a class named BookFair with the following description:
Instance variables/data members:
String bName: stores the name of the book.
double price: stores the price of the book.
Member methods:
BookFair(): default constructor to initialize data members.
void input(): to input and store the name and the price of the book.
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

void display(): to display the name and price of the book after discount.

import java.util.Scanner; class BookFair{ String bName; double price; public BookFair(){ bName = ""; price = 0.0; } public void input(){ Scanner in = new Scanner(System.in); System.out.print("Book name: "); bName = in.nextLine(); System.out.print("Price: "); price = Double.parseDouble(in.nextLine()); } public void calculate(){ if(price <= 1000) price -= 2.0 / 100 * price; else if(price <= 3000) price -= 10.0 / 100 * price; else price -= 15.0 / 100 * price; } public void display(){ System.out.println("Book name: " + bName); calculate(); System.out.println("Price after discount: " + price); } public static void main(String[] args){ BookFair obj = new BookFair(); obj.input(); obj.display(); } }

Output

 
 OUTPUT 1: 
Book name: The Wings of Eagles
Price: 1500
Book name: The Wings of Eagles
Price after discount: 1350.0

 OUTPUT 2: 
Book name: Two little Soldiers
Price: 2100
Book name: Two little Soldiers
Price after discount: 1890.0