Java Programming Problem 5 A complex number is defined as z=a+i*b, where a is th
ID: 3662043 • Letter: J
Question
Java Programming Problem 5
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.
Explanation / Answer
import java.util.*;
public class ComplexTester
{
int real1;
int real2;
int img1;
int img2;
int realsum;
int imgsum;
int realminus;
int imgminus;
int realmult;
int imgmult;
int imgrealmult;
int realimgmult;
int squaresum;
double multiply;
double divide;
public void calculation()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter real part of first complex number");
real1=scanner.nextInt();
System.out.println("Enter img part of first complex number");
img1=scanner.nextInt();
System.out.println("Enter real part of second complex number");
real2=scanner.nextInt();
System.out.println("Enter img part of second complex number");
img2=scanner.nextInt();
if(img1>=0)
{
System.out.println("First complex number="+real1+"+"+img1+"i");
}
if(img1<0)
{
System.out.println("First complex number="+real1+""+img1+"i");
}
if(img2>=0)
{
System.out.println("First complex number="+real2+"+"+img2+"i");
}
if(img2<0)
{
System.out.println("First complex number="+real2+""+img2+"i");
}
realsum=real1+real2;
imgsum=img1+img2;
realminus=real1-real2;
imgminus=img1-img2;
realmult=real1*real2;
imgmult=img1*img2;
imgrealmult=img1*real2;
realimgmult=real1*img2;
squaresum=(real2*real2)+(img2*img2);
int multiplysol1=realmult-imgmult;
int multiplysol2=realimgmult+imgrealmult;
int dividesol1=realmult+imgmult;
int dividesol2=dividesol1/squaresum;
int dividesol3=imgrealmult-realimgmult;
int dividesol4=dividesol3/squaresum;
System.out.println(squaresum);
System.out.println(realminus);
if(imgsum>=0)
{
System.out.println("Addition of two complex numbers you entered="+realsum+"+"+imgsum+"i");
}
if(imgsum<0)
{
System.out.println("Addition of two complex numbers you entered="+realsum+""+imgsum+"i");
}
if(imgminus>=0)
{
System.out.println("Subtraction of two complex numbers you entered="+realminus+"+"+imgminus+"i");
}
if(imgminus<0)
{
System.out.println("Subtraction of two complex numbers you entered="+realminus+""+imgminus+"i");
}
if(multiplysol2>=0)
{
System.out.println("Multiplication of two complex numbers you entered="+multiplysol1+"+"+multiplysol2+"i");
}
if(multiplysol2<0)
{
System.out.println("Multiplication of two complex numbers you entered="+multiplysol1+""+multiplysol1+"i");
}
if(dividesol4>=0)
{
System.out.println("Division of two complex numbers you entered="+dividesol2+"+"+dividesol4+"i");
}
if(dividesol4<0)
{
System.out.println("Division of two complex numbers you entered="+dividesol2+""+dividesol4+"i");
}
}
public static void main(String args[])
{
System.out.println("PLese enter the inputs");
ComplexTester obj=new ComplexTester();
obj.calculation();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.