Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 [40pts]. A complex (imaginary) number has the form a + bi, where a is called the real part, b is called the imaginary

Question 1 [40pts]. 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 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 equal, 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) Command number 2 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: 2 Enter complex number (real imaginary): 1.78 4.5

Enter complex number (real imaginary): 3.56 8.9

First complex number is: (1.78, 4.50)

Second complex number is: (3.56, 8.90)

Result: (1.78, 4.50) - (3.56, 8.90) = (-1.78, -4.40) Command number 3 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: 3 Enter complex number (real imaginary): 2.22 3.33

Enter complex number (real imaginary): 1.24 2.45

First complex number is: (2.22, 3.33)

Second complex number is: (1.24, 2.45)

Result: (2.22, 3.33) * (1.24, 2.45) = (-5.41, 9.57) Command number 4 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: 6 Enter complex number (real imaginary): 1.11 2.22

Enter complex number (real imaginary): 1.11 2.22

First complex number is: (1.11, 2.22)

Second complex number is: (1.11, 2.22)

The complex numbers are equal. Command number 5 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: 6 Enter complex number (real imaginary): 1.2 2.3

Enter complex number (real imaginary): 11.2 2.3

First complex number is: (1.20, 2.30)

Second complex number is: (11.20, 2.30)

The complex numbers are NOT equal. Command number 6 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: 5 Enter complex number (real imaginary): 11.1 22.2

The complex number is: (11.10, 22.20)

Result: |(11.1, 22.2)| = 24.82 Command number 7 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: 0 Testing completed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions