Question: You have to write a class for complex numbers. This class must be called Complex. A basic skeleton of the class is given as a

 You have to write a class for complex numbers. This class

You have to write a class for complex numbers. This class must be called Complex. A basic skeleton of the class is given as a starting point. Your class must be complete enough for a professional use. For example, your class must provide at least one constructor, accessors and mutators, methods add, subtract, multiply, divide, conjugate, negative, modulus, toString, etc. Two static methods (getDecPlaces and setDecPlaces) must also be provided as a way to control the number of decimal places used in method toString to represent the real and imaginary parts of the complex numbers. By default, the number of decimal places will be 2.

To test your complex class, a user will be allowed to enter the following commands from the keyboard:

define : Define a complex variable called with an initial value of +i.

define : Define a complex variable called with no initial value. Notice that the value of this variable should not be used until it has been assigned a value.

set : Change the value of the existing variable to +i.

show : Display the complex number stored in variable with the specified number of decimal places. For example, if the number of decimal places is 4, complex numbers will be shown as: 0.7500+9.2800i, -3.4500+7.9925i, 8.5500-6.4500i

add : Add and and store the result in the existing variable

subtract : Subtract from and store the result in the existing variable

multiply : Multiply by and store the result in the existing variable

divide : Divide by and store the result in the existing variable

negative : Change the sign of the real and imaginary part of the complex number stored in variable

conjugate : Change the sign of the imaginary part of the complex number stored in variable

decimal : Set the number of decimal places when displaying a complex number. The default value is 2.

Write a second class called TestComplex that will read the commands from the keyboard and display the result on the standard output.

Input Format

The input will consist of several lines. In each line, there is a valid command. The commands have to be processed until reaching the end-of-file.

Constraints

Unfortunately, Hackerrank does not allow us to create 2 files. In the ideal solution, we should have a file called Complex.java for the class that manages the complex numbers, and another file called TestComplex.java for the test class. Here, we will just have one file with both classes.

Output Format

The output of the show commands. For more details, see the test cases.

Sample Input 0

decimal 4 define c1 -1.2 -7.83 define c2 -0.45 2.07 define sum add sum c1 c2 show sum 

Sample Output 0

-1.6500-5.7600i 

Sample Input 1

define c1 1.256 -7.83 define c2 0.45 2.078 define prod multiply prod c1 c2 show prod decimal 4 show prod 

Sample Output 1

16.84-0.91i 16.8359-0.9135i 

Sample Input 2

define c1 1.2 -4.5 define c2 -7.8 3.2 define c3 -3.4 -0.8 define c4 3.3 2.8 define tmp1 multiply tmp1 c1 c2 decimal 5 show tmp1 define tmp2 multiply tmp2 c3 c4 show tmp2 add tmp1 tmp1 tmp2 decimal 2 show tmp1 decimal 6 show tmp1 

Sample Output 2

5.04000+38.94000i -8.98000-12.16000i -3.94+26.78i -3.940000+26.780000i 

Sample Input 3

define c1 4.20 -2.32 define c2 0.252 3.35 divide result c1 c2 show result negate result show result decimal 3 show result decimal 4 show result decimal 5 show result decimal 6 conjugate result show result 

Sample Output 3

-0.59-1.30i 0.59+1.30i 0.595+1.298i 0.5949+1.2985i 0.59486+1.29848i 0.594861-1.298479i 

Code:

class Complex { /* Enter your code here for the Complex class. */ private double real, imag; private static int decPlaces = 2; public static int getDecPlaces() { /* To be completed */ } public static void setDecPlaces(int decPlaces) { /* To be completed */ } }

class TestComplex { /* Enter your code here for the TestComplex class. */ public static void main(String[] args) { /* To be completed */ } }

The idea is to implement a class for complex numbers. As a reminder, a complex number can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit (which satisfies the equation i21). In this expression, a is called the real part of the complex number, and b is called the imaginary part. If za+bi, then we define real(z)-a, and imag(z)-b. Some of the operations defined on complex numbers are shown below Addition: (a + bi) + (c+ di) - (a+c)(b d)i Subtraction: (a + bi) - (c+ di) -(a c)(b - d)i Multiplication: (a +bi) x (c+ di) (ac - bd) +(bc +ad)i Division:(a +bi)/ Conjugate: a +bi-a - bi (c+ di) (ac +bd)/(cd) (bc - ad)/(c d2)i Negative:-(a +bi)--a - bi . Modulus: la + bil- /a2 + b2 The idea is to implement a class for complex numbers. As a reminder, a complex number can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit (which satisfies the equation i21). In this expression, a is called the real part of the complex number, and b is called the imaginary part. If za+bi, then we define real(z)-a, and imag(z)-b. Some of the operations defined on complex numbers are shown below Addition: (a + bi) + (c+ di) - (a+c)(b d)i Subtraction: (a + bi) - (c+ di) -(a c)(b - d)i Multiplication: (a +bi) x (c+ di) (ac - bd) +(bc +ad)i Division:(a +bi)/ Conjugate: a +bi-a - bi (c+ di) (ac +bd)/(cd) (bc - ad)/(c d2)i Negative:-(a +bi)--a - bi . Modulus: la + bil- /a2 + b2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!