Jave Program - There are somethings wrong with my code and it will not run/give
ID: 3868415 • Letter: J
Question
Jave Program - There are somethings wrong with my code and it will not run/give the correct output. Please see the output needed the code just needs debugging:
THIS IS THE SUBCLASS
THIS IS THE OUTPUT
User input is in BOLD
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!
Explanation / Answer
There were alot of errors in your program. I have corrected them and fixed your code as per desired output. Let me know if you still face any issue in comments. .
MyCalcs.java
package sixdebug;
/* Begin Class MyCalcs */
public class MyCalcs {
/* Variable declarations */
private double prev, curr, use;
private final double RATE_A = .0809, RATE_B = .091, RATE_C = .109, TAX_R = .0346;
private double rate, sub, tax, tot;
/* Constructor initializes default set in main class. */
MyCalcs(double prv, double cur) {
prev = prv;
curr = cur;
}
/*
* Set methods
*************************************************************/
/* Set usage */
public void setUsage() {
use = curr - prev;
}
/* Set rate */
public void setRate() {
if (use <= 500) {
rate = RATE_A;
} else if (use > 500 && use < 900) {
rate = RATE_B;
} else {
rate = RATE_C;
}
}
/* Set subtotal */
public void setSubTot() {
sub = use * rate;
}
/* Set taxes */
public void setTax() {
tax = sub * TAX_R;
}
/* Set total bill */
public void setTot() {
tot = tax + sub;
}
/*
* Get methods
*************************************************************/
/* Get Usage */
public double getUsage() {
return this.use;
}
/* Get rate */
public double getRate() {
return rate;
}
/* Get subtotal */
public double getSubTot() {
return sub;
}
/* et taxes */
public double getTax() {
return tax;
}
/* Get total */
public double getTot() {
return tot;
}
} /* End Class MyCalcs */
Week14Debug.java
package sixdebug;
//Imports
import java.util.InputMismatchException;
import java.util.Scanner;
//Begin Class SixDebug
public class Week14Debug {
// Begin Main Method
public static void main(String[] args) {
//New Scanner object
Scanner sc = new Scanner(System.in);
//Declare variables
double prev = 0, curr = 0;
String ans = "Y";
boolean flag1 = false, YesNo;
//Ask user for input and receive it
System.out.println("Welcome to the City Power Bill Calculator!");
//Begin while loop
do {
do { /* Begin internal do loop */
try { /* Try/Catch block beginning */
System.out.print("Please enter your previous meter reading: ");
prev = sc.nextDouble();
} catch (InputMismatchException IME) {
System.err.printf("Exception: %s ", IME);
errCall(); /* Call error message */
sc.nextLine(); /* Clear buffer */
} /* End Try/Catch block */
} while (flag1); /* End internal do loop */
do { /* Begin internal do loop */
try { /* Try/Catch block beginning */
System.out.print("Please enter your current meter reading: ");
curr = sc.nextDouble();
if (prev > curr) { /* Check for previous reading < current reading */
System.out.print("Your current entry must be higher than your previous! ");
flag1 = true;
} else {
}
} catch (InputMismatchException IME) {
System.err.printf("Exception: %s ", IME);
errCall(); /* Call error message */
sc.nextLine(); /* Clear buffer */
flag1 = true;
} /* End Try/Catch block */
} while (flag1); /* End internal do loop */
//New instance of the subclass & set current and previous values
MyCalcs myCalcs = new MyCalcs(prev,curr);
//Set usage and call it for output
myCalcs.setUsage();
System.out.printf("Your usage was: %.1fKwHs ", myCalcs.getUsage());
//Set rate and call it for output
myCalcs.setRate();
System.out.printf("Your rate is: $%.4f/KwH ", myCalcs.getRate());
//Set subtotal and call it for output
myCalcs.setSubTot();
System.out.printf("Your Subtotal will be: $%.2f ", myCalcs.getSubTot());
//Set taxes and call them for output
myCalcs.setTax();
System.out.printf("Your taxes are: $%.2f ", myCalcs.getTax());
//Set total bill and call it for output
myCalcs.setTot();
System.out.printf("Your total bill this month is: $%.2f ", myCalcs.getTot());
//Run again? Validate answer. Begin inner loop.
do {
System.out.print("Calculate a new usage? Y for Yes, N for No: ");
ans = sc.next();
if (!(ans.equals("Y") || ans.equals("N"))) {
YesNo = true; /* reset incase of previous good run */
System.err.println("Exception! Please enter only a Y for Yes or an N for No.");
} else if (ans.equals("Y")) {
YesNo = false;
} else {
System.out.println("Thank you for using the program! Good Bye!");
YesNo = false;
}
} while (YesNo); // End inner loop
} while (ans.equals("Y"));//End outer while loop
} // End Main Method
// Method to output error message
public static void errCall() {
System.err.println("You must enter a numeric value. Please try again. ");
}
} // End Class sixDebug
Sample Output
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.0KwHs
Your rate is: $0.0809/KwH
Your Subtotal will be: $40.45
Your taxes are: $1.40
Your total bill this month is: $41.85
Calculate a new usage? Y for Yes, N for No: Y
Please enter your previous meter reading: 750
Please enter your current meter reading: 1350.63
Your usage was: 600.6KwHs
Your rate is: $0.0910/KwH
Your Subtotal will be: $54.66
Your taxes are: $1.89
Your total bill this month is: $56.55
Calculate a new usage? Y for Yes, N for No: Y
Please enter your previous meter reading: 750.39
Please enter your current meter reading: 1655.37
Your usage was: 905.0KwHs
Your rate is: $0.1090/KwH
Your Subtotal will be: $98.64
Your taxes are: $3.41
Your total bill this month is: $102.06
Calculate a new usage? Y for Yes, N for No: N
Thank you for using the program!
Good Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.