Screen Shot 2013-03-23 at 3.25.37 PM.jpgThis is the code I am working with now. I was able to get the errors to disappear but it is buggy still.
The text says I should enter coefficients as follows:
a = 1
b = -5
c = 6
and get the result:
the first answer is 3Screen Shot 2013-03-23 at 3.25.37 PM.jpg
the second answer is 2
I get 5.5 for both. I guess the equation is entered wrong. hmm... keep on trucking I guess, see what I can figure.
I am trying to code this question from my self study:
In high-school algebra, you learned that the standard quadratic equation
ax2 +bx+c=0
has two solutions given by the formula
x=–b±√⎯⎯b⎯⎯–⎯4⎯ac / 2a (won't display correctly)
The first solution is obtained by using + in place of ±; the second is obtained by using – in place of ±.
Write a Java program that accepts values for a, b, and c, and then calculates the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assumethatthevalueforaisnonzero.
Here is my code;
import acm.program.*; import java.math.*; public class Quadratic extends ConsoleProgram { public void run() { println("Enter coefficients for the quadratic equation."); double a = readDouble("a: "); double b = readDouble("b: "); double c = readDouble("c: "); double positive = solution1(a, b, c); // if coefficient b is positive double negative = solution2(a, b, c); // if coefficient b is negative println("The first solution is " +negative); println("The second solution is " +positive); } private double solution1(double a, double b, double c) { //x = -b + ((Math.sqrt((b * b) - (4 * a * c))) / 2 * a); double equat = (b * b) - (4 * a * c); double result1 = - b + (Math.sqrt(equat)) / (2 * a); if (equat < 0) { println("The result cannot be calculated"); } return result1; } private double solution2(double a, double b, double c) { //x = -b - ((Math.sqrt((b * b) - (4 * a * c))) / 2 * a); double equat = (b * b) - (4 * a * c); double result2 = - b + (Math.sqrt(equat)) / (2 * a); if (equat < 0) { println("The result cannot be calculated"); } return result2; } }
Screen Shot 2013-03-23 at 2.11.12 PM.jpg