Question
Java Help 1. A complex (imaginary) number has the form a + bi, where a is called the real part, b is called the imaginary
Java Help
1. A complex ("imaginary") number has the form a + bi, where a is called the real part, b is called the imaginary part, and i = sqrt(-1). A complex number a + bi can be expressed as the ordered pair of real numbers (a, b). Arithmetic operations on two complex numbers (a, b) and (c, d) are as follows:
Addition: (a, b) + (c, d) = (a + c, b + d) Subtraction: (a, b) - (c, d) = (a - c, b - d) Multiplication: (a, b) * (c, d) = (a * c - b * d, a * d + b * c) Division: (a, b) / (c, d) = ((a * c + b * d)/(c2 + d2), (b * c - a * d)/(c2 + d2)) Absolute value: |(a, b)| = sqrt(a2 + b2)
Design and implement a ComplexNumber class that represents the real and imaginary parts as double values and provides at least the following methods:
- Constructors for default and explicit initialization.
- A method to read (get input) a complex number. Look at the sample output screen for the design required.
- A method to print a complex number as (a, b). Have 2 decimals for both.
- A method called getReal that returns the real part of a complex number.
- A method called getImaginary that returns the imaginary part of a complex number.
- Methods equals, copy, getCopy, toString.
- Arithmetic methods to add, subtract, multiply, and divide two complex numbers.
- A method called cAbs to implement the absolute value of a complex number.
To test your class write a client that has at least a function menu() with options for the methods implemented and an option to exit. Your program should loop until the user chooses to exit. In this loop you are required to use a switch statement for all possible cases (similar design as the one used for Problem#1 in Assignment#1).
SAMPLE OUTPUT:
Your options for Complex arithmetic are: ---------------------------------------- 1) Add 2 complex numbers 2) Subtract 2 complex numbers 3) Multiply 2 complex numbers 4) Divide 2 complex numbers 5) Absolute value of a complex number 6) Compare 2 complex numbers 0) EXIT Please enter your option: 2
Enter complex number (real imaginary): 3.4 5.6 Enter complex number (real imaginary): 1.23 2.56 First complex number is: (3.40, 5.60) Second complex number is: (1.23, 2.56) Result: (3.40, 5.60) - (1.23, 2.56) = (2.17, 3.04) Command number 1 completed.
Your options for Complex arithmetic are: ---------------------------------------- 1) Add 2 complex numbers 2) Subtract 2 complex numbers 3) Multiply 2 complex numbers 4) Divide 2 complex numbers 5) Absolute value of a complex number 6) Compare 2 complex numbers 0) EXIT Please enter your option: 4
Enter complex number (real imaginary): 11.2 22.1 Enter complex number (real imaginary): 1.45 3.56 First complex number is: (11.20, 22.10) Second complex number is: (1.45, 3.56) Result: (11.20, 22.10) / (1.45, 3.56) = (6.42, -0.53)
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