Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please use java: A complex number has a real component and an imaginary component. So for example, 3+2i is an imaginary number, as is something
Please use java:
A complex number has a real component and an imaginary component. So for example, 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 o dd the corresponding components real to real and imaginary to imaginary). So 2+4i + 1-2i 3+21 and 7-29-2 i)- 9 21. Multiplying and dividing are a bit more tricky, but still quite doable if you treat the like a variable, then you Just need to multiply out the expression. An i2 su ply becomes 1 T us 4+2i * 1+4i 4+16i+2i 8i2 4+18i. In general, (a+bi)(a+b2i) is (a1*a2-b*b(alb2a2*)i Likewise (2-4) (1 i) ((2-4)*(1 i)/ (1 )(1-)) ( 2-6i)(1+1) -1-3. Te general fo mula for the expression (athli) (a2th2 ) 1 (al *a2 bi *b2)(a2*a2+b2*b2) (b *a2-al *b2)/(a2* 2th2*b2) Naturally, Just lik 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 ComplexDivideByZcroException, which should be a typc 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 objcct has it's fields declared final, so we can't change them but we also know that there wil be no side-effects of the opcration. Finally, we will also have a toString0 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. Thc - 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 rcal) initializes a new complex number with the given rcal 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 "T, 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 R0 getter for the real component public double IO 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 toString0 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 ".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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started