Javatpoint Logo
Javatpoint Logo

Electricity Bill Program in Java

In Java, there are various ways through which we can calculate electricity bills. We can calculate electricity bills using static values, command-line argument, method and functions, user-defined method, and do-while and for loop.

Let's understand each one of them one by one:

By Using the Static Method

In this way of calculating the electricity bill, we use the static method with static values, i.e., units. We use if and else if statements to check the number of units. Let's implement the logic for calculating the electricity bill by using the static method:

ElectricityBillExample1.java

Output:

Electricity Bill Program in Java

By Using the Scanner Class

In this way of calculating the electricity bill, we also use the static method. The only difference between both of them is that we take input from the user for the number of units rather than using static values. Let's implement the logic for calculating the electricity bill by using the Scanner class:

ElectricityBillExample2.java

Output:

Electricity Bill Program in Java

By Using Command Line Argument

In this way, we also use the same approach which we used for the previous ones. Here, we use the command line argument to take value for the number of units. Let's implement the logic for calculating the electricity bill by using the command line argument:

ElectricityBillExample3.java

Output:

Electricity Bill Program in Java

By Using Inheritance

In this way of calculating electricity bills, we use the Inheritance concept of Oops. We create a child class, i.e., CalculateBill, that calculates the electricity bill for the user given input units. Let's implement the logic for calculating the electricity bill by using Inheritance.

ElectricityBillExample4.java

Output:

Electricity Bill Program in Java

By Using a Separate Class (without Inheritance)

In this way, we create a new separate class, CalculateElectricityBill, for calculating electricity bills. In the parameterized constructor of this class, we calculate the electricity bill for the given number of units.

In the main() method of the main class, we create an object of the CalculateElectricityBill class and pass the number of units to its constructor.

Let's implement the code to understand how we can use the separate class for calculating electricity bills:

ElectricityBillExample5.java

Output:

Electricity Bill Program in Java

By Using the User-Defined Method

It is one of the simplest ways to calculate electricity bills for the given number of units. In this approach, we put the code of calculating the electricity bill in a user-defined method and call it from anywhere in the code.

Let's implement the code to understand how we can use the user-defined method for calculating electricity bills:

ElectricityBillExample6.java

Output:

Electricity Bill Program in Java

All the above ways are used in different scenarios. Each and every method is helpful in calculating electricity bills, and each one has its own importance in Java.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA