Skip to content

Commit 1174acc

Browse files
Create 24.1 Multiplication Imaginary Number 2 Constructor 3 Object.java
1 parent 2717b9f commit 1174acc

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
My son Priyanshu got an assignment from his teacher to perform multiplication of complex number.
3+
Help him to complete his assignment. Write a java code with following features:
4+
class Name: Complex
5+
Instance Variable: real, imaginary (int type)
6+
Constructor: **
7+
1. Complex()
8+
2. Complex(int, int)
9+
10+
**Instance Method:
11+
1. Mul_Complex(Complex, Complex)
12+
2. Display()
13+
Read the real and imaginary from user and pass to the constructor as parameter
14+
Write a separate class Test_Complex with a main() method and test the Complex class methods
15+
16+
Input Format
17+
18+
First line Read the complex number 1 for real and imaginary First line Read the complex number 2 for real and imaginary
19+
20+
Constraints
21+
22+
real, imaginary > 1
23+
24+
Output Format
25+
26+
Print the Result in form of complex number : a+bi
27+
28+
Sample Input 0
29+
30+
2 3
31+
4 5
32+
Sample Output 0
33+
34+
-7+22i
35+
Sample Input 1
36+
37+
0 1
38+
2 3
39+
Sample Output 1
40+
41+
-3+2i
42+
Sample Input 2
43+
44+
2
45+
4 2
46+
Sample Output 2
47+
48+
8+4i
49+
*/
50+
51+
52+
import java.io.*;
53+
import java.util.*;
54+
import java.text.*;
55+
import java.math.*;
56+
import java.util.regex.*;
57+
class Complex
58+
{
59+
60+
int real,imaginary;
61+
Complex()
62+
{
63+
real = 0;
64+
imaginary = 0;
65+
}
66+
Complex(int x,int y)
67+
{
68+
real = x;
69+
imaginary = y;
70+
}
71+
void mul_Complex(Complex c1,Complex c2)
72+
{
73+
//(a+bi)*(c+di) = (ac - bd) +i(ad + bc)
74+
this.real = (c1.real*c2.real) - (c1.imaginary*c2.imaginary);
75+
this.imaginary = (c1.real*c2.imaginary) + (c1.imaginary*c2.real);
76+
}
77+
void display()
78+
{
79+
System.out.println(real+"+"+imaginary+"i");
80+
}
81+
}
82+
public class Solution {
83+
84+
public static void main(String[] args) {
85+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
86+
Scanner sc = new Scanner(System.in);
87+
Complex c1,c2,c3;
88+
String []s1 = sc.nextLine().replaceAll("\\s+$","").split(" ");
89+
String []s2 = sc.nextLine().replaceAll("\\s+$","").split(" ");
90+
91+
int r1 = Integer.parseInt(s1[0]);
92+
int r2 = Integer.parseInt(s2[0]);
93+
int i1,i2;
94+
if(s1.length==1)
95+
i1 = 0;
96+
else
97+
i1 = Integer.parseInt(s1[1]);
98+
99+
if(s2.length==1)
100+
i2 = 0;
101+
else
102+
i2 = Integer.parseInt(s2[1]);
103+
c1 = new Complex(r1,i1);
104+
c2 = new Complex(r2,i2);
105+
c3 = new Complex();
106+
107+
108+
c3.mul_Complex(c1,c2);
109+
c3.display();
110+
}
111+
}

0 commit comments

Comments
 (0)