Table of Contents
In this article, we will discuss the concept of Java code to calculate Electricity bill
In this program, we are going to learn how to calculate electricity bill using different methods in Java language.
This is done using if else if , method , Java object and inheritance in Java language
In this program, we will calculate electricity bill using if else if with && operator
Program 1
//Sample electicity bill calculation program import java.util.*; class Electricity_Bill_if { public static void main (String args[]){ long units;//declare variable unit double cost=0.0; /*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 if(units<=100)cost=units*5; if(units>100 && units<=200)cost=(100*5)+units-100*7; if(units>200 && units<=300)cost=(100*5)+(100*7)+(units-200)*10; if(units>300) cost=(100*5)+(100*7)+(100*10)+(units-300)*15; System.out.print("Total cost is: "+cost); } }
When the above code is executed, it produces the following result
Enter the number of units:350 Total cost is: 2950.00
In this program, we will calculate electricity bill using if else if with && operator in java language
Program 2
public class electicity_bill{ public static void main(String args[]){ int unit=350; /*1 -100 - 5/= 101 - 200 - 7/= 201 - 300 - 10/= above 301 =12/=*/ if(unit>=1 && unit <=100){ System.out.println(unit * 5); } else if(unit>=101 && unit <=200){ System.out.println(100 * 5 + (unit-100) *7); } else if(unit>=201 && unit <=300){ System.out.println(100 * 5 + 100 *7+(unit -200) * 10); } else if(unit>=301 ){ System.out.println(100 * 5 + 100 *7+100 * 10+(unit-300)*12); } else{ System.out.println("No unit"); } } }
When the above code is executed, it produces the following result
2800
In this program, we will calculate electricity bill using if else if in Java language
Program 3
//Sample electicity bill calculation program import java.util.*; class Electisity_Bill_Scan{ public static void main (String args[]){ long unit;//declare variable unit double bill_Amount=0;//declare and tnitialize bill amount /*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: "); unit=scan.nextLong();//taking input from user for usage unit if(unit<=100){ System.out.print("Bill amount is "+(unit*5)); } else if(unit<=200){ System.out.print("Bill amount is "+((100*5)+(unit-100)*7)); } else if(unit<=300){ System.out.print("Bill amount is "+((100*5)+(100*7)+(unit-200)*10)); } else if(unit>300){ System.out.print("Bill amount is "+((100*5)+(100*7)+(100*10)+(unit-300)*15)); } else{ System.out.print("No value"); } } }
When the above code is executed, it produces the following result
Enter number of unit: 400 Bill amount is 3700
In this program, we will calculate electricity bill using if else if with user input
Program 4
//Sample electicity bill calculation program import java.util.*; class Electisity_Bill1{ public static void main (String args[]){ int units =0; double amount=0.0; Scanner scan=new Scanner(System.in); System.out.print("Enter number of unit you consumed: "); units=scan.nextInt();//taking input from user for usage unit /*1 - 100 units - 5/= 101-200 units - 7/= 201-300 units - 10/= above 300 - 15/=*/ if(0<units && units<=100){ amount=(units*5); } else if(units>100 && units<=200){ amount=((100*5)+(units-100)*7); } else if (units>200 && units<=300){ amount=((100*5)+(100*7)+(units-200)*10); } else if(units>300){ amount=((100*5)+(100*7)+(100*10)+(units-300)*15); } else{ amount=0; } System.out.print("Total amount is: "+amount); } }
When the above code is executed, it produces the following result
Enter number of unit you consumed: 325 Total amount is: 2575.00
In this program, we will calculate electricity bill using Java method
Program 5
//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 calland assign the value //to total variable System.out.print("Total amout of bill: "+total); } static double calc_Bill(double units){//user defined method //method definition double bill_Amount=0;//declare and tnitialize 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 executed, it produces the following result
Enter number of unit : 400 Total amount of Bill: 3700.00
In this program, we will calculate electricity bill using Java object
Program 6
class electricity_bill2{ int consumerId; String name; int units; void setData(int id, String name, int units) { consumerId=id; this.name=name; 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; 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+(3300-200)*5+(units-350)*6; else System.out.println("No charges"); System.out.println("bill to pay"+billAmount); return billAmount; } } class Calc_Bill1{ public static void main(String args[]){ electricity_bill2 obj=new electricity_bill2(); obj.setData(101,"Jhon",351); double billPay=obj.balanceCalc(); obj.display(); System.out.println("bill to pay"+billPay); } }
When the above code is executed, it produces the following result
In this program, we will calculate electricity bill using Java inheritance
Program 7
import java.util.*; class ElectricityBillInheritance { double billAmount; void balanceCalc(long units){ /*1 - 100 unit - 1.5/= 101-200 unit - 3/= 201-300 unit - 5/= above 350 - 6/=*/ 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)+100*3+(units-200)*5; else if(units>350) billAmount=(100*1.5)+100*3+100*5+(units-350)*6; else System.out.println("No charges"); } } class EleBillPay extends ElectricityBillInheritance{ public static void main (String args[]){ long units;//declare variable unit //Using scanner class Scanner scan=new Scanner(System.in); System.out.print("Enter number of units: "); units=scan.nextLong();//taking input from user for usage unit EleBillPay b=new EleBillPay(); b.balanceCalc(units); System.out.print("Bill payment "+b.billAmount); } }
When the above code is executed, it produces the following result
Enter the number of units: 400 Bill Payment 1250.0
Suggested for you
if statements in Java language
Data type and variable in Java
Do-while loop in Java language
Similar post
Python code to calculate Electricity bill
C++ code to calculate Electricity bill
C code to calculate Electricity bill
Java code to calculate Electricity bill
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.