a. Give a brief description of the problem (Specification and assumptions) public class electricbill { public static void main(String[] args) { int presentReading = 0, previousReading = 0; Scanner kbd = new Scanner(System.in); String consumer = ""; // to hold name of consumer char cType = 'x'; // to hold type of consumer int nCMUsed; // to hold number of units consumed int minCMResidential = 12; // to hold cut-off for minimum Bill // for residential consumers double minBillResidential = 98; // minimum bill for <= 12 units float rateResidential = 8.1583F; // cost of 1 unit above the min. consumption int minCMCommercial = 7; // to hold cut-off for minimum Bill for commercial consumers double minBillCommercial = 600.00; // minimum bill for <= 20 units used float rateCommercial = 50.00F; // cost of 1 unit above the min. // consumption for commercial consumers double amountDue = 0.0; // to hold the amount due showIntroduction(); // prompt message for name System.out.print("Enter the name of the electricity consumer: "); // input name consumer = kbd.nextLine(); // store previous reading using method readPreviousReading(); previousReading = readPreviousReading(); // store cuurent reading using method readPresentReading(); presentReading = readPresentReading(previousReading); // store electricity consumption nCMUsed = presentReading - previousReading; // store consumer type cType = readTypeOfConsumer(); // compute due amount amountDue = computeAmountDue(nCMUsed, cType, minCMResidential, minBillResidential, minCMCommercial, minBillCommercial, rateResidential, rateCommercial); showBill(consumer, cType, previousReading, presentReading, nCMUsed, amountDue); System.exit(0); } // end of main method public static void showBill(String n, char t, int previous, int present, int c, double amount) { System.out.println("-------------------------------"); System.out.println("Electricity Billing Statement"); System.out.println("Name of Consumer: " + n); System.out.print("Type of consumer: "); if (Character.toLowerCase(t) == 'r') System.out.println("Residential"); if (Character.toLowerCase(t) == 'c') System.out.println("Commercial"); System.out.println("Meter reading last month = " + previous + " units "); System.out.println("Meter reading this month = " + present + " units"); System.out.println("Units Consumed = " + c + " units"); System.out.println("Amount Due = ₱ " + amount); return; } public static void showIntroduction() { System.out.println(); System.out.println("This program will prepare a bill for a electricity " + "distribution consumer."); System.out.println("--------------------------------------------------------------------------"); System.out.println("You will be asked to enter the name of the " + "consumer,\n\tthe electricity meter reading last month, " + "\n\t and the electricity reading this month."); System.out.println("---------------------------------------------------------------------------"); return; // optional since return type of the method is void } public static int readPreviousReading() { int reading = 0; Scanner kbd = new Scanner(System.in); do { System.out.print("Enter the meter reading last month: "); // input previous reading reading = Integer.parseInt(kbd.nextLine()); // if less than 0 if (reading < 0) { System.out.println("The meter reading cannot be negative."); } } while (reading < 0); // loop until value is positive return reading; } public static int readPresentReading(int previous) { int reading = 0; Scanner kbd = new Scanner(System.in); do { System.out.print("Enter the meter reading for this month: "); // input current reading reading = Integer.parseInt(kbd.nextLine()); // check if current reading is less than previous reading if (reading < previous) { System.out.println("Invalid datum entry! The present reading " + "must be greater than the previous reading."); } } while (reading < previous); // loop until current > previous return reading; } public static char readTypeOfConsumer() { char t = 'x'; Scanner kbd = new Scanner(System.in); do { System.out.print("Enter the type of the consumer (type r " + "for residential or c for commercial): "); // input consumer type String input = kbd.nextLine(); // store first character of input t = input.charAt(0); if (t != 'r' && t != 'R' && t != 'c' && t != 'C') { System.out.println("The valid types are r for residential " + "and c for commercial."); } } while (t != 'r' && t != 'R' && t != 'c' && t != 'C'); // loop until type is valid return t; } public static double computeAmountDue(int c, char t, int min1, double minB1, int min2, double minB2, float rate1, float rate2) { double amount = 0; switch (t) { case 'r': case 'R': if (c <= min1) { amount = minB1; } else { amount = minB1 + (c - min1) * rate1; } break; case 'c': case 'C': if (c <= min2) { amount = minB2; } else { amount = minB2 + (c - min2) * rate2; } } return amount; } } // end of class

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

a. Give a brief description of the problem (Specification and assumptions)

public class electricbill {

public static void main(String[] args) {
int presentReading = 0, previousReading = 0;
Scanner kbd = new Scanner(System.in);
String consumer = ""; // to hold name of consumer
char cType = 'x'; // to hold type of consumer
int nCMUsed; // to hold number of units consumed
int minCMResidential = 12; // to hold cut-off for minimum Bill
// for residential consumers
double minBillResidential = 98; // minimum bill for <= 12 units

float rateResidential = 8.1583F; // cost of 1 unit above the min. consumption
int minCMCommercial = 7; // to hold cut-off for minimum Bill for commercial consumers
double minBillCommercial = 600.00; // minimum bill for <= 20 units used
float rateCommercial = 50.00F; // cost of 1 unit above the min.
// consumption for commercial consumers
double amountDue = 0.0; // to hold the amount due
showIntroduction();
// prompt message for name
System.out.print("Enter the name of the electricity consumer: ");
// input name
consumer = kbd.nextLine();
// store previous reading using method readPreviousReading();
previousReading = readPreviousReading();
// store cuurent reading using method readPresentReading();
presentReading = readPresentReading(previousReading);
// store electricity consumption
nCMUsed = presentReading - previousReading;
// store consumer type
cType = readTypeOfConsumer();
// compute due amount
amountDue = computeAmountDue(nCMUsed, cType, minCMResidential,
minBillResidential, minCMCommercial,
minBillCommercial, rateResidential,
rateCommercial);

showBill(consumer, cType, previousReading, presentReading, nCMUsed,
amountDue);
System.exit(0);
} // end of main method
public static void showBill(String n, char t, int previous, int present,
int c, double amount) {
System.out.println("-------------------------------");
System.out.println("Electricity Billing Statement");
System.out.println("Name of Consumer: " + n);
System.out.print("Type of consumer: ");
if (Character.toLowerCase(t) == 'r')
System.out.println("Residential");
if (Character.toLowerCase(t) == 'c')
System.out.println("Commercial");
System.out.println("Meter reading last month = " + previous +
" units ");
System.out.println("Meter reading this month = " + present +
" units");
System.out.println("Units Consumed = " + c + " units");
System.out.println("Amount Due = ₱ " + amount);
return;
}
public static void showIntroduction() {
System.out.println();
System.out.println("This program will prepare a bill for a electricity " +
"distribution consumer.");
System.out.println("--------------------------------------------------------------------------");
System.out.println("You will be asked to enter the name of the " +
"consumer,\n\tthe electricity meter reading last month, " +
"\n\t and the electricity reading this month.");
System.out.println("---------------------------------------------------------------------------");
return; // optional since return type of the method is void
}
public static int readPreviousReading() {
int reading = 0;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the meter reading last month: ");
// input previous reading
reading = Integer.parseInt(kbd.nextLine());
// if less than 0
if (reading < 0) {
System.out.println("The meter reading cannot be negative.");
}
} while (reading < 0); // loop until value is positive
return reading;
}
public static int readPresentReading(int previous) {
int reading = 0;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the meter reading for this month: ");
// input current reading
reading = Integer.parseInt(kbd.nextLine());
// check if current reading is less than previous reading
if (reading < previous) {
System.out.println("Invalid datum entry! The present reading " +
"must be greater than the previous reading.");
}
} while (reading < previous); // loop until current > previous
return reading;
}
public static char readTypeOfConsumer() {
char t = 'x';
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the type of the consumer (type r " +
"for residential or c for commercial): ");
// input consumer type
String input = kbd.nextLine();
// store first character of input
t = input.charAt(0);
if (t != 'r' && t != 'R' && t != 'c' && t != 'C') {
System.out.println("The valid types are r for residential " +
"and c for commercial.");
}
} while (t != 'r' && t != 'R' && t != 'c' && t != 'C'); // loop until type is valid
return t;
}
public static double computeAmountDue(int c, char t, int min1, double minB1,
int min2, double minB2, float rate1,

float rate2) {

double amount = 0;
switch (t) {
case 'r':
case 'R':
if (c <= min1) {
amount = minB1;
} else {
amount = minB1 + (c - min1) * rate1;
}
break;
case 'c':
case 'C':
if (c <= min2) {
amount = minB2;
} else {
amount = minB2 + (c - min2) * rate2;
}
}
return amount;
}
} // end of class

Expert Solution
steps

Step by step

Solved in 7 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY