Computer Science 60
Principles of Computer Science
Spring 2005


Assignment 0: Implementing Complex Numbers with Class [40 points]
Due Friday, January 21 by 5 PM
Recommended Reading: All handouts from first class.

This is a "special" assignment intended to familiarize you with turing (the CS department's main server) and exercise your understanding of Java classes. Note that it is due this Friday before 5 PM. The next assignment, Assignment 1, will be short and will be due on Monday, January 24 by 11:59 PM - the normal due day and time for assignments in this course.

First, logon to turing from a terminal in Beckman 102 using your new account. Go through the "Quick Guide to Unix" handed out in class. Spend a bit of time going through this guide and using the commands that it introduces. In particular, as described in the "Quick Guide to Unix", make a directory called assignment0 inside your cs60 directory. Within the assignment0 directory, you will now create your first Java program. Use the emacs editor (or whatever other editor you prefer) to create this file.

Java has a number of built-in types such as int, char, boolean, float, and double, among others. Scientists and engineers often like to work with complex numbers, but complex numbers are not a built-in type in Java. Therefore, your task is to implement a complex number class. The class should be implemented in a file named Complex.java.

Recall that a complex number is of the form a + bi where a and b are real numbers and i is an imaginary number. Addition of two complex numbers is defined by (a + bi) + (c + di) = (a+c) + (b+d)i. Similarly, to multiply two complex numbers, we simply compute (a + bi) * (c + di) = ac + adi + bci + bdii = (ac - bd) + (ad + bc) i since i*i is -1 by definition.

In your implementation, the numbers a and b in the complex number a + bi should be represented by doubles. They should be private data members of your Complex class. Below are the public methods (functions) which your Complex class should contain. Be sure to use exactly these names and the specified kinds of arguments and return values. You may use any additional private methods that you like in order to build your public methods.

You'll find a program called ComplexTester.java in the directory /cs/cs60/assignments/assignment0 which you can copy to your own directory, compile, and run to test your Complex class. In general, it's a very good idea to write test programs to test every method in your class.

A note about commenting your code: Please be sure to have the following three pieces of information as comments at the top of each file: The name of the java class (such as "Complex" in this case), your name, and the date the java class was completed. In addition, have at least one line of information that explains what the class is used for. Then, for each method in the class, have at least one line of comments explaining what it does. If the method does something complicated that might be confusing to someone reading the code, make sure to have explanatory comments within the method as well.

Please also keep your code neat and easy-to-read. Use "whitespace" (spaces and line-breaks) to make your code readable. In theory, you could write your entire program on one very long line. This would be very hard to read. As a good rule-of-thumb, a line should never be longer than 80 characters long. Almost all editors will give you a window that is 80 characters wide. Try to make sure that your lines don't wrap around from one line to the next. In this assignment, we won't take off points for these kinds of issues, but we will make a note to you to be careful about this in the future.

When you are ready to submit your program, type cs60submit Complex.java in the directory which contains your Complex.java file. Be careful to submit Complex.java and NOT the compiled version Complex.class. You will receive an e-mail message at your turing account indicating that you have submitted your file. (Note that your turing account is not the same as the standard issue hmc account that every Mudder gets!) If you made a mistake and want to submit a new version, that's fine. Just use cs60submit again. The system keeps the most recent submission and checks that it was submitted before the due time.

Admittedly silly and completely optional extra credit      (up to +5 points)

The methods of your Complex class are not -- or at least not intended to be -- particularly complex. The real goal of this assignment is to give you a chance to familiarize yourself with things.

However, since it may have been a while since you last used java, here is a "brain-teaser" of sorts you might try for extra credit. It is possible to write every method in the Complex class using only one line of code. If you'd like (it's entirely optional), see how many methods you can write in one line of code. Note that you must stick with conventional style guidelines, so you can't simply place multiple logical lines of code onto a single line in your file. You may, however, need to use a somewhat unorthodox construction or two -- a couple of these are somewhat tricky... !

Last modified January 2005 by Zach Dodds or Ran Libeskind-Hadas