Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

b. The result of subtracting two ComplexNumber objects is the difference of the real parts and the difference of the imaginary parts. i.e. (0.5 + 1.5i) - (2.0 + -4.5i) = -1.5 + 6i

c. The result of multiplying two ComplexNumber objects, (a1 + b1i) and (a2 + b2i), is as follows:

i. The real part is equal to the product of the two real parts minus the product of the two imaginary parts. In symbols, this is a1* a2 - b1*b2.

ii. The imaginary part is equal to the product of the first numbers real part and the second numbers imaginary part plus the product of first numbers imaginary part and the second numbers real part. In symbols, this is a1*b2 + b1*a2.

iii. For example, if the current object has the data: (0.5 + 1.5i) and the parameter * (2.0 + -4.5i) is the ComplexNumber returned should be (7.75 + 0.75i).

d. The result of dividing two ComplexNumber objects, (a1 + b1i) and (a2 + b2i), is as follows:

i. The real part is equal to the product of the two real parts plus the product of the two imaginary parts divided by the sum of the squares of the real and imaginary part of the second number. In symbols, this is (*+*)/(+).

ii. The imaginary part is equal to the product of the first numbers imaginary part and the second numbers read part minus the product of first numbers real part and the second numbers imaginary part. This is divided by the sum of the squares of the real and imaginary part of the second number (same as with the real part). In symbols, this is (**)/(+).

iii. For example, if the current object has the data: (0.5 + 1.5i) and the parameter * (2.0 + - 4.5i) is the ComplexNumber returned should be (-0.237 + 0.216i).

6. Write JUnit Tests in the ComplexNumberTest class for each of the arithmetic methods you just added.

7. If you have not yet committed and pushed your changes, do so now.

Task #4 Documenting a Java Program

1. Ensure that your JavaDoc comments are there and that all the required tags are there too.

a. You need a class comment for each class and a method comment for each method. Be sure to use the @ tags correctly where appropriate.

b. You do not need a lot of depth when documenting the test class and methods (i.e. This tests the add method. is fine).

2. Select Project Generate Javadocs. Ensure that the correct file was generated by going to the local git repository in file explorer. It should be located in the doc folder. It should show all the comments and @tags that you entered, output in the format of other standard Java APIs.

3. Make sure you format your code. Ctrl + Shift + F will do most of it for you automatically in Eclipse.

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

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions

Question

b. What groups were most represented? Why do you think this is so?

Answered: 1 week ago