Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The electric company charges according to the following rate schedule: 9 cents p

ID: 3905228 • Letter: T

Question

The electric company charges according to the following rate schedule: 9 cents per kilowatt-hour (kwh) for the first 300 kwh 8 cents per kwh for the next 300 kwh (up to 600 kwh) 6 cents per kwh for the next 400 kwh (up to 1,000 kwh) 5 cents per kwh for all electricity used over 1,000 kwh. Implement the following function: Calculate_charge – Takes kwh used as parameter and returns charge as function value. The program should ask the user to enter the customer number and the kwh used. It calls the Calculate_charge function to calculate the charge. Then it will display the charge. The program will loop until the user enters -1 for customer number. The program also compute and print the number of customers, the total kilowatt-hours used, and the total charges.

Explanation / Answer

Solution:

ElectricityBill.java

/*

* File Name: ElectricityBill.java

*Author : Your Name Here

*Creation Date: Today's Date

*/

import java.util.InputMismatchException;

import java.util.Scanner;

public class ElectricityBill {

static int customerNumber; //COunter to store number of customers

static float KWH;// To store total KWHs

static float totalCharge;// To store total charge

public static void main(String[] args) {

askInput();//Asking for the input

}

//Displaying the end results

private static void display() {

System.out.println(" ***********Electricity Board************* ");

System.out.println("Total number of Customers: "+customerNumber);

System.out.println("Total number of KWHs used: "+ KWH);

System.out.println("Total Charge: "+totalCharge+" cents");

}

//Taking input from the user

private static void askInput() {

while(true) {

try {

//Declaring and Initializing the local variables to zero

int number= 0;

float watt = 0;

//Asking the customer number and watt usage until the customer types -1 as customer number

while(true){

Scanner scan = new Scanner(System.in);

System.out.println("Enter the Customer Number");

number = scan.nextInt();

//Validating the customer that it should not be less than 0

if(number>0) {

while(true) {

System.out.println("Enter Watt usage in KWH");

//handling the exception if user types other than integer a customer number.

try {

scan = new Scanner(System.in);

watt = scan.nextFloat();

if(watt >0) {

System.out.println("Total Charge for "+watt+" KWH is "+Calculate_charge(watt)+" cents");

totalCharge +=Calculate_charge(watt);

customerNumber++;

KWH +=watt;

break;

}else {

System.out.println(watt+ " is not a valid KWH");

}

}catch(InputMismatchException ime) {

System.out.println("Invalid KWH");

continue;

}

}

}else {

//Before exiting the application we are displaying the details and exiting

if(number == -1) {

display();

System.exit(0);

}

System.out.println(number+ " is not a valid Customer Number.");

}

}

}catch(InputMismatchException ime) {

System.out.println("Invalid Customer ");

continue;

}

}

}

//Calculating Charge for watt usage

public static float Calculate_charge(float kwh) {

float firstWatt = 300 * 9; //first 300 kwh charged at 9 cents

float secondWatt = 300 * 8;//Second 300 kwh charged at 8 cents upto 600kwh

float thirdWatt = 400 * 6;//Next 400 kwh charged at 6 cents utpo 1000kwh

if(kwh>0 && kwh <= 300) {

return kwh*9;// Calculating total charge for under 300

}else if(kwh>0 && kwh>300 && kwh<=600) {

return (kwh-300)*8 + firstWatt; // Calculating total charge for betwen 301 to 600 KWHs

}else if(kwh>0 && kwh>600 && kwh<=1000) {

return (kwh-600)*6 +firstWatt+ secondWatt;// Calculating total charge for betwen 601 to 100 KWHs

}else if(kwh>0 && kwh>1000) {

return (kwh-1000)*5 + firstWatt+secondWatt+thirdWatt; //Calculating total charger for over 1000KWHs

}

return 0;

}

}

Sample Run:

Enter the Customer Number
dgrdg
Invalid Customer
Enter the Customer Number
100
Enter Watt usage in KWH
301
Total Charge for 301.0 KWH is 2708.0 cents
Enter the Customer Number
123
Enter Watt usage in KWH
600
Total Charge for 600.0 KWH is 5100.0 cents
Enter the Customer Number
3435
Enter Watt usage in KWH
1003
Total Charge for 1003.0 KWH is 7515.0 cents
Enter the Customer Number
5465
Enter Watt usage in KWH
700
Total Charge for 700.0 KWH is 5700.0 cents
Enter the Customer Number
456
Enter Watt usage in KWH
267
Total Charge for 267.0 KWH is 2403.0 cents
Enter the Customer Number
46546
Enter Watt usage in KWH
-445
-445.0 is not a valid KWH
Enter Watt usage in KWH
fg
Invalid KWH
Enter Watt usage in KWH
12
Total Charge for 12.0 KWH is 108.0 cents
Enter the Customer Number
-1

***********Electricity Board*************

Total number of Customers: 6
Total number of KWHs used: 2883.0
Total Charge: 23534.0 cents

Note: If you find something wrong please do discuss in the comment section, before giving the negative feedback. If you really liked the solution then don't forget to give a big thumbs up.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote