Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

package sixdebug; //Imports import java.util.InputMismatchException; //Begin Class SixDebug public class Week14Debug { //Begin Main Method public static void main(String[] args) { //New Scanner object Scanner sc = new Scanner(System); //Declare variables int prev, wrg; 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.nextInt(); } 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.nextInt(); 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,wrg); //Set usage and call it for output myCalcs.setUsage(); System.out.print("Your usage was: %dKwHs ", 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: $%.2d ", myCalcs.getTax()); //Set total bill and call it for output myCalcs.setTot(); System.out.printf("Your total bill this month is: $%.2f ", myCalcs.setTot()); //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; } } until (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 

THIS IS THE SUBCLASS

package sixdebug; /* Begin Class MyCalcs */ public class MyCalcs { /* Variable declarations */ private int 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(int prv, int cur) { prev = prv; curr = cur; } /* Set methods*************************************************************/ /* Set usage */ public void setUsage() { use = prev - curr; } /* 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 void getUsage() { use = use; } /* Get rate */ public double getRate() { return sub; } /* Get subtotal */ public double getSubTot() { return rate; } /* et taxes */ public double getTax() { return tax; } /* Get total */ public double getTot() { return tot; } } /* End Class MyCalcs */ 

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!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions