Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6.2 Question 2 Create a package named Q2. Recycle the Q3 from Lab#4. Create a calculator program to perform arithmetic operations on complex numbers. Create

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

6.2 Question 2 Create a package named Q2. Recycle the Q3 from Lab#4. Create a calculator program to perform arithmetic operations on complex numbers. Create a java class named ComplexNumber, as shown in the UML diagram. Circle -real: double -imaginary: double +ComplexNumber() + ComplexNumber (re: double, im: double) +getReal(): double +getimaginary(): double +getMagnitude(): double +getAngle(): double +addTwoComplexNumbers(a: ComplexNumber, b: ComplexNumber): ComplexNumber +subtract TwoComplexNumbers(a: ComplexNumber, b: ComplexNumber): ComplexNumber +multiplyTwoComplexNumbers(a: ComplexNumber, b:ComplexNumber): ComplexNumber +divide TwoComplexNumbers(a: ComplexNumber, b:ComplexNumber): ComplexNumber Specifications for the ComplexNumber Class: The constructor without argument will assign all field values to zero. . The constructor with parameter should accept the real and imaginary values as arguments, and assign these values to the fields real and imaginary. The accessor/getter method getReal() will return the real value of the complex number. The accessor/getter method getImaginary0 will return the imaginary value of the complex number. The getMagnitude() method is a helper method which will return the magnitude of a complex number. Magnitude of a complex number a+bi = Va? + b2 (Use Math's pow() and sqrt() methods, see Unit 2: slide 139-140 in regular version) The getAngle() method is a helper method which will return the angle of a complex number in degrees. Angle of a complex number a+bitan (Use Math's atem2(yl) atan(x) and toDegrees(x) methods, see Unit 2: slide 140 in regular version. All the trigonometric methods in Math Class returns the result in radians, and to get the result in degrees one has to use toDegree() method) The addTwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments, and after adding these two complex numbers, it will return a ComplexNumber type reference variable. (Hint: Review slide numbers 76 and 84, Unit 4 (regular version) The subtract TwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments (For example x and y), subtract the two (1.e. X-y), and will return a Complex Number type reference variable. The multiplyTwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments, and after multiplying these two complex numbers, it will return a Complex Number type reference variable. Formula for multiplication: (a+bi)*(c+di) -(ac - bd)+(betad)i The divide TwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments(for example x and y), divide the two (i.e. x/y) and will return a Complex Number type reference variable. Formula for division: (a+bi)/(c+di) - (ac+bd) + (bc-adyk i where ke2 + Specifications for the Driver Class: In the same Java package, create a new driver class with the name ComplexNumberDemo Your FirstName. If your first name is Quazi, the name of this class should be ComplexNumberDemoQuazi. This class will contain three public static methods: your driver method, header and footer methods and another public static method called convertRectangularTopolarFrom(). Define the rectangular ToPolarForm() method as a public static void method which will accept only one ComplexNumber type reference variable as an argument, and then print the polar form of that complex number with the help of the helper methods getMagnitude() and getAngle(). This ComplexNumber object, that you need to pass, can be any object such as A or B. See the sample output before implementing this method. From inside the driver method do the following: a. Call your header method and print the required info. b. Declare two ComplexNumber type reference variables. c. Ask the user to enter the real and imaginary numbers for the first complex number, and get those inputs with the help of a Scanner reference variable. d. Instantiate the first ComplexNumber type reference variable. e. Repeat steps c) and d) for the second one. f. Declare a third ComplexNumber type reference variable and instantiate with the no argument constructor. Use this variable to store your resultant Complex Number value from addition, subtraction, multiplication, or division operation. g. Print a statement to ask the user to select the calculation they would like to perform. Display a menu for addition, subtraction, multiplication, division and Rectangular to Polar to the user. (see the sample output before implementing this part) h. Take user input and perform the right calculation as directed by the user. You must use switch statements to make a decision here. i. Using printf() method print the results as outlined in the sample output. Please note that the polar form for the complex number will be printed from the rectangular ToPolar Form() method using printf statement up to two decimal places. j. Call the footer method and end the program. Sample output Run 1: ****** Your Full Name Lab #?, Question #? ******* YourFirstName's complex-number adder: Calculator for complex numbers: Enter the real part of the first number: 3.52 Enter the imaginary part of the first number: 7.20 Enter the real part of the second number: 8.11 Enter the imaginary part of the second number: -9.31 You entered the following complex numbers: A = 3.52 + 7.201 B = 8.11 - 9.311 What calculation would you like to perform? 1) Addition (A+B) 2) Subtraction (A-B) 3) Multiplication (A*B) 4) Division (A/B) 5) Rectangular to Polar (A/B) Enter choice here: 2 Here is the result: A - B = -4.59 + 16.511 *** Goodbye from yourFullName *** Run 2: Your Full Name Lab #?, Question #? YourFirstName's complex-number adder: Calculator for complex numbers: Enter the real part of the first number: 3.52 Enter the imaginary part of the first number: 7.20 Enter the real part of the second number: 8.11 Enter the imaginary part of the second number: -9.31 You entered the following complex numbers: A = 3.52 + 7.20 B = 8.11 - 9.311 What calculation would you like to perform? 1. Addition (A+B) 2. Subtraction (A-B) 3. Multiplication (A*B) 4. Division (A/B) 5. Rectangular to Polar (4407 Enter choice here: 5 Which Complex number do you want to convert to polar form? Enter your choice (A or B): B Here is the result: 4.59 +46-544-Magnitude: 4.44 and Angle: 74.46 Hegrees 8.11 - 9.31i - Magnitude: 12.25 and Angle: -48.94 degrees. *** Goodbye from yourFullName *** 6.2 Question 2 Create a package named Q2. Recycle the Q3 from Lab#4. Create a calculator program to perform arithmetic operations on complex numbers. Create a java class named ComplexNumber, as shown in the UML diagram. Circle -real: double -imaginary: double +ComplexNumber() + ComplexNumber (re: double, im: double) +getReal(): double +getimaginary(): double +getMagnitude(): double +getAngle(): double +addTwoComplexNumbers(a: ComplexNumber, b: ComplexNumber): ComplexNumber +subtract TwoComplexNumbers(a: ComplexNumber, b: ComplexNumber): ComplexNumber +multiplyTwoComplexNumbers(a: ComplexNumber, b:ComplexNumber): ComplexNumber +divide TwoComplexNumbers(a: ComplexNumber, b:ComplexNumber): ComplexNumber Specifications for the ComplexNumber Class: The constructor without argument will assign all field values to zero. . The constructor with parameter should accept the real and imaginary values as arguments, and assign these values to the fields real and imaginary. The accessor/getter method getReal() will return the real value of the complex number. The accessor/getter method getImaginary0 will return the imaginary value of the complex number. The getMagnitude() method is a helper method which will return the magnitude of a complex number. Magnitude of a complex number a+bi = Va? + b2 (Use Math's pow() and sqrt() methods, see Unit 2: slide 139-140 in regular version) The getAngle() method is a helper method which will return the angle of a complex number in degrees. Angle of a complex number a+bitan (Use Math's atem2(yl) atan(x) and toDegrees(x) methods, see Unit 2: slide 140 in regular version. All the trigonometric methods in Math Class returns the result in radians, and to get the result in degrees one has to use toDegree() method) The addTwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments, and after adding these two complex numbers, it will return a ComplexNumber type reference variable. (Hint: Review slide numbers 76 and 84, Unit 4 (regular version) The subtract TwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments (For example x and y), subtract the two (1.e. X-y), and will return a Complex Number type reference variable. The multiplyTwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments, and after multiplying these two complex numbers, it will return a Complex Number type reference variable. Formula for multiplication: (a+bi)*(c+di) -(ac - bd)+(betad)i The divide TwoComplexNumbers() method will accept two ComplexNumber type reference variables as arguments(for example x and y), divide the two (i.e. x/y) and will return a Complex Number type reference variable. Formula for division: (a+bi)/(c+di) - (ac+bd) + (bc-adyk i where ke2 + Specifications for the Driver Class: In the same Java package, create a new driver class with the name ComplexNumberDemo Your FirstName. If your first name is Quazi, the name of this class should be ComplexNumberDemoQuazi. This class will contain three public static methods: your driver method, header and footer methods and another public static method called convertRectangularTopolarFrom(). Define the rectangular ToPolarForm() method as a public static void method which will accept only one ComplexNumber type reference variable as an argument, and then print the polar form of that complex number with the help of the helper methods getMagnitude() and getAngle(). This ComplexNumber object, that you need to pass, can be any object such as A or B. See the sample output before implementing this method. From inside the driver method do the following: a. Call your header method and print the required info. b. Declare two ComplexNumber type reference variables. c. Ask the user to enter the real and imaginary numbers for the first complex number, and get those inputs with the help of a Scanner reference variable. d. Instantiate the first ComplexNumber type reference variable. e. Repeat steps c) and d) for the second one. f. Declare a third ComplexNumber type reference variable and instantiate with the no argument constructor. Use this variable to store your resultant Complex Number value from addition, subtraction, multiplication, or division operation. g. Print a statement to ask the user to select the calculation they would like to perform. Display a menu for addition, subtraction, multiplication, division and Rectangular to Polar to the user. (see the sample output before implementing this part) h. Take user input and perform the right calculation as directed by the user. You must use switch statements to make a decision here. i. Using printf() method print the results as outlined in the sample output. Please note that the polar form for the complex number will be printed from the rectangular ToPolar Form() method using printf statement up to two decimal places. j. Call the footer method and end the program. Sample output Run 1: ****** Your Full Name Lab #?, Question #? ******* YourFirstName's complex-number adder: Calculator for complex numbers: Enter the real part of the first number: 3.52 Enter the imaginary part of the first number: 7.20 Enter the real part of the second number: 8.11 Enter the imaginary part of the second number: -9.31 You entered the following complex numbers: A = 3.52 + 7.201 B = 8.11 - 9.311 What calculation would you like to perform? 1) Addition (A+B) 2) Subtraction (A-B) 3) Multiplication (A*B) 4) Division (A/B) 5) Rectangular to Polar (A/B) Enter choice here: 2 Here is the result: A - B = -4.59 + 16.511 *** Goodbye from yourFullName *** Run 2: Your Full Name Lab #?, Question #? YourFirstName's complex-number adder: Calculator for complex numbers: Enter the real part of the first number: 3.52 Enter the imaginary part of the first number: 7.20 Enter the real part of the second number: 8.11 Enter the imaginary part of the second number: -9.31 You entered the following complex numbers: A = 3.52 + 7.20 B = 8.11 - 9.311 What calculation would you like to perform? 1. Addition (A+B) 2. Subtraction (A-B) 3. Multiplication (A*B) 4. Division (A/B) 5. Rectangular to Polar (4407 Enter choice here: 5 Which Complex number do you want to convert to polar form? Enter your choice (A or B): B Here is the result: 4.59 +46-544-Magnitude: 4.44 and Angle: 74.46 Hegrees 8.11 - 9.31i - Magnitude: 12.25 and Angle: -48.94 degrees. *** Goodbye from yourFullName ***

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

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago