The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Quadratic Equation

Posted by James on October 25, 2001 at 10:20 PM

I have an assignment where i have to enter methods into a quadratic class. the folowing methods must be used:
getCoefficient(), computeDiscriminant() (b^2-4ac), displayTwoRealRoots(), displayOneRoot() (if discriminant=0 theres only one solution. it will compute and display that root with formula x= -b/2a
I have to add those methods inside main() below.

// Program 7
// Due October 26, 2001
// This program prompts the user for the coefficients a, b, and c in
// the equation a x^2 + b x + c = 0, and then it figures out the roots
// using the quadratic equation
// x = (-b +/- sqrt(b^2 - 4ac)) / 2a

import jpb.*;
public class Quadratic
public static void main(String[] args)
double a, b, c;
double discriminant;

// Get the coefficients from the user
System.out.println("This program solves the quadratic equation: "
+ "a x^2 + b x + c = 0");
System.out.println("Enter the coefficients");
a = getCoefficient();
b = getCoefficient();
c = getCoefficient();

discriminant = computeDiscriminant(a, b, c);

if (discriminant > 0)
displayTwoRealRoots (a, b, discriminant);
else if (discriminant == 0)
displayOneRoot(a, b);
System.out.println("No real roots");

// methods go here.

I know this is huge, any help is appreciated.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us