Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a package named Q 2 . Create a new Java class called ComplexNumber as shown in the UML diagram. Specifications for the ComplexNumber Class
Create a package named Q
Create a new Java class called ComplexNumber as shown in the UML diagram.
Specifications for the ComplexNumber Class In combination with the UML:
The constructor without argument will assign zero to all the field values.
The Constructor with parameters: should accept the real and imaginary values
as arguments and assign these values to the fields real and imaginary
Define the accessorgetter methods only; no mutatorsetter methods are required. The getMagnitude method is a helper method which will return the magnitude of
Lab Exercise a complex number. Magnitude of a complex number: abi Use
Math.pow and Math.sqrt methods
The getAngle method is a helper method which will return the angle of a complex number in degrees. Angle of a complex number abi is tan# $ Here i is a symbol
# $
for the imaginary part of the number. For tan use Javas Math class
Math.atanb a that returns the angle in radians, and use Math.toDegreesx method to convert the radians to degrees. You can use these two in nested form. See Unit Part All the trigonometric methods in the Math class return the results in radians, and to get the result in degrees one must use Math.toDegreex method
The displayRecForm method will display the complex number as shown in the sample output. For example, if the complex number is a bi it should display a bi with the corresponding a and b values. The i in the display needs to be hard coded as a literal. The displayed result should use up to decimal places.
In the same Java package, create a driver class with the name:
ComplexNumberDemoYourFirstName.
Import the QStudentGradesByYourFirstName class at the top so that you can use your myHeader and myFooter methods from that class in the same way, you did in your last lab.
This class will contain two public static fields a Scanner type instantiated field, and an int type field with an initial value and four publicstatic methods dataEntry complexAdder complexMultiplier and main
a Specifications for dataEntry method:
o Theheader:publicstaticComplexNumberdataEntry
o Oncecalled,insidethemethod,itwillincrementtheintegertypefieldyou
declared. We use it to keep the track of the number of Complex numbers in a
run. You can see in the sample output that it says complex number etc. o Thenitwillprompttheusertoentertherealandimaginarypartsofa
complex number. Get those with the help of the Scanner type data field you
already declared in this class.
o Atthispoint,itwilldeclareaComplextNumbertypereferencevariableand
instantiate it with the above values.
o Finally,itwillreturntheaboveinstantiatedreferencevariablebacktothe
calling method.
b Specifications for complexAdder method:
o Theheader:
public static ComplexNumber complexAdder ComplexNumber x
ComplexNumber y
o Oncecalled,itwilldeclareanewComplexNumbertypereferencevariableand instantiate it with the added realterm values of x and y and the added imaginaryterm values of x and y
Note: If there are two complex numbers x a bi and y c di then, x y a cb d i
Example: if there are two complex number x i and y i then, x y i i
o Itwillreturntheaboveinstantiatedreferencevariablebacktothecalling method.
Page of
ESB Programming Fundamentals for Engineers
c Specifications for complexMultiplier method:
Lab Exercise
Note: Run the program three times as per the following cases. The output should appear as the
following Sample Outputs. To test your code with more datapoints, use the following website
the result may vary slightly in the decimal points while you are comparing, and that is fine:
https:wwwomnicalculator.commathcartesiantopolar
Run:
o Theheader:
public static ComplexNumber complexMultiplier ComplexNumber x ComplexNumber y
o Oncecalled,itwilldeclareanewComplexNumbertypereferencevariable and instantiate it with the appropriate values that result from the multiplication of x and y
Note: If there are two complex numbers x a bi and y c di then, x y ac bdbc ad i
o Itwillreturntheaboveinstantiatedreferencevariablebacktothecalling method.
d Specifications for the Driver Method:
o CallmyHeaderinthesamewayasyoudidforQinyourpreviouslab. o DeclareaComplexNumbertypereferencevariablex.
o CallthedataEntrymethodandassignthereturnedvaluetoxdeclared
above.
o RepeatthesameprocedureforasecondComplexNumbertypereference
variable y
o Displaythetwocomplexnumbersusingtheabiformasshowninthe
sample output using the displayRecForm method.
o DeclareardcomplexreferencevariablecalledaddRes.
o CallthecomplexAddermethodwiththereferencevariablesxandyas
arguments, and then assign the returned value to addRes.
o DeclareathcomplexreferencevariablecalledmulRes.
o CallthecomplexMultipliermethodwiththereferencevariablesxandy
as arguments, and then assign the returned value to
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