Class 10th ICSE

Q.4

4. Define a class 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:
(i) BookFair()-Default constructor to initialize data members.
(ii) void input()-to input and store the name and price of the book.
(iii) void calculate()-to calculate the price after discount. Discount is calculated
based on the following criteria:
Price Discount
Lessthan or equal to Rs.1000 2% of price
Morethan Rs.1000 and lessthan or
equal to Rs.3000
10% of price
Morethan Rs.3000 15% of price

Solution:-

import java.util.*;
class p4{
Scanner sc=new Scanner(System.in);
String bname;
double price;
double discount;
p4(){
discount=0.0;
bname=””;
price=0.0;
}
void input(){
System.out.println(“Enter name of the book”);
bname=sc.nextLine();
System.out.println(“Enter price of the book”);
price=sc.nextDouble();
}
void calculate(){
if(price<=1000)
discount=price*0.02;
else if(price>1000 && price<=3000)
discount=price*0.1;
else if(price>3000)
discount=price*0.15;
}
void display(){
System.out.println(“Name of the book=”+bname);
System.out.println(“Price of the book after discount=”+(price-discount));
}
public static void main(){
p4 obj=new p4();
obj.input();
obj.calculate();
obj.display();
}
}

Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable

Popular Videos

Play Video

UX for Teams

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer

Play Video

SEO & Instagram

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer