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
the important points of this program
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…