CS100J Spring 2004
Project 3
Due Thursday 3/11 at 3pm

0. Objective

Completing all tasks in this assignment will help you learn about: First skim, and then carefully read the entire assignment before starting any tasks! You'll need your high school math, but we've given enough explanations below to get you through this project. Write (type) your answers to Questions 1 and 5 in one plain text file called q1q5.txt. You must use the identifiers (variable names, class names, etc.) exactly as we specify, including capitalization. Use good programming style and observe the style guidelines that are given in the Project 1/2 Grading Guides. As you finish the project you should have a much better understanding of classes and Java applications.

1. Complex (Imaginary) Numbers

Using conventional real numbers, we cannot take the square root of any negative numbers. As it turns out, taking the square root of negative numbers actually happens quite often in higher mathematics and has real physical implications. Sufficed to say, mathematicians needed some way to conceive of square roots of negative numbers. Complex numbers were born!

Complex numbers take the form a + bi, where i is the square root of -1. Notice that a complex number is comprised of two components, a real part a and an imaginary part b. To get some intuition, consider the quadratic formula:

For an equation ax2 + bx + c = 0, the solution for x is given by:
x =-b ± sqrt(b^2 -4ac)
2a
Consider the equation x2 - 3x + 3. Solving for x results in 3 ± sqrt(-3) which is not a real number. We can represent this result as an imaginary number, 3 ± i*sqrt(3).

You can do the same basic arithmetic with imaginary numbers as you could with real numbers. In fact, much of the math is done as if you were treating i as a variable. Operations include:


In addition, we can perform these additional operations:
Your task is to write a class called ComplexNumber to represent a complex number. Remember that a complex number consists of a real part and an imaginary part - you should have class variables a and b to represent these values. Think about this before you start coding! Answer the following questions in a plain text file called q1q5.txt.
  1. Should the variables a and b be instance or static? Why?
  2. In order to match other pre-defined Java classes representing mathematical quantities, we want to make it possible to write the following statements in a client class that uses class ComplexNumber:
    ComplexNumber cn = new ComplexNumber(5, 2);
    double x= cn.a + cn.b;

    What modifier should you place in the declaration of the variables in the class to make this possible?
  3. If your class is called ComplexNumber, what should the java file be called?
  4. Is the ComplexNumber class an application? Why or why not?

2. Operations with Complex (Imaginary) Numbers

Start your class by declaring the variables a and b representing the real and imaginary parts of a complex number.

Next, write a constructor for a complex number. This constructor should take two parameters, the real and the imaginary components, and set the instance variables to these parameters. Once you have finished the constructor, you can test it in Dr. Java. Create a new instance of a ComplexNumber and then access the a and b fields. Are they what you expect?

In the Interactions Pane
>ComplexNumber c = new ComplexNumber(5, 4);
>c.a
5.0
>c.b
4.0
Now that you have your constructor, you should implement the methods listed below. Test each method after you write it! Incremental testing of your class will end up saving you time. You do not want to wait until the entire class is written to start testing--likely there'll be an overwhelmingly large number of compilation error messages. Experienced programmers know to test incrementally. You should do the same!
In addition, write the following methods for convenience: Note that you must write your methods to match the above specification (including capitalization!). Failure to do so will result in points being lost.

3. Complex Circle

Consider plotting a complex number on a graph where the horizontal axis is the real component and the vertical axis is the imaginary component. For example, look at the graph below. The complex numbers 0 + 1i and 1 + 0i are on the graph.

Notice that the magnitude of both complex numbers is equal to 1. (Recall that magnitude is abs(a + bi) = sqrt(a2 + b2).)In fact, every complex number that lies on the displayed circle has a magnitude of 1, i.e., everyone complex number that lies on a circle of radius r has magnitude r. You can imagine that for every degree around the circle (a circle has 360 degrees or 2π radians), there exists a corresponding imaginary number. More succinctly, any point on a circle is:
rcosθ + rsinθ

where r is the magnitude of the complex number and θ ranges from 0 to 360 degrees (or 0 to 2π radians). Write another method in ComplexNumber called complexCircle. This method should display the complex numbers corresponding to θ = 0, 45, 90, 135, 180, 225, 270, and 315 degrees where r is the magnitude of this instance. For example, given the complex number 1 + 1i, we have the following 'complex circle':

θ (in degrees)Complex Number
0sqrt(2)
451 + 1i
90sqrt(2)i
135-1 + 1i
180-sqrt(2)
225-1 - 1i
270-sqrt(2)i
3151 - 1i

Note that Java's Math.sin and Math.cos take radians, not degrees! To convert from degrees to radians:   radians = π * degrees / 180
(Hint) Figure out on paper what the various θ angles are when expressed in radians.

4. Client Code

Download Project3.java from the Projects page. This file contains skeleton code for a Java application. The main method provides the user with a menu in order to manipulate complex numbers. In addition, there are several other methods in the class. You will need to complete two of the methods. Read the methods and their comments and make sure you understand what they do. Be sure that you understand the provided code before you start filling in the missing parts. Notice that the instance methods you have written in class ComplexNumber are called by the "Op methods" (see method addOp for example). You will finish two incomplete methods: promptForComplexNumber and the main method.

Complete these methods - but do not change any of the other skeleton code.

5. Testing your code

Once you complete Project3 and ComplexNumber, you can try it out in the Interactions Pane. Make sure you have Project3, ComplexNumber, and JLiveRead open. Compile the three files and then type:
>java Project3
in the Interactions Pane. This will run your Java application! Try all the operations. Then answer the following questions in q1q5.txt:
  1. Run your application and display the 'complex circle' for the complex number 1 + i. Explain why the results displayed do not match up with the answers from the example given above
  2. Usually, a for-loop header contains an update that is an increment (or decrement) statement. The for-loop header in the main method in Project3.java does not have an update, yet the for-loop terminates eventually. Why? The update in this for-loop is not an increment statement, what is it?

6. Bonus!

Control the print format so that one decimal place is shown in the real and imaginary parts of a complex number. Hint: check out the DecimalFormat class in the API.

7. Submitting Your Work

Submit your Project3 and ComplexNumber files on-line using CMS (Course Management System) before the project deadline. Make sure you are submitting the correct, up to date, .java files (not .class or .java~). We will not accept any files after the deadline for any reason (except for documented medical reasons). See the CMS link on the web page for instructions on using CMS. If necessary, turn off the DrJava feature that saves the .java~ files (see course webpage announcement on 2/17 for instructions).