# Stuck, anyone care to help. Thx

• March 23rd, 2013, 04:23 PM
kungfuthug
Stuck, anyone care to help. Thx
Attachment 1928This 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 3Attachment 1928

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;

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; } }```

Attachment 1926
• March 23rd, 2013, 04:27 PM
kungfuthug
Re: Stuck, anyone care to help. Thx
Here is the problem I am trying to work out, it was not clear above.
Attachment 1927
• March 23rd, 2013, 04:37 PM
Norm
Re: Stuck, anyone care to help. Thx
Please copy any text you want anyone to read and paste it here on the forum. Images are hard to read and their text can't be copied.

Can you explain what your problems are? Post the program's output and add some comments saying what is wrong and show what the output should be.
• March 23rd, 2013, 04:47 PM
kungfuthug
Re: Stuck, anyone care to help. Thx
My code is copied onto this forum, and I included two screenshots. One of the exercise and one of my output that is incorrect. I can view this fine on my thread?

I am wondering if anyone see's anyway to tidy up my code and give me some insight as to why I cannot calculate the correct result(s).

I did try and copy the text from my assignment here, but the format was all squirrelly with copy, paste... so I included a screen shot.

My apologies if I am making things more difficult than they need to be, I am famous for doing just that. Thanks for the patience.
• March 23rd, 2013, 05:01 PM
Norm
Re: Stuck, anyone care to help. Thx
Quote:

why I cannot calculate the correct result(s).
Can you Post the program's output and add some comments saying what is wrong and show what the output should be.

One problem with testing the posted code is that it uses third party packages.
It would be better if it were written using Java SE packages and classes.
• March 23rd, 2013, 11:32 PM
kungfuthug
Re: Stuck, anyone care to help. Thx
I have got it working well now, thanks.