Question
The if else statements in my subclass will not run, they give the same output every time no matter what the total is MY MAIN
The if else statements in my subclass will not run, they give the same output every time no matter what the total is
MY MAIN CLASS:
//Begin Main Method public static void main(String[] args) {
Subclass MyUtility = new Subclass(); //Gives access to subclass
String choice; //Set variable for loop that allows user to rerun
do { //Loop gets readings from customer System.out.println("Welcome to the City Power Bill Calculator!");
Scanner input = new Scanner(System.in);
System.out.print("Please enter your previous meter reading:"); double preread = input.nextDouble(); //Users first input
System.out.print("Please enter your current reading:"); double curread = input.nextDouble(); //Users second input
System.out.printf("Your usage was %.1f KhWs ", MyUtility.getUsage(curread, preread)); //Call getusage
//Assign usage to the variable "total" to determine rate double total = MyUtility.getUsage(curread, preread);
System.out.printf("Your rate was $%.4f/KhWs ", MyUtility.getRate()); //Return rate
double rate = MyUtility.getRate(); //Assign rate to variable
System.out.printf("Your subtotal is $%.2f ", MyUtility.getSubtotal(total, rate)); //Call getsubtotal
double subtotal = MyUtility.getSubtotal(total, rate); //Assign subtotal final double TAXPERCENT = 0.0346; //Assign tax percent
System.out.printf("Your tax was $%.2f ", MyUtility.getTax(subtotal, TAXPERCENT));//Call gettax double tax = MyUtility.getTax(subtotal, TAXPERCENT); //Assign tax double totalbill = (subtotal + tax); //Assign bill total
System.out.printf("Your total bill this month is $%.2f ", totalbill); //Return the over all bill amount
System.out.println("Would you like to run again? y or n"); choice = input.next(); //User can rerun program or not
} while (choice.equalsIgnoreCase("y")); //Reruns program
System.out.println("Thank you for " + "using the program! Goodbye"); //Ends program }
Subclass
//Set variables for subclass private final double RATE_A = 0.0809; private final double RATE_B = 0.091; private final double RATE_C = 0.109; private double rate; private double tax; private double total; private double subtotal; private double finalBill; public final double TAXPERCENT = 0.0346;
//Set public variables to call to methods public Subclass() { this(0,0); } /** * * @param curread * @param preread */ public Subclass(double curread, double preread) { total = curread - preread; setRate(total); //Determines the users usage and sets it to setRate }
/** * * @return */ public double getRate() { //Return users rate return rate; }
/** * * @param total */ private void setRate(double total) { //Calculate rate for customers bill if (total <= 500) { rate = RATE_A; } else if (total > 500 && total <= 900) { rate = RATE_B; } else { rate = RATE_C; } setSubtotal(); }
/** * * @param subtot * @param tot * @return */ public static final double getTax(double subtot, double tot) { double tax; tax = (subtot * tot); return tax; //Return users tax amount
}
/** * */ public void setTax() { //Calculates the users tax tax = subtotal * TAXPERCENT; }
/** * * @param current * @param previous * @return */ public static double getUsage(double current, double previous) { double total; total = (current - previous); return total; //Calculate rate, subtotal, tax, & total for users bill }
/** * * @param usage */ public void setUsage(double usage) { this.total = usage; //Returns the usage total for customers bill }
/** * * @param usage * @param rating * @return */ public static double getSubtotal(double usage, double rating) { double subtotal; subtotal = (usage * rating); return subtotal; //Returns users subtotal } private void setSubtotal() { //Calculates subtotal for customers bill subtotal = total * rate; setFinalBill(); }
public double getFinalBill() { //Returns final bill amount return finalBill; } private void setFinalBill() { //Calculates the final bill amount finalBill = subtotal + tax; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started