C?an someone tell me the errors in this case so I can double check my answers: M
ID: 3714305 • Letter: C
Question
C?an someone tell me the errors in this case so I can double check my answers:
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
double prev = 0, 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(prev,curr); //change wrg to curr
//Set usage and call it for output
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;
}
} 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
__________________________________________________________________________________________
SUBCLASS:
package week14debug;
/* 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 */
Explanation / Answer
Error 1:
The constructor MyCalcs(double, double) is undefined
In line 62(see below) of Week14Debug.java new object myCalcs created using argument constructor but there is no two argument constructor in MyCalcs class. To resolve the error a constructor with two argument must be defind.
MyCalcs myCalcs = new MyCalcs(prev,curr); //change wrg to curr
Error 2:
The method print(String) in the type PrintStream is not applicable for the arguments (String, void)
In line 68(see below) of Week14Debug class, there is a statement to output the value to console using System.out.print method. There is %d formator to print the number value but no value is passed to it since myCalcs.getUsage() returns nothing i.e. return trype is void. To resolve the error myCalcs.getUsage() should return number value
Warning 1
The assignment to variable use has no effect
In MyCalcs class in getUsage() method member variable of the MyCalcs class i.e. use assigned to self.
use = use
Error 3:
Syntax error, insert ")" to complete Expression
In line 74(see below) of the Week14Debug class there is a statement with out ) at the end of printf call. To resolve the error ) should be added at the end.
System.out.printf("Your Subtotal will be: $%.2f ", myCalcs.getSubTot();
Error 4:
The method printf(String, Object...) in the type PrintStream is not applicable for the arguments (String, void)
In line 84(see below) of Week14Debug class, there is a statement to output the value to console using System.out.print method. There is %f formator to print the double value but no value is passed to it since myCalcs.setTot() returns nothing i.e. return trype is void. To resolve the error myCalcs.setTot() should return double value
System.out.printf("Your Subtotal will be: $%.2f ", myCalcs.getSubTot();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.