FAQs
Search
Recent Topics
Flagged Topics
Hot Topics
Best Topics
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Java JSRs
Mobile
Certification
Databases
Caching
Books
Engineering
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Products
This Site
Careers
Other
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Bear Bibeault
Devaka Cooray
Campbell Ritchie
Tim Cooke
Jeanne Boyarsky
Sheriffs:
Liutauras Vilda
Junilu Lacar
Paul Clapham
Saloon Keepers:
Tim Moores
Frits Walraven
Carey Brown
Stephan van Hulst
Tim Holloway
Bartenders:
Paweł Baczyński
salvin francis
Brian Tkatch
Forum:
Beginning Java
solve quadratic equations with arrays
Ben Abrams
Greenhorn
Posts: 4
posted 3 years ago
I am suppose to write a method for solving a quadratic equation using the following header:
public static int solveQuadratic(double[] eqn, double[] roots)
I have entered 1, -3 and -4. The answer I get is "The solution is:0.0" .
import java.util.Scanner; public class QuadratilEq { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner (System.in); double[] eqn = new double[3]; double[] roots = new double[4]; // Prompt the user to enter three double integers System.out.print("Enter a:"); double a = Double.parseDouble(input.next()); //double a = input.nextDouble(); a = eqn[0]; System.out.println("Enter b:"); double b = Double.parseDouble(input.next()); //double b = input.nextDouble(); b = eqn[1]; System.out.println("Enter c:"); double c = Double.parseDouble(input.next()); //double c = input.nextDouble(); c = eqn[2]; // preparing the discreminate value double discriminate = Math.pow(eqn[1],2)-4 * eqn[0] * eqn[2]; solveQuadrat(eqn, roots); if (discriminate > 0) { System.out.println("there are two solutions: " + roots[0] + "and" + roots[1]); } else if (discriminate == 0) { System.out.println("The solution is:"+roots[0]); } else if (discriminate < 0) { System.out.println("The solutions are " +roots[2] + "i"+" and " + roots[3] + "i"); } } public static int solveQuadrat(double[] eqn, double[] roots){ double discriminate = Math.pow(eqn[1],2) - 4 * eqn[0] * eqn[2]; double one = (-eqn[1] + Math.sqrt(discriminate )) / (2 * eqn[0]); double two = (-eqn[1] - Math.sqrt(discriminate)) / (2 * eqn[0]); double i=Math.sqrt(-1); double three = (-eqn[1] + (Math.sqrt(Math.abs(discriminate))))/(2*eqn[0]); double four = (-eqn[1] + (Math.sqrt(Math.abs(discriminate))))/(2*eqn[0]); one = roots[0]; two = roots[1]; three = roots[2]; four = roots[3]; return 0; } }
Paul Clapham
Sheriff
Posts: 22846
43
I like...
posted 3 years ago
Simply put, roots[0] is always zero because you never assign it a value. I'm sure you meant to, at line 51 of your code for example, but you didn't.
Post Reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Making a Scanner (java.util.Scanner) in a jframe?
Quadratic formula result problem
N00b Question: Quadratic Equation Code
Requesting a Code Review
equation problem
More...