Bill calculation

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.

Electricity bill calculation using the Java method

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

This keyword in Java

Data type in Java

Python program to calculate electricity bill
Java code to calculate factorial of a number
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

3 weeks ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

4 weeks ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

1 month ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago