Problem Statement - Class definition – Electric Bill
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.
Solution
TC++ #7253
Run Output
Enter name: Sanjay
Enter units: 400
Name of the customer: Sanjay
Number of units consumed: 400
Bill amount: 1332.5
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.nextLine();
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