Calculations

Calculate electricity bill using OOP in Java

Calculate electricity bill using OOP in Java

In this tutorial, we will discuss the concept of Calculate electricity bill using OOP in Java

In this topic, we are going to learn how to calculate the usage electricity bill using OPP in Java language

 

We can calculate our monthly electric power usage using different methods

In this tutorial, we will discuss two different methods for calculating electricity bill using OOP in Java.

Before we write a program to find the electricity bill, we must understand the charges and rates.

/*1 - 100 unit - 5/= 
101-200 unit - 7/= 
201-300 unit - 10/= 
above 300 - 15/=*///for example

 

 

Calculate electricity bill using OOP

This program has the following explanations of calculating electricity bill

  • Declare total unit consumed by the customer using the variable unit.
  • if the unit consumed less or equal to 100 units, calculates the total amount of usage =unit*5
  • if unit consumed between 100 to 200 units, calculates the total amount of usage =(100*5)+(unit-100)*7)
  • if  unit consumed between 200 to 300 units ,calculates total amount of usage =(100*5)+(100*7)+(unit-200)*10
  • if unit consumed above 300 units ,calculates total mount of usage =(100*5)+(100*7)+(100*10)+(unit-300)*15
  • No additional charge

Electricity bill Calculation using method

Program 1

class Calc_Bill_Method{//class declaration
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 to input unit of usage
double total=calc_Bill(units);//calling the method and assign the result
                             //to total variable

System.out.print("Total amout of bill: "+total);
}
static double calc_Bill(double units){//user defined method 
//method definition
double bill_Amt=0;//declare and tnitialize bill amount
if(units<=100){
    bill_Amt=(units*5);
}
else if(units<=200){
    bill_Amt=(100*5)+(units-100)*7;
}
else if(units<=300){
    bill_Amt=(100*5)+(100*7)+(units-200)*10;
}
else if(units>300){
    bill_Amt=(100*5)+(100*7)+(100*10)+(units-300)*15;
}
else{
    System.out.print("No value");
    
}
return bill_Amt;
}

}

When the above code is executed, it produces the following result

Enter number of unit: 270
Total amount of bill: 1900.0

 

This program has the following explanations of calculating electricity bill

  • Declare total unit consumed by the customer using the variable units.
  • if the units consumed less or equal to 100 units, calculates the total amount of usage =units*1.5
  • if unit consumed between 100 to 200 units, calculates the total amount of usage =(100*1.5)+(units-100)*3)
  • if  unit consumed between 200 to 300 units ,calculates total amount of usage =(100*1.5)+(100*3)+(units-200)*6
  • if unit consumed above 300 units ,calculates total mount of usage =(100*1.5)+(100*3)+(100*6)+(units-300)*7
  • No additional charge

Electricity bill Calculation using classes

Program 2

class electricity_bill2{
int consumerId;
String name;
int units;
//declare global variabls
void setData(int id, String name, int units)
{//declare method for set data
consumerId=id;
this.name=name;
this.units=units;
//Using "this" keyword for access global variable inside the method as same name
}
/*1 - 100 unit - 1.5/= 
101-200 unit - 3/= 
201-300 unit - 6/= 
above 300 - 7/=*/
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)*6;

else if(units>350)
billAmount=(100*1.5)+200*3+(300-200)*6+(units-350)*7;

else
System.out.println("No charges");

return billAmount;
}
}

class Calc_Bill_Ele{//define class
    public static void main(String args[]){//define main method
        electricity_bill2 obj=new electricity_bill2();//create object
        obj.setData(105,"Jhon",362);//call method using object with argument
        double billPay=obj.balanceCalc();
        obj.display();//call method using object
        System.out.println("bill to pay"+billPay);
        //display amount
    }
    
}

When the above code is executed, it produces the following result

Consumer id:101
Consumer name:Jhon
Consumed unit:362
Bill to pay:1434.0

 

Suggested for you

Operator in Java language

If statements in Java

Data type and variable in Java language

 

Similar post

Java code to find electricity bill

C code to find electricity bill

C++  code to calculate electricity bill

Python code to calculate electricity bill

Use of C program to subtraction of two numbers using recursion
Program to Electricity bill calculation using OOP in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago