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
This program has the following explanations of calculating electricity bill
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
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
How to find reverse number using method In this article, we will discuss the concept…
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…