Problem: A complex number is defined as z=a+i*b, where a is the real part, and b is the imaginary part. In other words, in order to define a complex number, we need the two floating numbers a and b.
Write methods that perform for each of the following operations with complex numbers z1 = a1 + i*b1, and z2 = a2 + i*b2:
Addition: z1 + z2=(a1+a2) + i*(b1+b2)
Subtraction: z1 - z2=(a1-a2) + i*(b1-b2)
Multiplication: z1*z2 = (a1*a2 – b1*b2) + i*(a1*b2 + b1*a2)
Division: z1/z2 = (a1*a2 +b1*b2)/(a2^2 + b2^2) + i*(b1*a2 – a1*b2)/(a2^2 + b2^2)
Create a test program that asks for the real and imaginary parts of two complex numbers from the user, and displays the results of the four operations, writing the formula as shown above, and replacing the a1, a2, b1 and b2 with the numbers entered by the user.
****
The professor used the incorrect complex number equations and has notified the students of his error. I have run into a few problems thus far.
1. I'm not sure how to use floating numbers with the Math.pow(double, double) function, since its requires doubles!? So instead of using floating numbers, I've knowingly switched them all to double in order to see if the code itself is working properly for the purposes of this forum. Is there a way that I can modify this function so that it will work for floating numbers?
2. Regarding the division method, an error stating that c and d have not been initialized and I'm not sure how to change it because the other calculation methods work fine. Why do I need to initialize c and d in the division method and not the others?
3. Am I on the right path? I have surfed the web to see how others completed the program and they all appear very different than mine...
4. As always feel free to offer an constructive criticism on how I can improve on the code. It's always important to me that I create an easy to read and streamlined program. I'm just beginning my journey into programming, so please be critical! Thank you.
****
package program5; import java.util.Scanner; public class Program5 { static double a, b, c, d; static double i = Math.pow(-1,1/2); public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" + Calculating Complex Numbers + "); System.out.printf("%15s\n","z = a + (i*b) and z = c + (i*d)"); System.out.print("Enter a: "); a = input.nextFloat(); System.out.print("Enter b: "); b = input.nextFloat(); System.out.print("Enter c: "); c = input.nextFloat(); System.out.print("Enter d: "); d = input.nextFloat(); addition("Addition: "); subtraction("Subtraction: "); multiplication("Multiplication: "); division("Division: "); } public static void addition(String name){ double add = (a + c) + i*(b + d); System.out.println(name + add); } public static void subtraction(String name){ double subtract = (a - c) + i*(b - d); System.out.println(name + subtract); } public static void multiplication(String name){ double multiply = ((a * c) - (b * d)) + i*((b * c) + (a * d)); System.out.println(name + multiply); } public static void division(String name){ double c = Math.pow(c,2); double d = Math.pow(d,2); double division = (((a * c) + (b * d)) / (c + d)) + i*((b * c)- (a * d)) / ((c + d)); System.out.println(name + division); } }