calculations

Java code to calculate Electricity bill | Java example

Java code to calculate Electricity bill | Java example

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

Program to calculate Electricity bill

Calculate Electricity bill using if with && operator

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

 

Calculate Electricity bill using if else if with && operator

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

 

Calculate Electricity bill using if else if

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

 

 

Calculate Electricity bill using if else if with user input

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

 

Calculate Electricity bill using Java method

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

 

Calculate Electricity bill using java object

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

 

Calculate Electricity bill using Inheritance

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

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

For loop in Java language

while loop in Java language

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

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.