I submited this question before but they didnt follow the requirements/contraint
ID: 3857799 • Letter: I
Question
I submited this question before but they didnt follow the requirements/contraints or format...they didnt use methods. Please use comments.
Write a JAVA program that will ask for the customer’s last and current meter reading in Kilowatt Hours (KwHs). Determine the amount of usage for the month and calculate a subtotal (before tax) and a total amount due (tax included) using the following constraints.
Constraints
Rate A: For 500 KwHs or less = $0.0809 / KwH
Rate B: For 501 to 900 KwHs = $0.091 / KwH
Rate C: For greater than 900 KwHs = $0.109 / KwH
Utilities Tax is 3.46% regardless of usage.
Requirements
Create a separate (external to the main class) subclass MyUtility()
Rate and tax variables should be private to the subclass
Use a constructor to initialize the default reading values to 0
Use another constructor to set the reading values
Use set and get methods in MyUtility() to determine the usage, rate, subtotal, tax, and final bill
Format all output as follows:
o Usage to 1 decimal place, KhWs
o Rate to 4 decimal places, monetary
o Subtotal to 2 decimal places, monetary o Taxes to 2 decimal places, monetary
o Total to 2 decimal places, monetary
Implement a loop to return and enter new values (run the program again) if the user wishes to Hints
Set methods can do all the computation
Set methods can call other set methods
The type of loop can be of your choosing
Make sure you use Java coding conventions
Expected Output
Below is a sample run with three iterations. User input is in red.
Welcome to the City Power Bill Calculator!
Please enter your previous meter reading: 750
Please enter your current meter reading: 1250
Your usage was: 500.0 KwHs
Your rate was: $0.0809/KwH
Your subtotal is: $40.45
Taxes are: $1.40
Your total bill this month is: $41.85
Would you like to calculate a new usage?
(Y for Yes, N to exit): y
Please enter your previous meter reading: 750
Please enter your current meter reading: 1350.63
Your usage was: 600.6 KwHs
Your rate was: $0.0910/KwH
Your subtotal is: $54.66
Taxes are: $1.89
Your total bill this month is: $56.55
Would you like to calculate a new usage?
(Y for Yes, N to exit): y
Please enter your previous meter reading: 750.39
Please enter your current meter reading: 1655.37
Your usage was: 905.0 KwHs
Your rate was: $0.1090/KwH
Your subtotal is: $98.64 Taxes are: $3.41
Your total bill this month is: $102.06
Would you like to calculate a new usage?
(Y for Yes, N to exit): n
Thank you for using this program. Goodbye!
Deliverables
Explanation / Answer
Main.java
import java.util.Scanner;
class Main
{
public static void main (String[] args){
Scanner input=new Scanner(System.in);
MyUtility utility=new MyUtility();
utility.setUtilityTaxPercent(3.46);
double prvsReading, currentReading;
String response;
System.out.println("Welcome to the City Power Bill Calculator!");
while(true){
System.out.print("Please enter your previous meter reading:");
prvsReading=Double. parseDouble(input.nextLine());
System.out.print("Please enter your current meter reading:");
currentReading=Double. parseDouble(input.nextLine());
utility.setPreviousReading(prvsReading);
utility.setCurrentReading(currentReading );
System.out.println(String.format("Your usage was: %.1f KwHs",utility.getUsage()));
System.out.println(String.format("Your rate was: $%.4f/KwHs",utility.getRate()));
System.out.println(String.format("Your subtotal is: $%.2f",utility.getSubtotal()));
System.out.println(String.format("Taxes are: $%.2f",utility.getTax()));
System.out.println(String.format("Your total bill this month is: $%.2f",utility.getTotalBill()));
System.out.print("Would you like to calculate a new usage? "+
"(Y for Yes, N to exit):" );
response=input.nextLine();
if(response.equals("n")||response.equals("N"))
break;
}
System.out.println("Thank you for using this program. Goodbye!");
}
}
class MyUtility{
private double prvsReading;
private double currentReading;
private double utilityTaxPercent;
MyUtility(){
prvsReading=0;
currentReading=0;
utilityTaxPercent=0;
}
MyUtility(double prvs,double curr){
prvsReading=prvs;
currentReading=curr;
utilityTaxPercent=3.46;
}
public double getPreviousReading(){
return prvsReading;
}
public double getCurrentReading(){
return currentReading;
}
public void setPreviousReading(double prvs){
prvsReading=prvs;
}
public void setCurrentReading(double curr){
currentReading=curr;
}
public double getUtilityTaxPercent(){
return utilityTaxPercent;
}
public void setUtilityTaxPercent(double tax){
utilityTaxPercent=tax;
}
public double getUsage(){
return currentReading-prvsReading;
}
public double getRate(){
double usage=getUsage();
if(usage<=500)
return 0.0809;
else if(usage>500&&usage<=900)
return 0.091;
else
return 0.109;
}
public double getSubtotal(){
return getUsage()*getRate();
}
public double getTax(){
return (getSubtotal()*getUtilityTaxPercent())/100;
}
public double getTotalBill(){
return getSubtotal()+getTax();
}
}