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 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:
Number of units Rate per unit
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
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: ………………………
Write a main method to create an object of the class and call the above member methods.
Answer:
import java.util.Scanner;
public class ElectricBill {
private String n;
private int units;
private double bill;
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
n = scanner.next();
System.out.print("Enter units: ");
units = scanner.nextInt();
}
public void calculate() {
if (units <= 100) { bill = units * 2; } else if (units > 100 && units <= 300) {
bill = 100 * 2 + (units - 100) * 3;
} else {
bill = 100 * 2 + 200 * 3 + (units - 300) * 5;
double surcharge = bill * 2.5 / 100;
bill = bill + surcharge;
}
}
public void print() {
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(String[] args) {
ElectricBill electricBill = new ElectricBill();
electricBill.accept();
electricBill.calculate();
electricBill.print();
}
}
Sample output
Enter name: Sai
Enter units: 50
Name of the customer: Sai
Number of units consumed: 50
Bill amount: 100.0
C. Sir| Created: 15-Apr-2019 | Updated: 11-Jul-2026|ICSE2017