The question is to write two solutions to the quadratic formula. One result is when you use the plus operator in the formula and another when you use the negative operator.
My plan was to create two different methods calculating the different results of the formula with one method using the (+) and one using the (-). Then I want to call both those methods to display the results. The problem is when I call those methods in Eclipse it says there is an error “i cannot be resolved to a variable.” Is my solution to the problem right, am I missing anything and how can I fix the error?
import acm.program.*;
public class QuadraticFormula extends ConsoleProgram {
public void run(){
println("Enter the coefficients for the quadratic equation: ");
int a = readInt("Please enter the value of a: ");
int b = readInt("Please enter the value of b: ");
int c = readInt("Please enter the value of c: ");
println("Your first solution is" + QuadPlus(i));
println("Your second solution is" + QuadMinus(i));
}
private double QuadPlus (double a, double b, double c, double x){
double i = ((+(b)) + Math.sqrt(( b * b) - (4 * a * c)) / (2 * a));
return i;
}
private double QuadMinus (double a, double b, double c, double x) {
double i = ((-(b)) + Math.sqrt(( b *b) - (4 * a * c)) / ( 2 * a));
return i;
}
}
The line
Has two things wrong with it.
It’s trying to reference a variable
ithat is local to a different method. Sinceiis out of the scope of the main method (the variables you declared callediare only in the scope of the QuadPlus and QuadMinus methods), eclipse can’t find a definition foriand so it’s throwing your error.The QuadPlus and QuadMinus methods are defined to take four
doubleparameters each, so when you call them, you have to put four doubles in the parameter brackets. Otherwise, how would the program know what a, b, … etc are from just gettingi?Your eventual call should look something like:
EDIT: You don’t actually seem to use the parameter
xin your QuadThing methods, so you can just take it out of the definition, like:And then have the call:
EDIT 2: Also,
… will divide only the square root by 2a, rather than the whole thing as in the quadratic formula. To fix it, move the last bracket before the slash divide. This applies to both methods.
Lastly, the +/- part of the formula doesn’t affect the b in front; b is always (-b). The +/- is whether you plus or minus the square root: