CS 211, 004/006
Lab Exercise-8
due Thursday, October 25th at noon


Part of the appeal of object-oriented programming is the ability to design more abstract versions of ordinary data types. As an example, let's consider complex numbers. Complex numbers are like ordinary numbers, except that they also have an imaginary component with a special behavior: when the the imaginary number i is multiplied by itself, it becomes -1. In this lab, we design a complex number class, complete with exceptions when something doesn't go as planned.

Overview:

  1. Create two Exception types, ComplexDivideByZeroException and InvalidComplexException.
  2. Create the Complex number class, complete with a set of methods for mathematical operations.
This is a lab exercise on which collaboration (including discussing the solution on Piazza or searching online sources) is explicitly permitted. It is still in your interest to understand and prepare your final submission yourself.

Background:

A complex number has a real component and an imaginary component. So for example, 3+2i is an imaginary number, as is something like -2+i or 2.5-6i. As with ordinary real numbers, complex numbers can be added, subtracted, multiplied and divided. Adding and subtracting is straightforward: you only need to add the corresponding components (real to real and imaginary to imaginary). So (2+4i) + (1-2i) = (3+2i), and (7-2i) - (-2-4i) = 9+2i. Multiplying and dividing are a bit more tricky, but still quite doable: if you treat the i like a variable, then you just need to multiply out the expression. An i2 simply becomes -1. Thus, (4+2i)*(1+4i) = (4+16i+2i+8i2) = -4 + 18i. In general, (a1+b1i)*(a2+b2i) is (a1*a2-b1*b2)+(a1*b2+a2*b1)i.

Likewise, (2-4i)/(1+i) = ((2-4i)*(1-i))/((1+i)(1-i)) = (-2-6i)/(1+1) = -1-3i. The general formula for the expression (a1+b1i)/(a2+b2i) is (a1*a2+b1*b2)/(a2*a2+b2*b2)+(b1*a2-a1*b2)/(a2*a2+b2*b2)i. Naturally, just like there are some things which are disallowed with regular numbers, there are things which are disallowed with complex numbers as well. For example, you still can't divide by zero.

In this lab, we will be writing a complex number class, along with all the arithmetic operations we'd expect. Furthermore, we will be checking for illegal conditions and throwing exceptions if they arise.

Task:

First, we want to create two new Exception classes. We want ComplexDivideByZeroException, which should be a type of ArithmeticException, and InvalidComplexException, which should be a type of NumberFormatException. All these exception types need to do is to be able to construct an object which passes an error message up to the parent.

The Complex class itself is implemented with two fields, real and im, for the real and imaginary components of the number. Those fields will be declared final, so the only way to get a new number is to either construct a new one, or to use some arithmetic operation.

The constructors for this class will allow you to either pass in two parameters (the real and imaginary component), a single double (just the real component), or a String (either a real or an imaginary component, which needs to be parsed out).

For functionality, you will need to implement the four major arithmetic operations: addition, subtraction, multiplication, and division. In each case, you will return a new object with the computed value. That's because the original object has it's fields declared final, so we can't change them but we also know that there will be no side-effects of the operation. Finally, we will also have a toString() which will print the number in a readable form.

If you want an extra challenge, you can implement this class internally using angle-magnitude rather than real and imaginary components, although the external interface must remain the same.

The Complex should have the following:

Testing:

The tester for this lab is in multiple parts, and can be downloaded from:

Submission:

Submission instructions are as follows.

  1. Let xxx be your lab section number (one of 213-220), and let yyyyyyyy be your GMU userid. Create the directory xxx_yyyyyyyy_E8/
  2. Place your three .java files into the directory that you've created.
  3. Create the file ID.txt in the format shown below, containing your name, userid, G#, lecture section and lab section, and add it to the directory.

    Full Name: Donald Knuth
    userID: dknuth
    G#: 00123456
    Lecture section: 004
    Lab section: 213

  4. compress the folder and its contents into a .zip file, and upload the file to Blackboard.