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 :
(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 % 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.
import java.util.*;
class BookFair
{
String Bname;
double price,amount,discount;
BookFair()
{
Bname="";
price=0.0d;
discount=0.0;
}
void input()
{
Scanner sc=new Scanner (System.in);
System.out.print("Enter the Book name: ");
Bname=sc.nextLine();
System.out.print("Enter the Price of the Book : ");
price=sc.nextDouble();
}
void calculate()
{
if(price<=1000)
discount=price*2/100;
else if(price<=3000)
discount=price*10/100;
else
discount=price*15/100;
}
void display()
{
System.out.println("The Name of the Book is "+Bname);
System.out.println("The Price of the Book is "+price);
System.out.println("The Discount Amount is "+discount);
System.out.println("The Actual Amount to be paid is "+(price-discount));
}
void main()
{
BookFair ob=new BookFair();
ob.input();
ob.calculate();
ob.display();
}
}
Define a class ElectricBill with the following specifications:
class : ElectricBill
Instance variables /data member :
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods:
void accept ( ) – to accept the name of the customer and number of units consumed
void calculate ( ) – to calculate the bill as per the following tariff :
· First 100 Units: ₹10/- unit
· Next 100 units: ₹15/- unit
· Next 100 units: ₹20/- unit
· above 300 units ₹25/- unit
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print() – To Print the details as follows:
Name of the Customer: _________________________
Number of Units Consumed:_________________________
Bill Amount: _________________________
import java.util.Scanner;
class ElectricityBill
{
String n;
int units;
double bill;
ElectricityBill()
{
n="";
units=0;
bill=0.0d;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Name of the Customer:");
n = sc.next ();
System.out.print("Enter the Number of Units Consumed");
units = sc.nextInt();
}
void calculate()
{
if(units<=100)
bill = units * 10;
else if(units<=200) //next 100 units
bill = 10 * 100 + (units - 100) * 15;
else if(units<=300) //next 100 units
bill = 10 * 100 + 100 * 15 + (units - 200) * 20;
else //above 300 units
{
bill = 10*100 + 100*15 + 100*20 + (units-300) * 25;
bill = bill + bill * 2.5 /100; //surcharge 2.5%
}
}
void display()
{
System.out.println("Name of the Customer : " + n);
System.out.println("Number of Units Consumed : " + units);
System.out.println("Bill Amount: " + bill);
}
public static void main()
{
ElectricityBill obj = new ElectricityBill();
obj.accept();
obj.calculate();
obj.display();
}
}