This repository was archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment No 1
157 lines (150 loc) · 3.58 KB
/
Assignment No 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Classes and object
Design a class ‘Complex’with data members for real and imaginary part.
Provide default and Parameterized constructors.
Write a program to perform arithmetic operations of two complex numbers. */
import java.io.*;
import java.util.Scanner;
class Complex
{
float real, imag;
Complex()
{
real=0;
imag=0;
}
Complex(float p1, float p2)
{
real=p1;
imag=p2;
}
public void add(Complex C1, Complex C2)
{
float r,i;
real = C1.real + C2.real;
imag = C1.imag + C2.imag;
}
public void sub(Complex C1, Complex C2)
{
float r,i;
real = C1.real - C2.real;
imag = C1.imag - C2.imag;
}
public void mul(Complex C1, Complex C2)
{
float r,i;
real = (C1.real *C2.real ) - (C1.imag * C2.imag);
imag = ( C1.real *C2.imag) + ( C1.imag * C2.real);
}
public void div(Complex C1, Complex C2)
{
real = (((C1.real*C2.real)+(C1.imag*C2.imag))/((C2.real*C2.real)+(C2.imag*C2.imag)));
imag =((( C1.real * C2.imag ) - ( C1.imag * C2.real )) / ((C2.real* C2.real) + (C2.imag * C2.imag)));
}
public void display()
{
System.out.println("Answer is: " + this.real + " + " + this.imag + "i" );
}
}
class JavaApplication1
{
public static void main(String args[])
{
float num1, num2;
int choice,cont;
Scanner input = new Scanner(System.in);
do
{
System.out.print("1.Addtion\n");
System.out.print("2.Subtraction\n");
System.out.print("3.Multiplication\n");
System.out.print("4.Division\n");
System.out.print("Enter your Choice\n");
choice = input.nextInt();
System.out.print("Enter the First no.\n");
num1 = input.nextInt();
num2 = input.nextInt();
Complex Obj1 = new Complex(num1,num2);
System.out.print("Enter the Second no.\n");
num1 = input.nextInt();
num2 = input.nextInt();
Complex Obj2 = new Complex(num1,num2);
Complex res = new Complex();
switch(choice)
{
case 1:
res.add(Obj1, Obj2);
res.display();
break;
case 2:
res.sub(Obj1,Obj2);
res.display();
break;
case 3:
res.mul(Obj1, Obj2);
res.display();
break;
case 4:
res.div(Obj1, Obj2);
res.display();
}
System.out.print("Do you want to continue Press 1 for yes: \n");
cont = input.nextInt();
}while(cont==1);
}
}
/* OUTPUT
run:
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter your Choice
1
Enter the First no.
10 20
Enter the Second no.
10 20
Answer is: 20.0 + 40.0i
Do you want to continue Press 1 for yes:
1
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter your Choice
2
Enter the First no.
20 10
Enter the Second no.
10 5
Answer is: 10.0 + 5.0i
Do you want to continue Press 1 for yes:
1
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter your Choice
3
Enter the First no.
2 5
Enter the Second no.
4 -3
Answer is: 23.0 + 14.0i
Do you want to continue Press 1 for yes:
1
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter your Choice
4
Enter the First no.
5 1
Enter the Second no.
0 2
Answer is: 0.5 + 2.5i
Do you want to continue Press 1 for yes:
2
BUILD SUCCESSFUL (total time: 2 minutes 21 seconds)
*/