Question: Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form IN JAVA CODE realPart + imaginaryPart * i where
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form IN JAVA CODE
realPart + imaginaryPart * i
where i is sqrt(-1)
A program has been provided for you to test your class. Use floating-point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:
Add two Complex numbers: The real parts are added together and the imaginary parts are added together. Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. Print Complex numbers in the form (realPart, imaginaryPart).
the code provided
import java.util.Scanner; // Test the Complex number class public class ComplexTest { public static void main(String[] args) { // use Scanner object to take input from users Scanner input = new Scanner(System.in); System.out.println("Enter the real part of the first number:"); double real = input.nextDouble(); System.out.println("Enter the imaginary part of the first number:"); double imaginary = input.nextDouble(); Complex a = new Complex(real, imaginary); System.out.println("Enter the real part of the second number:"); real = input.nextDouble(); System.out.println("Enter the imaginary part of the second number:"); imaginary = input.nextDouble(); Complex b = new Complex(real, imaginary); System.out.printf("a = %s%n", a.toString()); System.out.printf("b = %s%n", b.toString()); System.out.printf("a + b = %s%n", a.add(b).toString()); System.out.printf("a - b = %s%n", a.subtract(b).toString()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
