Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My program is running perfectly but my calculations arent actually working. MY M

ID: 3715113 • Letter: M

Question

My program is running perfectly but my calculations arent actually working.

MY MAIN:

//Imports
import java.util.InputMismatchException;
import java.util.Scanner;   //add import

//Begin Class Week14Debug
public class Week14Debug {

    //Begin Main Method
    public static void main(String[] args) {

        //New Scanner object
        Scanner sc = new Scanner(System.in); //add .in***

        //Declare variables
        int prev = 0;
        int curr = 0; //change wrg to curr for current reading variable**
        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); //add semicolon**
                   // 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(curr,prev); //change variable wrg to curr & switch order***

            //Set usage and call it for output
         
            System.out.printf("Your usage was: %.1f KhWs ", myCalcs.getUsage(curr, prev)); //insert an "f" to make sure the print method is formatted**

            //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()); //add a parenthesis to end the statement**

            //Set taxes and call them for output
            myCalcs.setTax();
            System.out.printf("Your taxes are: $%.2f ", myCalcs.getTax());     //change fomat to $%.2f

            //Set total bill and call it for output
            //Remove setTot**
            System.out.printf("Your total bill this month is: $%.2f ", myCalcs.getTot()); //change setTot to 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 //change to while**
        } 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 Week14Debug

MY SUBCLASS:

package week14debug;

/* Begin Class MyCalcs */
public class MyCalcs extends Week14Debug{

    /* 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 cur, int prv) {
        prev = prv;
        curr = cur;
    }

    /* Set methods*************************************************************/
    /* Set usage */
    public void setUsage() {
        use = curr - prev;      //make previous reading be subtracted from current**
    }

    /* Set rate */
    public void setRate() {
        if (use <= 500) {
            rate = RATE_A;
        } else if (use > 500 || use <= 900) {       //make sure use is set to less than or equa too 900**
            rate = RATE_B;
        } else {
            rate = RATE_C;
        }
        setSubTot();            //set the rate to subtot**
    }

    /* Set subtotal */
    public void setSubTot() {
        sub = use * rate;
        setTot(tot);            //set to setTot**
    }

    /* Set taxes */
    public void setTax() {
        tax = sub * TAX_R;      //change to multiplication**
    }

    /* Set total bill */
    public void setTot(double tot) {            //set setTot to return a double **
        tot = sub + tax;        //change to sub + tax**
    }

    /* Get methods*************************************************************/
    /* Get Usage */
    public double getUsage(double cur,double prv) {     //change method type to double**
         return use;            //make this method return the value of use variable**
    }

    /* 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 */

MY OUTPUT LOOKS LIKE THIS:

run:
Welcome to the City Power Bill Calculator!
Please enter your previous meter reading: 700
Please enter your current meter reading: 900
Your usage was: 0.0 KhWs
Your rate is: $0.0000/KwH
Your Subtotal will be: $0.08
Your taxes are: $0.00
Your total bill this month is: $0.00
Calculate a new usage? Y for Yes, N for No:

-The problem is everything comes out as 0 I need values to calculate from the methods in the subclass

Explanation / Answer

Screenshot

-------------------------------------------------------------------------------------------------------

Program

//Imports
import java.util.InputMismatchException;
import java.util.Scanner;   //add import
/* Begin Class MyCalcs */
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 cur, int prv) {
        prev = prv;
        curr = cur;
    }

    /* Set methods*************************************************************/
    /* Set usage */
    public void setUsage() {
        use = curr - prev;      //make previous reading be subtracted from current**
    }

    /* Set rate */
    public void setRate() {
        if (use <= 500) {
            rate = RATE_A;
        } else if (use > 500 || use <= 900) {       //make sure use is set to less than or equa too 900**
            rate = RATE_B;
        } else {
            rate = RATE_C;
        }
        //setSubTot();            //set the rate to subtot**
    }

    /* Set subtotal */
    public void setSubTot() {
        sub = use * rate;
        //setTot(tot);            //set to setTot**
    }

    /* Set taxes */
    public void setTax() {
        tax = sub * TAX_R;      //change to multiplication**
    }

    /* Set total bill */
    public void setTot() {            //set setTot to return a double **
        tot = sub + tax;        //change to sub + tax**
    }

    /* Get methods*************************************************************/
    /* Get Usage */
    public double getUsage() {     //change method type to double**
         return use;            //make this method return the value of use variable**
    }

    /* 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 */
//Begin Class Week14Debug
public class Week14Debug {

    //Begin Main Method
    public static void main(String[] args) {

        //New Scanner object
        Scanner sc = new Scanner(System.in); //add .in***

        //Declare variables
        int prev = 0;
        int curr = 0; //change wrg to curr for current reading variable**
        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); //add semicolon**
                   // 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(curr,prev); //change variable wrg to curr & switch order***
         
            //Set usage and call it for output
            myCalcs.setUsage();
            myCalcs.setRate();
            myCalcs.setSubTot();
            myCalcs.setTax();
            myCalcs.setTot();
            System.out.printf("Your usage was: %.1f KhWs ", myCalcs.getUsage()); //insert an "f" to make sure the print method is formatted**

            //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()); //add a parenthesis to end the statement**

            //Set taxes and call them for output
            myCalcs.setTax();
            System.out.printf("Your taxes are: $%.2f ", myCalcs.getTax());     //change fomat to $%.2f

            //Set total bill and call it for output
            //Remove setTot**
            System.out.printf("Your total bill this month is: $%.2f ", myCalcs.getTot()); //change setTot to 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 //change to while**
        } 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 Week14Debug

--------------------------------------------------------------------------------------

Note

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote