Calculate Electricity bill using Java method
- Home
- Bill calculation
- Calculate Electricity bill using Java method
- On
- By
- 0 Comment
- Categories: Bill calculation, Calculations
Calculate Electricity bill using Java method
Calculate Electricity bill using Java method
Using user-defined method program 1
In this tutorial, we will discuss the Calculate Electricity bill using the Java method
In this post, we will learn how to calculate electricity bill using if condition in the Java programming language
We can calculate monthly consumed electric power usage in many ways.

In this tutorial, we will explain two programs calculating electricity bill using the Java method
Program 1
//Sample electicity bill calculation program
import java.util.*;
class Electisity_Bill_Method{
public static void main (String args[]){
long units;//declare variable unit
/*1 - 100 unit - 5/=
101-200 unit - 7/=
201-300 unit - 10/=
above 300 - 15/=*/
//Using scanner class
Scanner scan=new Scanner(System.in);
System.out.print("Enter number of unit: ");
units=scan.nextLong();//taking input from user for usage unit
double total=calc_Bill(units); //method call
System.out.print("Total amout of bill: "+total);
}
//user defined method in Java
static double calc_Bill(double units){//defined a method
double bill_Amount=0; //declare and initialize bill amount
if(units<=100){
bill_Amount=(units*5);
}
else if(units<=200){
bill_Amount=(100*5)+(units-100)*7;
}
else if(units<=300){
bill_Amount=(100*5)+(100*7)+(units-200)*10;
}
else if(units>300){
bill_Amount=(100*5)+(100*7)+(100*10)+(units-300)*15;
}
else{
System.out.print("No value");
}
return bill_Amount;
}
}
When the above code is compiled and executed, it produces the following results
Enter number of units: 340 Total amount of bill: 2800.0
Using user-defined method program 2
the important points of this program
- The class electricity_bill2 contains fields consumerId, name, units.
- The class electricity_bill2 contains methods setData(), display(), balanceCalc(),
- An object created inside the class Calc_Bill for refer class electricity_bill2 using reference variable obj
- Reference variable obj is used to call the methods within the class electricity_bill2
Program 2
class electricity_bill2{
int consumerId;
String name;
int units;
void setData(int id, String name, int units)//set method
{
consumerId=id;
this.name=name;//this keyword used to refer global variable
this.units=units;
}
void display()
{
System.out.println("Consumer id: "+consumerId);
System.out.println("Consumer Name: "+name);
System.out.println("Consumed unit: "+units);
}
double balanceCalc(){
double billAmount=0.0;
if(units>0 && units<=100)
billAmount=units*1.5;
else if(units>100 && units<=200)
billAmount=100*1.5+(units-100)*3;
else if(units>200 && units<=300)
billAmount=(100*1.5)+200*3+(units-200)*5;
else if(units>350)
billAmount=(100*1.5)+200*3+(300-200)*5+(units-350)*6;
else
System.out.println("No charges");
System.out.println("bill to pay: "+billAmount);
return billAmount;
}
}
class Calc_Bill{
public static void main(String args[]){
electricity_bill2 obj=new electricity_bill2(); //creste object
obj.setData(101,"Jhon",351); //call the set metod
double billPay=obj.balanceCalc(); //call the balanceCalc method
obj.display(); //call the display method
System.out.println("bill to pay: "+billPay); //result display on the screen
}
}
When the above code is compiled and executed, it produces the following results
bill to pay: 1256.0 Consumer id: 101 Consumer Name: Jhon Consumed units: 351 bill to pay: 1256.0
Suggested for you
Java method
If statements in Java
Operator in Java
Data type in Java