-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
294 lines (254 loc) · 9.18 KB
/
Copy pathComplex.java
File metadata and controls
294 lines (254 loc) · 9.18 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* PROJECT II: Complex.java
*
* This file contains a template for the class Matrix. Not all methods are
* implemented. Make sure you have carefully read the project formulation
* before starting to work on this file.
*
* This class implements a new type for complex numbers and corresponding
* arithmetic operations. You should already have done something very similar
* in week 14 (see CmplxNum in the online lab notes), so you should be able to
* simply copy and paste from that file into this one.
*
* At the bottom of this file, I have included a simple main function which
* tests the basic functionality. This is the same code that was released in
* the week 14 solutions.
*
* Remember not to change the names, parameters or return types of any
* variables in this file!
*
* The function of the methods and instance variables are outlined in the
* comments directly above them.
*
* Tasks:
*
* 1) Complete this class with the indicated methods and instance variables.
*
* 2) Fill in the following fields:
*
* NAME: Archie Bevan
* UNIVERSITY ID: 5538165
* DEPARTMENT: Maths and stats
*/
public class Complex {
/**
* Real part x of the complex number x+iy.
*/
private double x;
/**
* Imaginary part y of the complex number x+iy.
*/
private double y;
// ========================================================
// Constructor functions.
// ========================================================
/**
* Constructor: Initializes x, y.
*
* @param x The initial value of the real component.
* @param y The initial value of the imaginary component.
*/
public Complex(double x, double y) {
// You need to fill in this method.
this.x = x;
// 'this' refers to the current object and lets us tell what is instance variable and what is parameter
this.y = y;
}
/**
* Real constructor - initialises with a real number.
*
* @param x The initial real number to initialise to.
*/
public Complex(double x) {
// You need to fill in this method.
this.x = x;
this.y = 0;
}
/**
* Default constructor; initialise x and y to zero.
*/
public Complex() {
// You need to fill in this method.
this.x = 0;
this.y = 0;
}
// ========================================================
// Accessor and mutator methods.
// ========================================================
/**
* Accessor Method: get real part of the complex number.
*
* @return The real part of the complex number.
*/
public double getReal() {
// You need to fill in this method with the correct code.
return this.x;
}
/**
* Accessor Method: get imaginary part of the complex number.
*
* @return The imaginary part of the complex number
*/
public double getImag() {
// You need to fill in this method with the correct code.
return this.y;
}
/**
* Mutator method: set the real part of the complex number.
*
* @param x The replacement real part of z.
*/
public void setReal(double x) {
// You need to fill in this method.
this.x = x;
}
/**
* Mutator method: set the imaginary part of the complex number.
*
* @param y The replacement imaginary part of z.
*/
public void setImag(double y) {
// You need to fill in this method.
this.y = y;
}
// ========================================================
// Operations and functions with complex numbers.
// ========================================================
/**
* Converts the complex number to a string. This is an important method as
* it allows us to print complex numbers using System.out.println.
*
* @return A string describing the complex number.
*/
public String toString() {
// This function is complete.
return String.format("%.3f%s%.3fi" , x , (y < 0.0 ? "-" : "+") , Math.abs(y));
}
/**
* Computes square of the absolute value (magnitude) of the complex number
* (i.e. |z|^2).
*
* @return The square of the absolute value of this complex number.
*/
public double abs2() {
// You need to fill in this method with the correct code.
return Math.pow(x,2) + Math.pow(y,2);
}
/**
* Computes absolute value (magnitude) of the complex number.
*
* @return The absolute value of the complex number.
*/
public double abs() {
// You need to fill in this method with the correct code.
return Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
}
/**
* Calculates the conjugate of this complex number.
*
* @return A Complex containing the conjugate.
*/
public Complex conjugate() {
// You need to fill in this method with the correct code.
return new Complex(x,-y);
}
/**
* Adds a complex number to this one.
*
* @param b The complex number to add to this one.
* @return The sum of this complex number with b.
*/
public Complex add(Complex b) {
// You need to fill in this method with the correct code.
return new Complex(this.x + b.getReal(),this.y + b.getImag());
}
/**
* Calculates -z.
*
* @return The complex number -z = -x-iy
*/
public Complex negate() {
// You need to fill in this method with the correct code.
return new Complex(-this.x,-this.y);
}
/**
* Multiplies this complex number by a constant.
*
* @param alpha The constant to multiply by.
* @return The product of alpha with z.
*/
public Complex multiply(double alpha) {
// You need to fill in this method with the correct code.
return new Complex(this.x * alpha , this.y * alpha);
}
/**
* Multiplies this complex number by another complex number.
*
* @param b The complex number to multiply by.
* @return The product of b with z.
*/
public Complex multiply(Complex b) {
// You need to fill in this method with the correct code.
return new Complex(this.x * b.getReal() - this.y * b.getImag(), this.x * b.getImag() + b.getReal() * this.y);
}
/**
* Divide this complex number by another.
*
* @param b The complex number to divide by.
* @return The division z/a.
*/
public Complex divide(Complex b) {
// You need to fill in this method with the correct code.
double divisor = b.abs2();
double divisionReal = (this.x * b.getReal() + this.y * b.getImag())/divisor;
double divisionImag = (this.y * b.getReal() - this.x * b.getImag())/divisor;
return new Complex(divisionReal, divisionImag);
}
// ========================================================
// Tester function.
// ========================================================
public static void main(String[] args) {
// Test all of the constructor functions. You can add anything you
// want to this if you think it's necessary!
Complex A = new Complex(2.0, 2.0);
Complex B = new Complex(1.0);
Complex C = new Complex();
// Test the converters by printing out A, B and C.
System.out.println("Constructor test:");
System.out.println("A = "+A.toString());
System.out.println("B = "+B.toString());
System.out.println("C = "+C.toString());
// Test accessor/mutators.
System.out.println();
System.out.println("Setting imag(C) = real(C) + 1:");
C.setImag(C.getReal() + 1.0);
System.out.println("C = "+C.toString());
// Now test operations for both complex and real numbers, just to make
// sure something isn't wrong.
System.out.println();
System.out.println("Testing operators:");
System.out.println("abs(A) = "+A.abs());
System.out.println("abs2(A) = "+A.abs2());
System.out.println("abs(B) = "+B.abs());
System.out.println("abs2(B) = "+B.abs2());
System.out.println("conj(A) = "+A.conjugate());
System.out.println("conj(B) = "+B.conjugate());
System.out.println("neg(A) = "+A.negate());
System.out.println("neg(B) = "+B.negate());
System.out.println("A+B = "+A.add(B));
System.out.println("A+C = "+A.add(C));
System.out.println("2*A = "+A.multiply(2.0));
System.out.println("A*C = "+A.multiply(C));
System.out.println("B*C = "+B.multiply(C));
System.out.println("A*A = "+A.multiply(A));
System.out.println("A/B = "+A.divide(B));
System.out.println("A/C = "+A.divide(C));
System.out.println("A/A = "+A.divide(A));
// And to finish off, a couple of examples of how you can chain the
// different operations together.
System.out.println();
System.out.println("Chained operators:");
System.out.println("B*(A+C) = "+B.multiply(A.add(C)));
System.out.println("-A+B*C = "+A.negate().add(B.multiply(C)));
}
}