Powere service has different rates for commercial clients than they have for res
ID: 3562880 • Letter: P
Question
Powere service has different rates for commercial clients than they have for residential clients. You are to create a program which will ask the user to enter either the word residential or commercial. You will also ask the user for the number of Kilowatt hours consumed (type double).
These are the rates you will use:
Commercial:
First 50 Kilowatt hours at $0.85 per KWHr
$0.95 per KWHr for consumption over 50 KWHrs.
Residential:
First 60 Kilowatt hours at $0.50 per KWHr
$0.65 per KWHr for consumption over 60 KWHrs.
You will extract the first character of that response which will be tested in an outer if structure.
Create your nested if structure first. It should have the following form:
if(test for commercial ){
if(test if under specified amount){
}else{
}
}else{
if(test if under specified amount){
}else{
}
}
Of course you will replace what I have in red with correct logical expressions which actually represent the correct logical expression.
Inside the four blocks if the if structure, you will compute the four different charges.
Copy this next line into the correct place in your program after importing the DecimalFormat library
public static DecimalFormat Currency = new DecimalFormat("$#,##0.00");
Also, you should copy these four rates into your main method and use them:
final double C1 = 0.85, C2 = 0.95, R1 = 0.50, R2 = 0.65;
Explanation / Answer
====================================
Program
===================================
import java.text.DecimalFormat;
import java.util.Scanner;
public class PowerService {
public static final double C1 = 0.85, C2 = 0.95, R1 = 0.50, R2 = 0.65;
public static DecimalFormat Currency = new DecimalFormat("$#,##0.00");
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word residential or commercial");
String input= sc.next();
if(!input.equalsIgnoreCase("commercial")&& !input.equalsIgnoreCase("residential")) {
System.out.println("Entered input is invalid, Enter only commercial/residential");
} else {
System.out.println("Enter the number of kilowaats");
//Added as per requirement that kilowattss must be double
double kilowatts= sc.nextDouble();
double totalCharge;
if(input.charAt(0)=='c' || input.charAt(0)=='C' ) {
if(kilowatts<=50) {
totalCharge = kilowatts*C1;
} else {
totalCharge = kilowatts*C2;
}
} else {
if(kilowatts<=60) {
totalCharge = kilowatts*R1;
} else {
totalCharge = kilowatts*R2;
}
}
System.out.println("The total Charge is:"+Currency.format(totalCharge));
}
}
}
======================================
Input and output
======================================
Case1:
Enter the word residential or commercial
commercial
Enter the number of kilowaats
50
The total Charge is:$42.50
Case2:
Enter the word residential or commercial
commercial
Enter the number of kilowaats
100
The total Charge is:$95.00
Case3:
Enter the word residential or commercial
residential
Enter the number of kilowaats
60
The total Charge is:$30.00
Case4:
Enter the word residential or commercial
residential
Enter the number of kilowaats
100
The total Charge is:$65.00
Case5:
Enter the word residential or commercial
test
Entered input is invalid, Enter only commercial/residential
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.