Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Tim Cooke
Junilu Lacar
Sheriffs:
Rob Spoor
Devaka Cooray
Jeanne Boyarsky
Saloon Keepers:
Jesse Silverman
Stephan van Hulst
Tim Moores
Carey Brown
Tim Holloway
Bartenders:
Jj Roberts
Al Hobbs
Piet Souris
Forum:
Beginning Java
solve quadratic equations with arrays
Ben Abrams
Greenhorn
Posts: 4
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Marshal
Posts: 26909
82
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
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...