Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First, you need to anticipate the possibility of complex roots.
- -----------------
- public class ComplexNumber
- {
- private double real;
- private double imaginary;
- public ComplexNumber() {
- this(0.0, 0.0);
- }
- public ComplexNumber( double real, double imaginary ) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public double[] get() {
- double[] complex = new double[2];
- complex[0] = real;
- complex[1] = imaginary;
- return complex;
- }
- public double[] set( double[] complex) {
- double[] old = new double[2];
- old[0] = real;
- old[1] = imaginary;
- real = complex[ 0 ];
- imaginary = complex[ 1 ];
- return old;
- }
- public double getReal() {
- return real;
- }
- public double setReal( double real ) {
- double old = this.real;
- this.real = real;
- return old;
- }
- public double getImaginary() {
- return imaginary;
- }
- public double setImaginary( double imaginary ) {
- double old = this.imaginary;
- this.imaginary = imaginary;
- return old;
- }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append( (0 != real) ? Double.toString( real ):"");
- sb.append( (0 != real && 0 != imaginary) ? " + ":"");
- sb.append( (0 != imaginary) ? Double.toString( imaginary ) + "i":"");
- return sb.toString();
- }
- }
- -----------------
- Next, handle extracting and reporting the roots of quadratic equations.
- -----------------
- public class QuadraticEquation
- {
- public ComplexNumber[] calculateRoots(double[] coefficients) {
- // calculate the roots of the quadratic equation 0 = ax² + bx + c
- // for coefficients a, b, and c where a = coefficients[ 0 ],
- // b = coefficients[ 1 ], and c = coefficients[ 2 ]
- // ...defer to worker signature
- return calculateRoots(
- coefficients[ 0 ], coefficients[ 1 ], coefficients[ 2 ]
- );
- }
- public ComplexNumber[] calculateRoots(double a, double b, double c) {
- // calculate the roots of the quadratic equation 0 = ax² + bx + c
- // for coefficients a, b, and c
- ComplexNumber[] roots;
- double B = (0 == b) ? 0 : -b; // avoid using -0.0 when b is zero
- double discriminant = b * b - 4 * a * c;
- if (0 == discriminant) {
- roots = new ComplexNumber[1];
- roots[0] = new ComplexNumber(B / (2 * a), 0.0);
- } else if (0 < discriminant) {
- roots = new ComplexNumber[2];
- roots[0] = new ComplexNumber(
- (B - Math.sqrt(discriminant)) / (2 * a), 0
- );
- roots[1] = new ComplexNumber(
- (B + Math.sqrt(discriminant)) / (2 * a), 0
- );
- } else {
- roots = new ComplexNumber[2];
- roots[0] = new ComplexNumber(
- B / (2 * a), -Math.sqrt(-discriminant)/ (2 * a)
- );
- roots[1] = new ComplexNumber(
- B / (2 * a), Math.sqrt(-discriminant) / (2 * a)
- );
- }
- return roots;
- }
- public void print(double[] coefficients, ComplexNumber[] roots) {
- String c = "\nFor the quadratic equation 0 = ax² + bx + c " +
- "with a = " + coefficients[0] + ", b = " + coefficients[1] +
- ", and c = " + coefficients[2];
- String r;
- if (1 == roots.length) {
- r = ",\nthe single root is x = " + (roots[0]).toString();
- }
- else {
- r = ",\nthe two distinct roots are:" +
- " x₁ = "+ (roots[0]).toString() +
- " and x₂ = " + (roots[1]).toString();
- }
- System.out.println( c + r );
- }
- }
- -----------------
- Finally, manage input collection and use of the QuadraticEquation class.
- -----------------
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class QuadraticEquationRunner
- {
- public static void main( String[] args) {
- // collect user inputs for coefficients a, b, and c of the
- // quadratic equation: 0 = ax² + bx + c
- double[] coefficients = readCoefficients();
- QuadraticEquation qe = new QuadraticEquation();
- ComplexNumber[] roots = qe.calculateRoots( coefficients );
- qe.print( coefficients, roots );
- }
- private static double[] readCoefficients() {
- Scanner scan = new Scanner( System.in );
- double[] coefficients = new double[ 3 ];
- System.out.println(
- "For the quadratic equation: 0 = ax² + bx + c:"
- );
- coefficients[ 0 ] = readDouble(
- "1) enter coefficient a (for x² term)", scan
- );
- coefficients[ 1 ] = readDouble(
- "2) enter coefficient b (for x term)", scan
- );
- coefficients[ 2 ] = readDouble(
- "3) enter c (constant term)", scan
- );
- return coefficients;
- }
- private static double readDouble( String prompt, Scanner scan ) {
- double d = 0; // set return into method-global scope
- // read until valid input given
- while (true) {
- System.out.println( prompt );
- try {
- d = scan.nextDouble();
- break;
- }
- catch( InputMismatchException e ) {
- String sink = scan.next();
- System.out.println(
- "invalid value: " + sink + "\n" +
- e.toString()
- );
- }
- }
- return d;
- }
- }
- -----------------
- Put these in the same package and run the QuadraticEquationRunner as a Java app.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement