Exception
types, ComplexDivideByZeroException
and InvalidComplexException
.Complex
number class, complete with a set of methods for mathematical operations.
3+2i
is an imaginary number, as is something like -2+i
or 2.5-6i
. As with ordinary real numbers, complex numbers can be added, subtracted, multiplied and divided. Adding and subtracting is straightforward: you only need to add the corresponding components (real to real and imaginary to imaginary). So (2+4i) + (1-2i) = (3+2i)
, and (7-2i) - (-2-4i) = 9+2i
. Multiplying and dividing are a bit more tricky, but still quite doable: if you treat the i
like a variable, then you just need to multiply out the expression. An i2
simply becomes -1. Thus, (4+2i)*(1+4i) = (4+16i+2i+8i2) = -4 + 18i
. In general, (a1+b1i)*(a2+b2i)
is (a1*a2-b1*b2)+(a1*b2+a2*b1)i
.
Likewise, (2-4i)/(1+i) = ((2-4i)*(1-i))/((1+i)(1-i)) = (-2-6i)/(1+1) = -1-3i
. The general formula for the expression (a1+b1i)/(a2+b2i)
is (a1*a2+b1*b2)/(a2*a2+b2*b2)+(b1*a2-a1*b2)/(a2*a2+b2*b2)i
.
Naturally, just like there are some things which are disallowed with regular numbers, there are things which are disallowed with complex numbers as well. For example, you still can't divide by zero.
In this lab, we will be writing a complex number class, along with all the arithmetic operations we'd expect. Furthermore, we will be checking for illegal conditions and throwing exceptions if they arise.
Task:
First, we want to create two new Exception
classes.
We want ComplexDivideByZeroException
, which should be a type of ArithmeticException
, and InvalidComplexException
, which should be a type of NumberFormatException
. All these exception types need to do is to be able to construct an object which passes an error message up to the parent.
The Complex
class itself is implemented with two fields, real
and im
, for the real and imaginary components of the number. Those fields will be declared final
, so the only way to get a new number is to either construct a new one, or to use some arithmetic operation.
The constructors for this class will allow you to either pass in two parameters (the real and imaginary component), a single double
(just the real component), or a String
(either a real or an imaginary component, which needs to be parsed out).
For functionality, you will need to implement the four major arithmetic operations: addition, subtraction, multiplication, and division. In each case, you will return a new object with the computed value. That's because the original object has it's fields declared final
, so we can't change them but we also know that there will be no side-effects of the operation. Finally, we will also have a toString()
which will print the number in a readable form.
If you want an extra challenge, you can implement this class internally using angle-magnitude rather than real and imaginary components, although the external interface must remain the same.
The Complex
should have the following:
private final double real
the real part of the complex number.
private final double im
the imaginary part of the complex number.
public Complex(double real, double im)
initializes a new complex number with the given real and imaginary parts.public Complex(double real)
initializes a new complex number with the given real part and zero for the imaginary part.public Complex(String num)
initializes a new complex number using the number which is provided in string format. Assume that valid inputs will either be an ordinary double
value, in which case it represents a real part, or an ordinary double followed by the letter "i"
or "I"
, in which case it represents an imaginary part. Thus, an input of "3.14"
would become the complex number 3.14+0i
, while an input of "4i"
would become the complex number 0+4i
. The input could be none of the above, in which case the constructor should throw a InvalidComplexException
.
public double R()
getter for the real component.
public double I()
getter for the imaginary component.
public Complex add(Complex z)
add the complex number z
to this number, and return the result. Note that since all fields are final, you need to create a new complex object instead of modifying the original.
public Complex sub(Complex z)
subtract the complex number z
from this number, and return the result.
public Complex mult(Complex z)
multiply the complex number z
with this number, and return the result. Use the approach described in the background section.
public Complex div(Complex z)
divide this number by the complex number z
, and return the result. Use the approach described in the background section.
@Override public String toString()
express the complex number in the format "a+bi"
, so for example if the real part is 1.5 and the imaginary part is 2.2, then this would give "1.5+2.2i"
. Note that if the imaginary part is negative, for example -2.2 instead of 2.2, it would give "1.5-2.2i"
instead.
Submission instructions are as follows.
xxx_yyyyyyyy_E8/
.java
files into the directory that you've created.ID.txt
in the format shown below, containing your name, userid, G#, lecture section
and lab section, and add it to the directory.
Full Name: Donald Knuth