Thursday 22 December 2016

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

13.17 (Math: The Complex class)
A complex number is a number in the form a + bi, where a and b are real
numbers and i is 2-1. The numbers a and b are known as the real part and
imaginary part of the complex number, respectively. You can perform addition,
subtraction, multiplication, and division for complex numbers using the following

(A complex number can be interpreted as a point on a plane by identifying the (a,b)
values as the coordinates of the point. The absolute value of the complex number corresponds
to the distance of the point to the origin, as shown in Figure 13.10b.)
Design a class named Complex for representing complex numbers and the methods add, subtract,
multiply, divide, and abs for performing complex- number operations, and override toString
method for returning a string repre- sentation for a complex number. The toString method returns (a + bi) as a string. If b is 0, it simply returns a. Your Complex class should also implement the Cloneable interface.
Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex() creates a
Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. Also
provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary
part of the complex number, respectively.
Write a test program that prompts the user to enter two complex numbers and displays the
result of their addition, subtraction, multiplication, division, and absolute value.

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)";

    }

}

public class Exercise_17 {

    public static void main(String[] args) {

        System.out.println(new Complex(3.5, 5.5).add(new Complex(-3.5, 1)));
        System.out.println(new Complex(3.5, 5.5).subtract(new Complex(-3.5, 1)));

        // This output is correct... http://www.mathsisfun.com/numbers/complex-number-calculator.html
        System.out.println(new Complex(3.5, 5.5).multiply(new Complex(-3.5, 1)));
        System.out.println(new Complex(3.5, 5.5).divide(new Complex(-3.5, 1)));
    }
}

1 comment :

  1. Say you wanted to find the absolute value of two numbers and print the result with four decimal places (ಢ⊱ಢ 。)..how would you go about changing the add method to show more decimal places?

    ReplyDelete