Problem Statement:
Create a class called Complex for performing arithmetic on complex numbers. Complex numbers have the form
a+bi where a is real part and b is imaginary part and i=√-1.
Write a program to test your class. Use floating point variables to represent the private data of the class. Provide constructor that enable an object to be initialized when it is declared. Provide no argument constructor with default values in case no initializers are provided. Provide public methods for addition, subtraction, multiplication and division of complex numbers. Pass objects of Complex as parameters of the method.
Input and Output Requirements:
Program reads real and imaginary parts of two complex numbers through keyboard and displays their sum, difference, product and quotient as result.
Algorithm:
Begin
Define a class operations with instance variables real and imag
Input the two complex numbers c1=(a+ib) and c2=(c+id)
Define the method add(c1,c2) as (a+ib)+(c+id) and stores result in c3
Define the method sub(c1,c2) as (a+ib)-(c+id) and stores result in c3
Define the method mul(c1,c2) as (a+ib)*(c+id) and store the result in c3 as (ac-bd)+i(bc+ad)
Define the method div(c1,c2) as (a+ib)/(c+id) and stores the quotient c3 as
{(ac+bd)/(c2+d2)}+i{(bc-ad)/(c2+d2)}
Define the method display() which outputs each result
End
Program:
import java.io.*; import java.util.*; public class Complex { public static void main(String args[]) { int ch=0; float num1,num2,answer; Complex_Op cal = new Complex_Op() ; Scanner input = new Scanner(System.in); System.out.print("Enter the first Number\n"); System.out.print("Real Part:"); num1 = input.nextInt(); System.out.print("Imaginary Part:"); num2 = input.nextInt(); Complex_Op Object1 = new Complex_Op(num1,num2); System.out.print("Enter the Second Number\n"); System.out.print("Real Part:"); num1 = input.nextInt(); System.out.print("Imaginary Part:"); num2 = input.nextInt(); Complex_Op Object2 = new Complex_Op(num1,num2); do { System.out.println("1.Add"); System.out.println("2.Subtract"); System.out.println("3.Multiplication"); System.out.println("4.Division"); System.out.println("5.Exit"); System.out.print("Choose ur choice:"); ch = input.nextInt(); switch(ch) { case 1: cal.AddNumbers(Object1,Object2); break; case 2: cal.SubtractNumbers(Object1,Object2); break; case 3: cal.MultiplyNumbers(Object1,Object2); break; case 4: cal.DivideNumbers(Object1,Object2); break; case 5: break; } }while(ch!=5); } } class Complex_Op { private float real,imag; Complex_Op() { real=0; imag=0; } Complex_Op(float Comp1,float Comp2) { real=Comp1; imag=Comp2; } void AddNumbers(Complex_Op C1,Complex_Op C2) { float real,imag; this.real = (C1.real + C2.real); this.imag = (C1.imag + C2.imag); System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" ); } void SubtractNumbers(Complex_Op C1,Complex_Op C2) { float real,imag; this.real = (C1.real - C2.real); this.imag = (C1.imag - C2.imag); System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" ); } void MultiplyNumbers(Complex_Op C1,Complex_Op C2) { float real,imag; this.real = (C1.real*C2.real - C1.imag*C2.imag); this.imag = (C1.real*C2.imag + C2.real*C1.imag); System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" ); } void DivideNumbers(Complex_Op C1,Complex_Op C2) { float real,imag,deno; deno = (C2.real*C2.real + C2.imag*C2.imag); this.real = (C1.real*C2.real + C1.imag*C2.imag)/deno; this.imag = (C2.real*C1.imag - C1.real*C2.imag)/deno; System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" ); } } |
Output:
Enter the first Number
Real Part:1
Imaginary Part:2
Enter the Second Number
Real Part:3
Imaginary Part:4
1.Add
2.Subtract
3.Multiplication
4.Division
5.Exit
Choose ur choice:1
Answer is:(4.0) + (6.0)i
1.Add
2.Subtract
3.Multiplication
4.Division
5.Exit
Choose ur choice:2
Answer is:(-2.0) + (-2.0)i
1.Add
2.Subtract
3.Multiplication
4.Division
5.Exit
Choose ur choice:3
Answer is:(-5.0) + (10.0)i
1.Add
2.Subtract
3.Multiplication
4.Division
5.Exit
Choose ur choice:4
Answer is:(0.44) + (0.08)i
1.Add
2.Subtract
3.Multiplication
4.Division
5.Exit
Choose ur choice:5
Leave a Reply