Re: help!! quadratic program
Re: help!! quadratic program
What have you done so far?
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.
Code :
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?
Re: help!! quadratic program
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 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);
}
}
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??
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 :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.
Re: help!! quadratic program
okayy so maybe you can help me make it so the user can be in control using the scanner??
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 :)
Code :
import java.util.*; //so that I can use Scanner
public class quadratic
{
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 quadratic = in.nextDouble();
double variablea = in.nextDouble();
double variableb = in.nextDouble();
double variablec = in.nextDouble();
// calling the method
double quadraticanswer = quadratic1(variablea, variableb, variablec);
}
// 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;
}
}
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).
Re: help!! quadratic program
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.
Re: help!! quadratic program
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(_._);
}
}
Re: help!! quadratic program
:O
Code :
public class Ø
{
public Ø()
{
System.out.println("No!");
}
}
Re: help!! quadratic program
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();
System.out.println("Please input coefficient A.");
double coeffA = in.nextDouble();
System.out.println("Please input coefficient B.");
double coeffB = in.nextDouble();
System.out.println("Please input coefficient C.");
double coeffC = in.nextDouble();
// calls the method
double quadraticAnswer1 = quadratic1(coeffA, coeffB, coeffC);
double quadraticAnswer2 = quadratic2(coeffA, coeffB, coeffC);
System.out.println("The roots of the equation are " + quadraticAnswer1 + " and " + quadraticAnswer2);
}
// 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?
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.