Concept explainers
(Math: The Complex class) A complex number is a number in the form a + bi, where a and b are real numbers and i is
a + bi + c + di = (a + c) + (b + d)i
a + bi – (c + di) = (a – c) + (b – d)i
(a + bi) * (c + di) = (ac — bd) + (bc + ad)i
(a + bi) / (c + di) = (ac + bd) / (c2 + d2) + (bc – ad)i / (c2 + d2)
You can also obtain the absolute value for a complex number using the following formula:
(A complex number can be interpreted as a point on a plane by identifying the (a, b) values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin, as shown in Figure 13.10.)
Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, and abs for performing complex-number operations, and override toString method for returning a string representation for a complex number. The toString method returns (a + bi) as a string. If b is 0, it simply returns a. Your Complex class should also implement Cloneable and Comparable. Compare two complex numbers using their absolute values.
Provide three constructors Complex (a, b), Complex (a), and Complex (). Complex () creates a Complex object for number 0, and Complex (a) creates a Complex object with 0 for b. Also provide the getRealPart() and getImaginaryPart () methods for returning the real part and the imaginary part of the complex number, respectively.
Draw the UML class diagram and implement the class. Write a test program that prompts the user to enter two complex numbers and displays the result of their addition, subtraction, multiplication, division, and absolute value. Here is a sample run:
Enter the first complex number: 3.5 5.5 Enter the second complex number: –3.5 1 (3.5 + 5.5i) + (–3.5 + 1.0i) = 0.0 + 6.5i (3.5 + 5.5i) – (–3.5 + 1.0i) = 7.0 + 4.5i (3.5 + 5.5i) * (–3.5 + 1.0i) = –17.75 + –15.75i (3.5 + 5.5i) / (–3.5 + 1.0i) = –0.5094 + –1.7i |(3.5 + 5.5i)| = 6.519202405202649 |
Want to see the full answer?
Check out a sample textbook solutionChapter 13 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- When you perform arithmetic operations with operands of different types, such as adding an int and a float, ____________. C# chooses a unifying type for the result you must choose a unifying type for the result you must provide a cast you receive an error messagearrow_forwardint power(int base,int exp); *This function accepts the arguments for base and exponent and returns power. The algorithm is to repeatedly multiply the value of the base to how many time the value of exponent.* For example: Test Input Result printf("%d",power(2,5)); 2 32 5 printf("%d",power(3,4)); 3 81 4 -Use C language, please. Thank you.arrow_forward(Python GUI -Tkinker) You have a group of friends coming to visit for your high school reunion, and you want to take them out to eat at a local restaurant. You are not sure if any of them have dietary restrictions, but your restaurant choices are as follows: Joe’s Gourmet Burgers—Vegetarian: No, Vegan: No, Gluten-Free: No Main Street Pizza Company—Vegetarian: Yes, Vegan: No, Gluten-Free: Yes Corner Café—Vegetarian: Yes, Vegan: Yes, Gluten-Free: Yes Mama’s Fine Italian—Vegetarian: Yes, Vegan: No, Gluten-Free: No The Chef’s Kitchen—Vegetarian: Yes, Vegan: Yes, Gluten-Free: Yes Write a Python program GUI that asks whether any members of your party are vegetarian, vegan, or gluten-free, to which then displays only the restaurants to which you may take the group.arrow_forward
- 58arrow_forwarduse c++arrow_forwardFactorial) The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is defined as follows:For example, 5!= 5.4.3.2.1 , which is 120. Use while statements in each of the following:A. Write a program that reads a nonnegative integer and computes and prints its factorial.B. Write a program that estimates the value of the mathematical constant e by using the formula:Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).C. Write a program that computes the value of by using the formula Prompt the user for the desired accuracy of e (i.e., thenumber of terms in the summation).arrow_forward
- The following variables have been initialized for you: int a; float f; double d; You have to cast the variable into different data types as follows: Cast a as float to a variable x. Cast f as an integer to a variable y. Cast d as float to a variable z. Hint: You have to initialize the variables x,y and z of proper data typesarrow_forwardQ2) (Perfect Numbers) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.arrow_forwardPlease help answer these two questions in Python. (See attached image) Attach screenshots of your code for clarity.arrow_forward
- (DEBUG AND MAKE A FLOWCHART OF THIS PROGRAM) // This pseudocode is intended to display// employee net pay values. All employees have a standard// $45 deduction from their checks.// If an employee does not earn enough to cover the deduction,// an error message is displayed.// This example is modularized.start Declarations string name string EOFNAME = ZZZZ while name not equal to EOFNAME housekeeping() endwhile while name not equal to EOFNAME mainLoop() endwhile while name not equal to EOFNAME finish() endwhilestop housekeeping() output "Enter first name or ", EOFNAME, " to quit "return mainLoop() Declarations num hours num rate num DEDUCTION = 45 num net output "Enter hours worked for ", name input hours output "Enter hourly rate for ", name input rate gross = hours * rate net = gross - DEDUCTION if net > 0 then output "Net pay for ", name, " is ", net else output "Deductions not covered. Net is…arrow_forwardUSE PYTHON Source: en.wikipedia.org/wiki/Camel_case In some languages, it’s common to use camel caseLinks to an external site. (otherwise known as “mixed case”) for variables’ names when those names comprise multiple words, whereby the first letter of the first word is lowercase but the first letter of each subsequent word is uppercase. For instance, whereas a variable for a user’s name might be called name, a variable for a user’s first name might be called firstName, and a variable for a user’s preferred first name (e.g., nickname) might be called preferredFirstName. Python, by contrast, recommendsLinks to an external site. snake caseLinks to an external site., whereby words are instead separated by underscores (_), with all letters in lowercase. For instance, those same variables would be called name, first_name, and preferred_first_name, respectively, in Python. In a file called camel.py, implement a program that prompts the user for the name of a variable in camel case and outputs…arrow_forwardContext: Using Python, I am also a beginner in programming. Having a hard time completing this practice and assisstance is appreciatedarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr