# Electric Bill with Java Assignment Solution

June 12, 2024
Sofiya Marcus
🇬🇧 United Kingdom
Java
PhD-qualified in Computer Science from the University of Bolton, I am Sofiya Marcus, a Java assignment expert with 7 years of experience. I specialize in delivering high-quality, tailored solutions for complex programming tasks.
Key Topics
• Calculate the Electric Bill
Tip of the day
News

## Calculate the Electric Bill

Program #4

For this assignment, you will create a Java program that will calculate a business customer’s amount due for their electric bill. The program will begin by asking the business customer for their account number (a ten-digit value). Next, it will ask if the bill is late. Then, the program will ask the business customer how many properties they own. Finally, it will ask the business customer to enter the number of kilowatt-hours (kWh) used at each property. The amount due depends on whether the bill is late and the total number of kWh used.

The cost of electricity is \$0.050445 per kWh. Late bills incur a late fee, which is equal to 15% of the amount due.

The program will use the information above to compute the business customer’s amount due. After calculating the amount due, for a bill paid on time, the program will print the business customer’s account number, total number of kWh used and amount due onto the screen. For a late bill, the program will print the business customer’s account number, total number of kWh used, amount due, late fee and total due onto the screen. Each item printed will be displayed on a separate line with an appropriate label and symbols. Example output is shown below (note: the output shown below is an example, your values will be different depending on the data input – your labels and symbols should match those shown below). All dollar values will be displayed to two decimal places, as shown below. After printing the billing information, 2 lines containing 45 asterisks per line will be printed (you must use a nested for loop to complete the java assignment).

You must make use of both variables and constants in your program. Be sure to name your constants and variables appropriately.

This program should make use of both if statements and a loop.

This program will make use of 6 methods. The first method will be used to ask for the business customer’s account number. The second method will be used to ask if the bill is late. The third method will be used to ask for the number of properties and perform a cumulative sum, based on the number of kWh used at each property. The fourth method will be used to calculate the amount due. The fifth method will be used to print the billing information, as described above. The sixth method will be used to print the lines of asterisks onto the screen. Several of these methods will make use of parameters. At least 3 of these methods will return a value.

Your output should appear exactly as shown below. User input is in red.

Sample Run #1:

Welcome to the Electricity Billing Program!

Please enter your account number: 0123456789 Is your bill late (enter true or false)? false

Please enter the number of properties that you own: 4 ]

Please enter the number of kWh used at property #1: 500

Please enter the number of kWh used at property #2: 1250

Please enter the number of kWh used at property #3: 3000

Please enter the number of kWh used at property #4: 4500

Account Number: 0123456789

Total Number of kWh Used = 9250

Amount Due = \$466.62

Sample Run #2:

Welcome to the Electricity Billing Program!

Please enter your account number: 9876543210 Is your bill late (enter true or false)? true

Please enter the number of properties that you own: 3

Please enter the number of kWh used at property #1: 3500

Please enter the number of kWh used at property #2: 1325

Please enter the number of kWh used at property #3: 435

Account Number: 9876543210

Total Number of kWh Used = 5260

Amount Due = \$265.34

Late Fee = \$39.80

Total Due = \$305.14

Solution:

```import java.util.Scanner; public class Main { private static final double KWH_COST = 0.050445; private static final double LATE_FEE_RATE = 0.15; private static final int NUM_ASTERISKS = 45; private static Scanner in = new Scanner(System.in); // Read an account number from user input private static String readAccountNumber() { System.out.print("Please enter your account number: "); return in.nextLine(); } // Read a value to check if bill being paid is late private static boolean readBillIfLate() { System.out.print("Is your bill late (enter true or false)? "); return in.nextLine().equalsIgnoreCase("true"); } // Return the total kwh used for all properties private static int readTotalKwhUsed() { System.out.print("Please enter the number of properties that you own: "); int numProperties = Integer.parseInt(in.nextLine()); // Ask for the kwh usage for each property int totalKwh = 0; for (int i = 0; i < numProperties; i++) { System.out.print("Please enter the number of kWh used at property #" + (i + 1) + ": "); totalKwh += Integer.parseInt(in.nextLine()); } return totalKwh; } // calculate the total amount due based on the kwh used private static double calculateAmountDue(int totalKwhUsed) { return totalKwhUsed * KWH_COST; } // Print the billing information private static void printBillingInformation(String accountNumber, int totalKwhUsed, boolean isLate) { double amountDue = calculateAmountDue(totalKwhUsed); System.out.println(); System.out.println("Account Number: " + accountNumber); System.out.println("Total Number of kWh Used = " + totalKwhUsed); System.out.println("Amount Due = \$" + String.format("%.2f", amountDue)); if (isLate) { // Apply 15% fee for late double lateFee = LATE_FEE_RATE * amountDue; System.out.println("Late Fee = \$" + String.format("%.2f", lateFee)); System.out.println("Total Due = \$" + String.format("%.2f", amountDue + lateFee)); } printDividerLine(); printDividerLine(); } // Prints out 45 asterisks private static void printDividerLine() { for(int i = 0; i < NUM_ASTERISKS; i++) { System.out.print("*"); } System.out.println(); } // Entry point of the program public static void main(String[] args) { // Print an intro System.out.println("Welcome to the Electricity Billing Calculator"); System.out.println(); // Ask for the account number String accountNumber = readAccountNumber(); // Ask if bill is late boolean isLate = readBillIfLate(); // Read the total kwh used int totalKwhUsed = readTotalKwhUsed(); // Print the billing information printBillingInformation(accountNumber, totalKwhUsed, isLate); } } ```

## Similar Services

Check out our range of programming homework samples to see the high standards we maintain. Each example highlights our proficiency in various programming languages and problem-solving skills. Let these samples demonstrate our dedication to providing top-quality assistance for all your programming needs.