On This Page
Overview
During this assignment, you will:
- Create a class for performing arithmetic on complex numbers
- Use classes as method parameters and return types
- Use a test class to drive the class you write
Background Information
All complex numbers are of the form:
a + bi
where 'a' and 'b' are real numbers and i is √-1. We call 'a' the real part and 'b' the imaginary part.
We cannot represent all real numbers with a computer. However, we can approximate real numbers using floating-point numbers, such as the primitive types float and double. To represent complex numbers, we will need to use one floating-point variable to represent the real part and another variable to represent the imaginary part.
Complex-Number Arithmetic
We can add, subtract, multiply and divide complex numbers like we would expect from elementary algebra.
To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts. For example, the sum of (5 + 3i) and (4 + 2i) is (9 + 5i). Another example: the sum of (3 + i) and (-1 + 2i) is (2 + 3i).
Multiplication is more complex but we can treat it algebraically with the following rule:
(a + bi)(c + di) = (ac - bd) + (ad + bc)i
Division is still more complex but we can treat it algebraically, as well, with the following rule:
a + bi
c + di |
= |
(ac + bd) + (-ad + bc)i
c2 + d2 |
More Information
^ top
Specifications
Create a class named Complex for performing arithmetic with complex numbers. Use two floating-point numbers, such as double, to represent the private data contained in your class. Within this class, implement all the following methods:
- A default constructor with default values in case no initializers are provided.
- An overloaded constructor that accepts a real argument for initializing the private data of your class and which sets a default value for the imaginary part.
- An overloaded constructor that accepts a real and an imaginary argument for initializing the private data of your class.
- "Get" methods to return the private data values named such that the methods pass the test program.
public Complex add(Complex z): Adds two complex numbers. The real parts are added together and the imaginary parts are added together.
public Complex subtract(Complex z): Subtracts two Complex numbers. The real part of the right operand is subtracted from the real part of the left operand. Also, the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
public Complex multiply(Complex z): Multiplies two Complex numbers.
public Complex divide(Complex z): Divides two Complex numbers. If a divide by zero is detected, return null. (last sentence added 10/7/03)
public String toString(): Returns a String containing the complex number in the form (a, b), where a is the real part and b is the imaginary part.
Note that calling toString must not display any values. The caller is responsible for displaying the value returned by toString.
Sample Output
Testing Complex()
Testing Complex(double)
Testing Complex(double, double)
Testing add(Complex)
Testing subtract(Complex)
Testing multiply(Complex)
Testing divide(Complex)
Testing toString()
*** All tests passed ***
^ top
Test Cases
Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.
To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.
Install the tests by copying the following file into the same directory as your .java source code files:
Run the tests as you would any other Java program. You may need to compile your source code before compiling and running the test code.
^ top
Extra Credit
The following are worth extra credit points:
- Add another operation appropriate for the
Complex class. Include a test method to test the new feature in a test class. (2 points)
Make certain that your README.txt file lists any extra credit attempted.
^ top
Grading Criteria
The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.
Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.
For information on grading policies, including interpretation of scores, see the course Syllabus.
Program Compilation
- 4: Source code compiles with no errors or warnings
- 0: Does not compile
Functionality
- 10: Demonstrates mastery of the assignment
- Has extra features or demonstrates techniques beyond the assignment
- Applies concepts from the lessons appropriately
- Meets all specifications (see above) with particularly elegant solutions
- No errors encountered during operation
- All test cases pass
- 8: Has all the functionality expected of the assignment
- Demonstrates many techniques from the lesson
- Meets all specifications (see above)
- Implementation seems more complicated than necessary.
- May have one minor error
- All test cases pass
- 6: Has most of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets all but one of the specifications (see above)
- Implementation seems excessively complicated.
- May have 2-3 minor errors
- All but one test case passes
- 4: Has some of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets at least 1/2 of the specifications (see above)
- Implementation seems excessively complicated.
- May have more than 3 minor errors
- At least 1/2 of all test cases pass
- 2: Serious functional problems but shows some effort and understanding
- Meets less than 1/2 of the of the specifications (see above)
- Has a major error or many minor errors
- Implementation seems very convoluted
- Demonstrates few techniques from the lesson
- Less than 1/2 of all test cases pass
- 0: Does not execute
Program Style
- 4: Code is well-documented
- 3: Code has minor documentation errors
- Has 1 documentation error
- 2: Code has some documentation errors
- Has 2-3 documentation errors
- 1: Code has many documentation errors
- Has more than 3 documentation errors
- 0: No apparent attempt to document code
README.txt File
- 2: README.txt file submitted with specified information included
- 1: README.txt submitted but some information was not included
- 0: No README.txt submitted
Maximum Score: 20, plus extra credit
^ top
What to Turn In
Submit your assignment following the instructions for homework. Include the following items for grading:
README.txt file
Complex.java
- Any other source code needed to make your program function
You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.
^ top
Home
| WebCT
| Announcements
| Schedule
| Expectations
| Syllabus
Help
| FAQ's
| HowTo's
| Links
Last Updated: October 07 2003 @13:54:57
|