Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Stuck, anyone care to help. Thx

1. ## Stuck, anyone care to help. Thx

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

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 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

2. ## Re: Stuck, anyone care to help. Thx

Here is the problem I am trying to work out, it was not clear above.
Screen Shot 2013-03-23 at 3.25.37 PM.jpg

3. ## 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.

4. ## 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.

5. ## Re: Stuck, anyone care to help. Thx

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.

6. ## Re: Stuck, anyone care to help. Thx

I have got it working well now, thanks.