Java program to calculate Electricity bill. If you wondering on how to calculate Electricity bill by writing the Java program there you go. Here we share the list of code with sample outputs, in five different ways using static values, using function or method, using command line arguments, do while, for loop, user define method along with sample outputs for each program.
Q. how to calculate electricity bill?
Consider the following example:
For example, a consumer consumes 500 watts per hour daily for one month. Calculate the total energy bill of that consumer if per unit rate is 7? ( In $, £, €, INR, DHR, Riyal etc) [Take 1 month = 30 Days].
Solution :
s we know that 1 Unit = 1kWh
So total kWh = 500 watts x 24 hours x 30 days
= 360000
So, we want to convert into units:
Where 1 unit = 1kWh
Total consumed units are as 360000/1000 = 360
And, cost per unit is = 7, the total cost of the electricity bill is 360 x 7 = 2520( In $, £, €, INR, Rs, DHR, Riyal etc).
That’s it. Let’s get into programming.
1. Java program to calculate electricity using the static method with outputs
There you go by using the standard values along with the sample outputs. If you have any doubts related to the following program do leave a comment here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.*; class ComputeElectricityBill { public static void main(String args[]) { int units=280; double billpay=0; if(units<100) { billpay=units*1.20; } else if(units<300) { billpay=100*1.20+(units-100)*2; } else if(units>300) { billpay=100*1.20+200*2+(units-300)*3; } System.out.println("Bill to pay : " + billpay); } } |
1 |
Bill to pay : 480.0 |
2. Taking inputs through scanner class
Java code for obtaining Electricity Bill taking inputs through scanner class, with outputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.*; class ComputeElectricityBill { public static void main(String args[]) { long units; Scanner sc=new Scanner(System.in); System.out.println("enter number of units"); units=sc.nextLong(); double billpay=0; if(units<100) billpay=units*1.20; else if(units<300) billpay=100*1.20+(units-100)*2; else if(units>300) billpay=100*1.20+200 *2+(units-300)*3; System.out.println("Bill to pay : " + billpay); } } |
1 2 3 4 5 6 7 8 9 |
C:\Users\goutham\Desktop\E>javac ComputeElectricityBill.java C:\Users\goutham\Desktop\E>java ComputeElectricityBill enter number of units 550 Bill to pay : 1270.0 |
3. Taking Inputs through command line arguments
Taking inputs through command line arguments with an output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; class ComputeElectricityBill { public static void main(String args[]) { long units; units=Long.parseLong(args[0]); double billpay=0; if(units<100) billpay=units*1.20; else if(units<=300) billpay=100*1.20+(units-100)*2; else if(units>300) billpay=100*1.20+200*2+(units-300)*3; System.out.println("Bill to pay : " + billpay); } } |
output:
1 2 3 4 5 |
C:\Users\goutham\Desktop\E>javac ComputeElectricityBill.java C:\Users\goutham\Desktop\E>java ComputeElectricityBill 500 Bill to pay : 1120.0 |
4. Using Inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.*; class CalculateBill { double billpay; void Bill(long units) { if(units<100) billpay=units*1.20; else if(units<=300) billpay=100*1.20+(units-100)*2; else if(units>300) billpay=100*1.20+200 *2+(units-300)*3; } } class ComputeElectricityBill extends CalculateBill { public static void main(String args[]) { long units; Scanner sc=new Scanner(System.in); System.out.println("enter number of units"); units=sc.nextLong(); ComputeElectricityBill b=new ComputeElectricityBill(); b.Bill(units); System.out.println("Bill to pay : " + b.billpay); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
enter number of units 500 Bill to pay : 1120.0 enter number of units 250 Bill to pay : 420.0 enter number of units 1000 Bill to pay : 2620.0 |
4. Electricity Bill code by creating a Separate class
Through creating a separate class( ) and taking inputs through scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.*; class CalculateElectricityBill { double billpay; CalculateElectricityBill(long units) { if(units<100) billpay=units*1.20; else if(units<=300) billpay=100*1.20+(units-100)*2; else if(units>300) billpay=100*1.20+200 *2+(units-300)*3; } } class ComputeElectricityBill { public static void main(String args[]) { long units; Scanner sc=new Scanner(System.in); System.out.println("enter number of units"); units=sc.nextLong(); CalculateElectricityBill b=new CalculateElectricityBill(units); System.out.println("Bill to pay : " + b.billpay); } } |
output:
1 2 3 |
enter number of units 1000 Bill to pay : 2620.0 |
5. User-defined method ( Function )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
java.util.Scanner; class ComputeElectricityBill { public static void main(String args[]) { double units,b; Scanner sc=new Scanner(System.in); System.out.println("enter number of units"); units=sc.nextDouble(); b= calculateElectricityBill(units); System.out.println("Bill to pay : " + b); } static double calculateElectricityBill(double units) { double billpay=0; if(units<100) billpay=units*1.20; else if(units<=300) billpay=100*1.20+(units-100)*2; else if(units>300) billpay=100*1.20+200 *2+(units-300)*3; return billpay; } } |
1 2 3 |
enter number of units 600 Bill to pay : 1420.0 |
More programs for you :