Pages

Friday, 23 December 2016

Chapter 13 Exercise 20, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

13.20 (Algebra: solve quadratic equations)
Rewrite Programming Exercise 3.1 to obtain imaginary roots
if the determinant is less than 0 using the Complex class
in Programming Exercise 13.17.

public class QuadraticEquation {

    private double a;
    private double b;
    private double c;

    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getDiscriminant() {
         return b * b - 4.0 * a * c;
    }

    public double getRoot1() {
            return  (-b + Math.pow(getDiscriminant(), 0.5)) / (2.0 * a);
    }

    public double getRoot2() {
            return  (-b - Math.pow(getDiscriminant(), 0.5)) / (2.0 * a);
    }

}

import java.text.DecimalFormat;
public class Complex {

    // real numbers
    private double a;
    private double b;


    public Complex(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public Complex(double a) {
        this(a, 0);
    }

    public Complex() {
        this(0);
    }

    public Complex add(Complex complex) {
        // a + bi + c + di = (a + c) + (b + d)i
        return new Complex((a + complex.a), (b + complex.b));
    }
    public Complex subtract(Complex complex) {
        // a + bi - (c + di) = (a - c) + (b - d)i

        return new Complex((a - complex.a), (b - complex.b));

    }
    public Complex multiply(Complex complex) {
        // (a + bi)*(c + di) = (ac - bd) + (bc + ad)i

        return new Complex((a * complex.a - b * complex.b), (b * complex.a + a * complex.b));

    }
    public Complex divide(Complex complex) {
        // (a+bi)/(c+di)=(ac+bd)/(c^2 +d^2)+(bc-ad)i/(c^2 +d^2)

        return new Complex( (a*complex.a+b*complex.b) / ((Math.pow(complex.a, 2) + Math.pow(complex.b, 2))),
                            (b * complex.a - a * complex.b) / ((Math.pow(complex.a, 2) + Math.pow(complex.b, 2))));
    }

    public double abs() {
        return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    }

    public double getRealPart() {
        return a;
    }

    public double getImaginaryPart(){
        return b;
    }

    @Override
    public String toString() {

        if (b == 0) return a + "";

        DecimalFormat decimal = new DecimalFormat("#.###");
        return  "(" + decimal.format(a) + " + " + decimal.format(b) + "i)";

    }

}

import java.util.Scanner;
public class Exercise_20 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a, b, c: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();

        QuadraticEquation equation = new QuadraticEquation(a, b, c);
        double discriminant = equation.getDiscriminant();
        if (discriminant > 0) {
            double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
            double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
            System.out.println("The roots are " + r1 + " and " + r2);
        }else if (discriminant == 0) {
            Complex r1 = new Complex(-b / (2 * a));
            System.out.println("The root is " + r1);
        } else {
            System.out.println("The equation has no real roots.");
            Complex r1 = new Complex(-b / (2 * a), Math.pow(-discriminant, 0.5) / (2 * a));
            Complex r2 = new Complex(-b / (2 * a), -Math.pow(-discriminant, 0.5) / (2 * a));
            System.out.println("The roots are " + r1 + " and " + r2);
        }

    }

}

No comments :

Post a Comment