Question: Task #1 ComplexNumber Class 1. Accept the assignment, open your repository, select Clone or Download, copy to the clipboard. 2. This repository is empty. Since

Task #1 ComplexNumber Class

1. Accept the assignment, open your repository, select Clone or Download, copy to the clipboard.

2. This repository is empty. Since there is not project yet, we do not have to import the project. We will simply create a local repository (folder) and connect it to the repository on GitHub. In the Git Repositories window, right click and Paste repository path or URI called Lab03.

3. Now we can create our own project, but have it put the files for it in the empty local repository. Select FileCreate a new java project called Lab03. Uncheck the Use default location box and use the location of the git repository you just created.

4. Create a new package and call it cs145.Lab03.

5. In the package, create a new class called ComplexNumber.

6. There should be two double instance variables: one to hold the real part and the other to hold the imaginary part.

7. Write four constructors:

a. A default constructor that sets both parts to 0.

b. A constructor that takes two double parameters (the new real and new imaginary parts in that order). It initializes the instance variables to the values in the corresponding parameters.

c. A constructor that takes one double parameter for the real part and sets the imaginary part to 0.

d. A copy constructor.

8. Automatically generate the setter and getter methods by Alt+Shift+ (tap)S, (tap)R. Select all and make sure the Generate method comments is checked.

Task #2 Unit Testing a Java Method

1. We want to test our code to ensure correct functionality. Right click on the project in the package explorer window. Select New Junit Test Case.

2. It should bring up a window as shown below. Be sure that the check boxes for setUp() and Generate comments are checked. Click Next.

3. Check the box next to ComplexNumber, which will select all methods. Click Finish.

4. This will bring up the window below. This happens once per project when you first setup JUnit test for that project. Click OK.

5. Notice this creates a class called ComplexNumberTest that contains stubs for test methods corresponding to each method in the ComplexNumber class. The @Test above the method indicates that this can run as a test without writing a main.

6. The general approach for each test will be to create your object, call some methods, and compare (using an assert method) to see if the expected value is what you get. Since you will need to create objects to test each method, lets declare several of them as instance variables of this class (above all the methods). The create them (at least one created with each constructor) in the setUp method, which gets run before each test executes.

7. In each of the constructor methods, you can call an assertEquals method for the real part to compare the expected value to the actual value. When comparing double values, you do not want to compare them directly for equality. Instead, check to see if they are close enough. To do that use the assertEquals statement:

assertEquals(, , );

Where is the double value you expected to get, is the number from your methods and is some number that determines if your values are close enough. Something like 0.00001 would be appropriate.

Example:

assertEquals(5.5, c.getReal(), 0.00001);

Repeat for the imaginary part in each constructor.

8. Write assert methods to thoroughly test each constructor, get method and set method. Test.

9. Lets commit our initial files to the repository. This will be our initial commit, so it can be on the main branch since we dont have anything there yet. Use your browser to confirm that it has been uploaded to Git. Now return to eclipse and make a branch to continue to add methods to your class.

Task #3 Adding Functionality to the Class

1. Write a method called conjugate. This should return a ComplexNumber object and take no parameters. The object returned should be a copy of the calling object, but with the sign on the imaginary part negated. Example: if the calling object is 6 + 2i, the conjugate is 6 2i.

2. Write a JUnit Test method in the ComplexNumberTest class to ensure the correctness of the conjugate method.

3. Write a toString method. It should return a String object and take no parameters. The string returned from the method should have the format:

a. real + imaginary i e.g. 4.000 + 3.333i

b. 3 decimals, Hint: use DecimalFormat class

c. Space before and after the +

d. The i following the imaginary part

4. Write a JUnit Test method in the ComplexNumberTest class to ensure the correctness of the toString method.

5. Write methods to add, subtract, multiply and divide. Each will return a ComplexNumber and take a ComplexNumber as a parameter. Remember that each operation is a binary operation (takes two ComplexNumbersthe calling object and the parameter object) and returns a result which is a new Complex Number. None of these methods will change the original objects.

a. The result of adding two ComplexNumber objects is the sum of the real parts and the sum of the imaginary parts. i.e. (0.5 + 1.5i) + (2.0 + -4.5i) = 2.5 + -3i

This program needs to java. I only needs the code not the whole process.

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!