• October 14th, 2009, 10:24 PM
dscrudato21xo
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??:-?
• October 14th, 2009, 10:25 PM
dscrudato21xo
• October 14th, 2009, 11:10 PM
helloworld922
What have you done so far?
• October 14th, 2009, 11:24 PM
dscrudato21xo
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.
Code :

{
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?
• October 14th, 2009, 11:43 PM
helloworld922
You're java syntax is a bit off :P 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

Code :

{
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);
}
}

• October 15th, 2009, 07:41 AM
dscrudato21xo
thanks!! do i have to show that i got the variable a b and c from a certain formula or no??
• October 15th, 2009, 09:11 AM
helloworld922
... 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 :P

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.
• October 15th, 2009, 01:24 PM
dscrudato21xo
okayy so maybe you can help me make it so the user can be in control using the scanner??
• October 15th, 2009, 02:11 PM
dscrudato21xo
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 :)
Code :

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

• October 15th, 2009, 06:42 PM
helloworld922
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).
• October 15th, 2009, 07:21 PM
cosecant
Use following code
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.
• October 16th, 2009, 09:06 AM
Freaky Chris
To be honest, you need to follow a good naming convention as helloworld said...
Code :

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(_._);
}

}

• October 16th, 2009, 09:35 AM
helloworld922
:O

Code :

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

• October 18th, 2009, 04:27 PM
ixjaybeexi
Hmm, I have the same problem. Here's my code:

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?
• October 18th, 2009, 05:13 PM
helloworld922