# Creating classes

### Week of Wednesday, 26 October, 2005

Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums

## Objective

This lab is a first introduction to creating classes.  You will complete the code for a class that represents a complex number.  You will need to use two files for this lab: Complex.java and ComplexUsage.java (right click on the link, and select "save as...").  You only need to submit Complex.java.

Thanks to Wikipedia, where the images in this lab came from (specifically the entries for complex numbers and distance)

## Complex Numbers

This section is a (brief!) review of complex numbers.  If you are already familiar with complex numbers, you can safely skip this section.  A full description of complex numbers can be found here.  If you find this section confusing, please ask a TA!

i is defined as the square root of -1.  Armed with i, one can take the square root of any negative number: the square root of -4 is 2i, as 2i*2i is 4i2, and since i2=-1, 4i2=-4.  One can start adding i to regular numbers as well: for example, 1+i.  Thus, a number can have a real part and a complex part.  Consider 3+2i.  This is the normal number 3, and to that is added two times i.  Such number are called complex numbers, and are the subject of this lab.  A complex number has a real part (the normal number system we are used to) and a complex part (a multiple of i).

Complex numbers can also be used to represent a 2-dimensional coordinate system.  The real part is the x coordinate, and the imaginary part is the y coordinate.  Because the x coordinate is the real numbers, the x axis is called the real axis.  Similarly with the y axis: it is called the imaginary axis.  The standard operations that one can do on regular (x,y) coordinates can also be done on complex numbers.

## The Complex class

Look at the code for the Complex class.  The class has two properties

• protected double realPart = 0;

• protected double imaginaryPart = 0;

These numbers represent (not surprisingly) the real and imaginary parts of the complex number.  For example, to represent 2+3i, realPart would be 2.0 and imaginaryPart would be 3.0.  Note that these two fields are protected.  We haven't gone over protected yet, but it is pretty much the same as private at this point.  We made it protected in this lab so that the lab itself is easier to grade.

There are a number of methods defined:

• public Complex(): The default constructor, it doesn't do anything

• public Complex add (Complex otherNumber): This method will find the sum of the current complex number and the passed complex number.  The methods returns a new Complex number which is the sum of the two.

• public double getRealPart(): This method returns the real part of the complex number

• public double getImaginaryPart(): This method returns the imaginary part of the complex number

• public Complex (double r, double i): A specific constructor, it creates the complex object by setting the two fields to the passed values.

• public Complex subtract (Complex otherNumber): This method will find the difference of the current complex number and the passed complex number.  The methods returns a new Complex number which is the difference of the two.

• public Complex multiply (Complex otherNumber): This method will find the product of the current complex number and the passed complex number.  The methods returns a new Complex number which is the product of the two.

• public Complex divide (Complex otherNumber): This method will find the ... of the current complex number and the passed complex number.  The methods returns a new Complex number which is the ... of the two.

• public void setRealPart (double towhat): Used to set the real part of this complex number.

• public void setImaginaryPart (double towhat): Used to set the imaginary part of this complex number.

• public double distance(): Returns the distance from that number to the origin.

• public String toString(): This method allows the complex number to be easily printed out to the screen

Creating these methods is described below.

## The ComplexUsage class

This class just has the main() method.  It's purpose is to use the Complex class.  Note that it will not yet work -- it uses the specific constructor of the Complex class, which has not yet been written.

## Creating the methods

First, create the specific constructor.  If you are confused as to how to start, see slides from chapter 4 that deal with the specific constructor for the Rational class.

Next, create the mutator methods (setRealPart() and setImaginaryPart()).  These are similar to the mutator methods for the Rational class.

The subtract() method is similar to the add() method: a new Complex number is created which is the difference of the current number and the passed number.

## toString()

The toString() method is used to print out the object.  Given a Complex object c, if Java encounters System.out.println(c);, it will call the toString() method to convert c into a String so it can be printed out to the screen.  This method must create a String representation of the complex number, and then return that String.

## Multiply

The multiply() method is a bit more difficult.  To multiply two complex numbers by each other, we use the following formula:

(a+bi) * (c+di) = a*c + a*di + bi*c + bi*di = a*c + (a*d + b*c)i + bdi2

However, remember that i2 = -1.  Thus, the above equation simplifies to:

(a+bi) * (c+di) = (a*c - b*d) + (a*d + b*c)i

Thus, a new complex number needs to be created has the above values for the real part and the imaginary part.

## Division

Dividing two complex numbers is done according to the following formula.

Thus, the division() method must create a new complex number according to the above formula.  Note that in this formula, the denominator can never be zero (as both c2 and d2 will always be positive).

## Distance

The distance between any two points is defined as follows.

This method is finding the distance between the complex number and the origin, so the Δx is the realPart minus 0, or just the realPart.  The same holds for Δy and the imaginaryPart.  Remember that you can use Math.sqrt() to find the square root of a number.

## Sample Execution

If you were to run ComplexUsage, and input values 1, 2, 3, and 4, the result should be the following.  Note that the code in red is what was input by the user.

This program plays around with complex numbers

Enter the real part of the first complex number: 1
Enter the imaginary part of the first complex number: 2
Enter the real part of the second complex number: 3
Enter the imaginary part of the second complex number: 4

The first complex number is 1.0+2.0i
The second complex number is 3.0+4.0i
The sum is 4.0+6.0i
The difference is -2.0+-2.0i
The product is -5.0+10.0i
The quotient is 0.44+0.08i

The distances to the origin are:
2.23606797749979 for 1.0+2.0i
5.0 for 3.0+4.0i

## Submission

When you are finished, you just need to submit the Complex.java file.  The ComplexUsage.java is just for you to use when developing your Complex class, and does not need to be submitted.

## If you are done early....

If you finish this lab early, use any remaining time to work on HW J5 or HW J6.