Computer Science 60
Principles of Computer Science
Fall 2003
Assignment 0
Due Friday, September 5 by 5 PM
This is a "special" assignment intended to familiarize you with
turing 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, September 8 before midnight -
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 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 ii is -1 by definition.
In your implementation, the numbers a and b in
the complex number a + bi should be represented by doubles. 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.
- The class should have a constructor that takes two double
arguments and sets the real and imaginary components of the complex
number to these values.
- The class should have a second constructor that takes no arguments
and simply sets the real and imaginary components of the complex
number to both be 0.0.
- The class should have a method called equal that
takes a Complex argument and determines if the two Complex
numbers (this one and the argument) are equal. The method
returns a boolean which is true if the numbers are
equal and false otherwise.
- The class should have a method called add
that takes a Complex argument and returns the sum
of this Complex number and the argument. Thus, the
return value is a Complex number itself and neither of the summands have their values changed in any way.
- The class should have a method called multiply that
takes a Complex argument and returns the product of
this Complex number and the argument. Thus the return value is a Complex number itself and neither of
the multiplicands have their values changed in any way.
- The class should have a toString() method that
converts complex numbers into strings for the sake of printing.
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.
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.
Last modified August 2003 by Ran Libeskind-Hadas