I got a assignment to write a java program to calculate the electricity bill. Can anyone help me regarding the example program to calculate the electricity bill in java.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Here is the sample example program in Java that calculate the electricity bill with given unit rate.
import java.util.Scanner;
public class ElectricityBill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompting the user to enter no of units consumed.
System.out.print(“Enter the no of units consumed: “);
double units = input.nextDouble();
// Compute the cost of the electricity bill
double cost = 0.0;
if (units <= 100)
cost = units * 0.5;
else if (units <= 300)
cost = 100 * 0.5 + (units – 100) * 0.75;
else if (units <= 400)
cost = 100 * 0.5 + 200 * 0.75 + (units – 300) * 1.2;
else
cost = 100 * 0.5 + 200 * 0.75 + 100 * 1.2 + (units – 400) * 1.5;
System.out.println(“The total electricity bill is $” + cost);
}
}
Examples our program that uses method parameters.
Scanner
class from thejava.util
package, which is used to read input from the user.2. Coming to the logic we need to check the number of unit whether it is less than 100, if so the unit calculation rate will be different.