Reputation: 1459
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;
}
}
Upvotes: 1
Views: 8491
Reputation: 796
Here's what I did:
Math.quadform = function(a, b, c) {
var pos = (-b + Math.sqrt(Math.pow(b, 2)-4*a*c))/2*a;
var neg = (-b - Math.sqrt(Math.pow(b, 2)-4*a*c))/2*a;
return {
"positiveRoot": pos,
"negativeRoot": neg
};
};
Much simpler and easier to work with IMO.
Upvotes: 0
Reputation: 1519
The line
println("Your first solution is" + QuadPlus(i));
Has two things wrong with it.
It's trying to reference a variable i
that is local to a different method. Since i
is out of the scope of the main method (the variables you declared called i
are only in the scope of the QuadPlus and QuadMinus methods), eclipse can't find a definition for i
and so it's throwing your error.
The QuadPlus and QuadMinus methods are defined to take four double
parameters 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 getting i
?
Your eventual call should look something like:
println("Your first solution is" + QuadPlus(aDouble, bDouble, cDouble, xDouble));
EDIT: You don't actually seem to use the parameter x
in your QuadThing methods, so you can just take it out of the definition, like:
... QuadPlus(double a, double b, double c) { ...
And then have the call:
QuadPlus(a, b, c);
EDIT 2: Also,
double i = ((+(b)) + Math.sqrt(( b * b) - (4 * a * c)) / (2 * a));
... 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:
double plus = ( (-(b)) + Math.sqrt(( b * b) - (4 * a * c)) ) / (2 * a);
double minus = ( (-(b)) - Math.sqrt(( b * b) - (4 * a * c)) ) / (2 * a);
Upvotes: 1
Reputation: 32576
Other's have answered your direct question, so I won't repeat that here, but I noticed that the equation you're using in the functions is incorrect. The change in sign for the result of the square root, not the -b
at the front. The functions should be:
private double QuadPlus (double a, double b, double c){
return ((-b) + Math.sqrt(( b * b) - (4 * a * c))) / (2 * a);
}
private double QuadMinus (double a, double b, double c) {
return ((-b) - Math.sqrt(( b *b) - (4 * a * c))) / ( 2 * a);
}
Upvotes: 2
Reputation: 1290
If the question is to write a solution to a quadratic equation, you should take care about the different possible results. You can have 2 solutions, 1 solution or 0 solutions.
To handle this, you can check the discriminant: d = b*b - 4*a*c
There are some more obstacles (floating point cancellation, overflows, etc.) But as an entry level exercise, just do not care about in-depth details.
--tb
Upvotes: 0
Reputation: 3876
You can drop the "double x" in the method parameter as it is not used, and then you have to call the function as: double i=QuadPlus(a, b, c); Also, use double a=read double...
Upvotes: 0
Reputation: 1131
You have to call the function with the right arguments, as in:
println("Your second solution is" + QuadMinus(a, b, c, x));
You are not using parameter x.
Upvotes: 1
Reputation: 1136
this
println("Your first solution is" + QuadPlus(i));
doesnt know what i is
it knows what a, b & c are though
println("Your first solution is: " + QuadPlus(a,b,c));
drop the x out of your function
private double QuadPlus (double a, double b, double c){
double i = ((+(b)) + Math.sqrt(( b * b) - (4 * a * c)) / (2 * a));
return i;
}
Upvotes: 2