Q:

(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. Here are some sample runs

0

(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. Here are some sample runs.

Output:

Enter a, b, c: 1 3 1
The roots are -0.381966 and -2.61803

Enter a, b, c: 1 2 1
The root is -1

Enter a, b, c: 1 2 3
The roots are -1.0 + 1.4142i and -1.0 + -1.4142i

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (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.                                           *
*********************************************************************************/
import java.util.Scanner;

public class Exercise_13_20 {
	public static void main(String[] args) {
		// Create a Scanner object
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter values for a, b and c.
		System.out.print("Enter a, b, c: ");
		double a = input.nextDouble();
		double b = input.nextDouble();
		double c = input.nextDouble();

		// Compute the discriminant of the quadriatic equation.
		double discriminant = Math.pow(b, 2) - 4 * a * c;

		// Compute the real roots of the quadriatic equation if any.
		if (discriminant > 0)
		{
			double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);  
			double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);  
			System.out.println("The roots are " + root1 + " and " + root2);
		}
		else if (discriminant == 0)
		{
			System.out.println("The root is " + 
				(-b + Math.sqrt(discriminant)) / (2 * a));
		}
		else { // Display imaginary roots by using two Complex objects.
			System.out.print("The roots are "); 
			Complex root1 = new Complex(-b / (2 * a), Math.sqrt(2 * a));
			Complex root2 = new Complex(-b / (2 * a), -Math.sqrt(2 * a));
			System.out.println(root1 + " and " + root2);
		}
	}
}
public class Complex implements Cloneable {
	private double a;
	private double b;

	// Constructors
	/** Creates a complex object for number 0 */
	public Complex() {
		this(0, 0);
	}

	/** Create a complex object with 0 for b */
	public Complex(double a) {
		this(a, 0);
	}

	/** Creates a complex object with specified a and b */
	public Complex(double a, double b) {
		this.a = a;
		this.b = b;
	}

	// Methods
	/** Return real part of complex number */
	public double getRealPart() {
		return a;
	}

	/** Return imaginary part of complex number */
	public double getImaginaryPart() {
		return b;
	}

	/** Add a complex number to this complex number */
	public Complex add(Complex secondComplex) {
		return new Complex(a + secondComplex.a, 
			b + secondComplex.b); 
	}

	/** Subtract a complex number from this complex number */
	public Complex subtract(Complex secondComplex) {
		return new Complex(a - secondComplex.a,
			b - secondComplex.b);
	}

	/** Multiply a complex number by this complex number */
	public Complex multiply(Complex secondComplex) {
		return new Complex(a * secondComplex.a - b * secondComplex.b,
			b * secondComplex.a + a * secondComplex.b);
	}

	/** Divide a complex number by this complex number */
	public Complex divide(Complex secondComplex) {
		return new Complex((a * secondComplex.a + b * secondComplex.b) /
			(Math.pow(secondComplex.a, 2) + Math.pow(secondComplex.b, 2)),
			(b * secondComplex.a - a * secondComplex.b) /
			(Math.pow(secondComplex.a, 2) +  Math.pow(secondComplex.b, 2)));
	}

	/** Returns the absolute value of this complex number */
	public double abs() {
		return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
	}

	@Override /** Override the protectec clone method defined in
		the Object class, and strengthen its accexxibility */
	public Complex clone() throws CloneNotSupportedException {
		return (Complex)super.clone();
	}

	@Override /** Retrun a string description 
		of this complex number */
	public String toString() {
		return b == 0 ? a + "" : "(" + a + " + " + b + "i)";
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now