Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
ComplexTest Prog 02 Lec 3 Ex 2 Formative
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jordan.dalby
ComplexTest Prog 02 Lec 3 Ex 2 Formative
Commits
852abf43
Commit
852abf43
authored
Feb 10, 2020
by
jordan.dalby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ Added comments to code
parent
9eef9328
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
22 deletions
+23
-22
Complex.java
src/com/jordan/Complex.java
+23
-22
No files found.
src/com/jordan/Complex.java
View file @
852abf43
...
...
@@ -3,71 +3,72 @@ package com.jordan;
public
class
Complex
{
private
double
realPart
;
private
double
imaginaryPart
;
private
double
realPart
_
;
private
double
imaginaryPart
_
;
public
Complex
(
double
realPart
,
double
imaginaryPart
)
{
this
.
realPart
=
realPart
;
this
.
imaginaryPart
=
imaginaryPart
;
this
.
realPart
_
=
realPart
;
// set the real
this
.
imaginaryPart
_
=
imaginaryPart
;
}
public
Complex
()
{
this
(
0
,
0
);
this
(
0
,
0
);
// initialize this class with the other constructor
}
public
double
real
()
{
return
this
.
realPart
;
return
this
.
realPart
_
;
// return the realPart_ variable
}
public
double
imag
()
{
return
this
.
imaginaryPart
;
return
this
.
imaginaryPart
_
;
// return the imaginaryPart_ variable
}
public
double
magnitude
()
{
double
val
=
Math
.
sqrt
(
(
Math
.
pow
(
realPart
,
2
)
+
Math
.
pow
(
imaginaryPart
,
2
)
)
);
double
val
=
Math
.
sqrt
(
(
Math
.
pow
(
this
.
realPart_
,
2
)
+
Math
.
pow
(
this
.
imaginaryPart_
,
2
)
)
);
// calculate the magnitude of the Complex numbers
return
val
;
}
public
double
argument
()
{
double
val
=
Math
.
atan2
(
this
.
imaginaryPart
,
this
.
realPart
);
double
val
=
Math
.
atan2
(
this
.
imaginaryPart
_
,
this
.
realPart_
);
// calculate the argument of the Complex numbers
return
val
;
}
public
Complex
add
(
Complex
b
)
{
double
real
=
this
.
realPart
+
b
.
real
();
double
imag
=
this
.
imaginaryPart
+
b
.
imag
();
double
real
=
this
.
realPart
_
+
b
.
real
();
// get the sum of the real numbers
double
imag
=
this
.
imaginaryPart
_
+
b
.
imag
();
// get the sum of the imaginary numbers
Complex
c
=
new
Complex
(
real
,
imag
);
return
c
;
Complex
c
=
new
Complex
(
real
,
imag
);
// create a new instance of the Complex class to return
return
c
;
// return the class
}
@Override
public
String
toString
()
{
String
s
=
""
;
if
(
realPart
!=
0
)
s
+=
String
.
valueOf
(
this
.
realPart
);
if
(
realPart
!=
0
&&
imaginaryPart
!=
0
)
if
(
this
.
realPart_
!=
0
)
// if there is a real part
s
+=
String
.
valueOf
(
this
.
realPart_
);
// append the real part to the string
if
(
this
.
realPart_
!=
0
&&
this
.
imaginaryPart_
!=
0
)
// if there is a real part and an imaginary part
{
if
(
imaginaryPart
<
0
)
s
+=
" - "
;
if
(
this
.
imaginaryPart_
<
0
)
// if the imaginary part is less than 0
s
+=
" - "
;
// append a minus
else
s
+=
" + "
;
s
+=
" + "
;
// append a plus
}
if
(
imaginaryPart
!=
0
)
if
(
this
.
imaginaryPart_
!=
0
)
// if the imaginary part exists
{
s
+=
"j"
+
String
.
valueOf
(
Math
.
abs
(
this
.
imaginaryPart
));
s
+=
"j"
+
String
.
valueOf
(
Math
.
abs
(
this
.
imaginaryPart
_
));
// append the imaginary part with a j to the string
}
return
s
;
return
s
;
// return the string
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment