Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write Java Program. 1) Recall that a complex number is defined as number of the

ID: 3680303 • Letter: W

Question

Write Java Program.

1) Recall that a complex number is defined as number of the form

z= a+bi

where a is called the real part of z,b is called the imaginarypart of Z, and i = sqrt(-1) is called the imaginary unit. write a calss called complexNumber that consists of at least two instace fields real and imag which are of type double and represent the real and imaginary parts of a imaginary parts of a cmplex number. Your class should also contain the following methods:

1. A parameterless constructor that creates the complex number 0 (with real and imaginary parts both being zero).

2. A constructor that takes two arguments: a real part and an imaginary part. The constructor should create a new complex number whose real and imaginary parts are that which are specified in the arguments.

3. A non-static method called add which accepts a complexNumber reference and returns the sum of the calling object and the object referenced by the parameter. The sum should be returned as a new ComplexNumber object. Hint: The sum of two complext numbers z1=a+bi and z2=c+di is:

z1+z2=(a+c)+(b+d)i.

4. A static method called mult which returns the product of two ComplexNumber objects passed as two arguments to the method. The product should be returned as a new ComplexNumber Object. Hint The product of two complext numbers z1=a+bi and z2 = c+di is

z1*z2 = (ac-bd)+(ad+bc)i

Explanation / Answer

import java.io.*; class complex { float a,b; void init(float x,float y) { a=x; b=y; } void complex_addition(complex c1,complex c2) { a=c1.a+c2.a; b=c1.b+c2.b; } static complex complex_subtraction(complex c1,complex c2) { complex c=new complex(); c.a=c1.a-c2.a; c.b=c1.b-c2.b; return c; } static complex complex_multiplication(complex c1,complex c2) { complex c=new complex(); c.a=((c1.a)*(c2.a))-((c1.b)*(c2.b)); c.b=((c1.a)*(c2.b))+((c1.b)*(c2.a)); return c; } static complex complex_division(complex c1,complex c2) { complex c=new complex(); c.a=(((c1.a)*(c2.a))+((c1.b)*(c2.b)))/(((c2.a)*(c2.a))-((c2.b)*(c2.b))); c.b=(-((c1.a)*(c2.b))+((c1.b)*(c2.a)))/(((c2.a)*(c2.a))-((c2.b)*(c2.b))); return c; } } class complexmain { public static void main(String args[]) throws IOException { while(true) { System.out.println("enter 1 for complex addition"); System.out.println("enter 2 for complex subtraction"); System.out.println("enter 3 for complex multiplication"); System.out.println("enter 4 for complex division"); System.out.println("enter 5 for exit"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i; i=Integer.parseInt(br.readLine()); if(i==5) System.exit(1); complex c1=new complex(); float a,b; System.out.println("enter the first complex number"); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c1.init(a,b); System.out.println("enter the second complex number"); complex c2=new complex(); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c2.init(a,b); complex c3=new complex(); switch(i) { case 1: /* complex c1=new complex(); int a,b; System.out.println("enter the first complex "); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c1.init(a,b); System.out.println("enter the second complex number") complex c2=new complex(); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c1.init(a,b); complex c3=new complex(); */ c3.complex_addition(c1,c2); System.out.println("the required complex number:"+c3.a+" "+c3.b+"i"); break; case 2: c3=complex.complex_subtraction(c1,c2); System.out.println("the required complex number:"+c3.a+" "+c3.b+"i"); break; case 3: c3=complex.complex_multiplication(c1,c2); System.out.println("the required complex number:"+c3.a+" "+c3.b+"i"); break; case 4: c3=complex.complex_division(c1,c2); System.out.println("the required complex number:"+c3.a+" "+c3.b+"i"); break; } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote