Knowee
Questions
Features
Study Tools

Write a Java program to find the addition, subtraction, multiplication, and division of two complex numbers using inheritance. Create an abstract class called Complex. From this extends the child classes Addition, Subtraction, Multiplication, and Division. All of these child classes should contain the same methods.Input format :The input consists of four double-type variables, which denote the real and imaginary parts of the first and second complex numbers, respectively.Output format :The output consists of the addition, subtraction, multiplication, and division of the two complex numbers.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :2 64 2Output 1 :Addition: 6.0000 +8.0000 iSubtraction: -2.0000 +4.0000 iMultiplication: -4.0000 +28.0000 iDivision: 1.0000 +1.0000 i

Question

Write a Java program to find the addition, subtraction, multiplication, and division of two complex numbers using inheritance. Create an abstract class called Complex. From this extends the child classes Addition, Subtraction, Multiplication, and Division. All of these child classes should contain the same methods.Input format :The input consists of four double-type variables, which denote the real and imaginary parts of the first and second complex numbers, respectively.Output format :The output consists of the addition, subtraction, multiplication, and division of the two complex numbers.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :2 64 2Output 1 :Addition: 6.0000 +8.0000 iSubtraction: -2.0000 +4.0000 iMultiplication: -4.0000 +28.0000 iDivision: 1.0000 +1.0000 i

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java program that performs the operations as per your requirements:

abstract class Complex {
    double real1, real2, imag1, imag2;
    Complex(double r1, double i1, double r2, double i2) {
        real1 = r1;
        imag1 = i1;
        real2 = r2;
        imag2 = i2;
    }
    abstract void add();
    abstract void subtract();
    abstract void multiply();
    abstract void divide();
}

class Addition extends Complex {
    Addition(double r1, double i1, double r2, double i2) {
        super(r1, i1, r2, i2);
    }
    void add() {
        System.out.printf("Addition: %.4f + %.4f i\n", real1 + real2, imag1 + imag2);
    }
    void subtract() {}
    void multiply() {}
    void divide() {}
}

class Subtraction extends Complex {
    Subtraction(double r1, double i1, double r2, double i2) {
        super(r1, i1, r2, i2);
    }
    void subtract() {
        System.out.printf("Subtraction: %.4f + %.4f i\n", real1 - real2, imag1 - imag2);
    }
    void add() {}
    void multiply() {}
    void divide() {}
}

class Multiplication extends Complex {
    Multiplication(double r1, double i1, double r2, double i2) {
        super(r1, i1, r2, i2);
    }
    void multiply() {
        System.out.printf("Multiplication: %.4f + %.4f i\n", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);
    }
    void add() {}
    void subtract() {}
    void divide() {}
}

class Division extends Complex {
    Division(double r1, double i1, double r2, double i2) {
        super(r1, i1, r2, i2);
    }
    void divide() {
        double denom = real2 * real2 + imag2 * imag2;
        System.out.printf("Division: %.4f + %.4f i\n", (real1 * real2 + imag1 * imag2) / denom, (imag1 * real2 - real1 * imag2) / denom);
    }
    void add() {}
    void subtract() {}
    void multiply() {}
}

public class Main {
    public static void main(String[] args) {
        double r1 = 2, i1 = 6, r2 = 4, i2 = 2;
        new Addition(r1, i1, r2, i2).add();
        new Subtraction(r1, i1, r2, i2).subtract();
        new Multiplication(r1, i1, r2, i2).multiply();
        new Division(r1, i1, r2, i2).divide();
    }
}

This program creates an abstract class Complex with four instance variables representing the real and imaginary parts of two complex numbers. Four child classes Addition, Subtraction, Multiplication, and Division extend Complex and implement the abstract methods add(), subtract(), multiply(), and divide(), respectively. In the main() method, instances of these classes are created and the corresponding methods are called to perform the operations.

This problem has been solved

Similar Questions

Create a Complex class to represent complex numbers. Overload the addition (+), subtraction (-), multiplication (*), and division (/) operators for complex numbers. The prompt ask 2 complex number from the user and display the respective output for the user input.

Write a class for addition of complex numbers. The program should ask for real and imaginary part of complex numbers and using function it should add the real part to real part of the number and imaginary part to imaginary part of the number.

operations on complex numbers Write a program to find out the addition and multiplication of two complex numbers using structures and functions. Note: Write the code in the functions readC(), display(), add() and product().

Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.

Write a program to overload arithmetic minus operator for subtracting two complex numbers (use constructors also).

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.