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

write a method called quadratic that solves quadratic equations and prints their roots. recall that a quadratic equation is a polynomial equation in terms of a variable x of the form ax^2 + bx + c = 0. The formula for solving a quadratic equation is:

x= -b + or - the square root of b^2 - 4ac/2a

Please use the example where a =1, b= -7, c=10

so the roots are 5 and 2.

i am unsure of how to program this can anyone figure it out??

3. ## Re: help!! quadratic program

What have you done so far?

4. ## Re: help!! quadratic program

i wrote the algorithm and started...

algorithm is
1. get the numbers for variables a,b and c from the given equation 0= ax^2 + bx +c.
2. declare the variables with their given numbers.
3.plug the numbers into the variables for the equation x = -b + the square root of b^2 - 4ac/2a
and solve for x.
4.plug the numbers into the variables for the equation x = -b - the square root of b^2 - 4ac/2a
and solve for x.
5.print the roots for both equations.
```public class quadratic
{
public static void main (String[] args){

int variable a = 1;
int variable b = -7;
int variable c = 10;

system.out.println(compute roots(equation);
}

public static int(compute roots(variable a, variable b, variable c); {

int roots = variable -b + math.sqrt((variable b * variable b) - (4 * a * c)/(2*a));

return root;
}
}```
I know im missing the other equation for the root with subtracting the square root, and i dont know how to say my given equation im using, 0 = x2 -7x +10...in order to declare my ints. maybe you can help me where I went wrong?

5. ## Re: help!! quadratic program

You're java syntax is a bit off Also, it's not possible for a method to return 2 values, so I removed the helper method. Don't know if you have to handle imaginary roots, I left that out. I would also highly recommend using doubles when computing quadratic roots

```public class Quadratic
{
public static void main (String[] args){
double a = 1;
double b = -7;
double c = 10;
// compute positive root
double positive_root = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
// compute negative root
double negative_root = (-b-Math.sqrt(b*b-4*a*c))/(2*a);
system.out.println(positive_root + "\n"+negative_root);
}
}```

6. ## Re: help!! quadratic program

thanks!! do i have to show that i got the variable a b and c from a certain formula or no??

7. ## Re: help!! quadratic program

... they are the coefficients to the quadratic equation. In other words, you (or the user) gets to choose them. I don't see much use in this program, it can only solve one quadratic equation

Unless your assignment specifically told you to find the solution to only one equation, I would make it so the user could input any values of a,b, and c, and also add support for imaginary roots.

8. ## Re: help!! quadratic program

okayy so maybe you can help me make it so the user can be in control using the scanner??

9. ## Re: help!! quadratic program

actuallly i would prefer if it included methods and returned them this way...

can you look this one over and make any changes to fix it pleaseee
```import java.util.*; //so that I can use Scanner

{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Please type a quadratic equation, which you would like to find out the roots.");

double variablea = in.nextDouble();
double variableb = in.nextDouble();
double variablec = in.nextDouble();

// calling the method
}
// compute positive root
public static double quadratic1(double a, double b, double c) {
double positiveroot = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
return positiveroot;
}

// compute negative root
public static double quadratic2(double a, double b, double c) {
double negativeroot = (-b-Math.sqrt(b*b-4*a*c))/(2*a);
return negativeroot;
}
}```

10. ## Re: help!! quadratic program

I think you might run into a problem by naming a variable quadratic and the class quadratic... not positive on this. As a general rule, always name classes starting with upper case letters, and variables/methods starting with lower case letters (the one exception being constructors, they must have the same name as the class, so should start with an upper-case letter).

11. ## Re: help!! quadratic program

Use following code
```Scanner keyb = new Scanner(in);
int a = keyb.nextInt();
int b = keyb.nextInt();
int c = keyb.nextInt();
double d = pow(b,2);
double f = sqrt(d-(4*a*c));
double g = (-b+f)/2;
double h = (-b-f)/2;
out.printf("The Roots are %1.2f,%1.2f\n",g,h);```

This code works, trust me. It's even formattted.

12. ## Re: help!! quadratic program

To be honest, you need to follow a good naming convention as helloworld said...
```public class _ {

private static int _ = 6;

public _(){
String _ = "Hello";
System.out.println(_ + this._);
}

public static void main(String[] args) {
_ _ = new _();
_._ = 60;
System.out.println(_._);
System.out.println(_._(_));
}

public String _(_ _){
return Integer.toString(_._);
}

}```

13. ## Re: help!! quadratic program

:O

```public class Ø
{
public Ø()
{
System.out.println("No!");
}
}```

14. ## Re: help!! quadratic program

Hmm, I have the same problem. Here's my code:

```import java.util.Scanner;

public class Assignment2
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);

System.out.println("This program computes the roots of a Quadratic Equation.");

System.out.println();

double coeffA = in.nextDouble();
double coeffB = in.nextDouble();
double coeffC = in.nextDouble();

// calls the method

}
// computes positive root
public static double quadratic1(double a, double b, double c) {
double posRoot = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
return posRoot;
}

// computes negative root
public static double quadratic2(double a, double b, double c) {
double negRoot = (-b-Math.sqrt(b*b-4*a*c))/(2*a);
return negRoot;
}
}```

But when I run the Program, my program says this: "The results are NaN and NaN" What the Hell does NaN mean?

15. ## Re: help!! quadratic program

You tried to take the square root of a negative number. NaN stands for "not a number", which technically isn't true. It's just not a real number, but there are imaginary roots.