CSCI A201/A597 and I210

Lab Notes Seven

Second semester 2000-2001


Complex numbers and fractions.
The minute paper (lab assignment) is in QuizSite, due Saturday at the end of the day.

For this lab assignment a Complex number is a number of the following form:

a + bi
where a and b are of type int.

For example the following are Complex numbers that satisfy our definition:

The second one can also be written as:
2 - i
The coefficients a and b are called the real and imaginary part of the complex number, respectively. But you could call them anything, for example Humpty and Dumpty would be names just as good.

Normally a and b would have to be real numbers (that is, have type double) but we want to restrict the set to only those numbers with an integer real part and an integer imaginary part to simplify things and help you with Fractions.

In this lab we will define a class of Complex numbers. When we're done we write a simple test program that will look very similar to the main program from the fourth assignment.

Here are the operations defined on our Complex numbers:

We don't define division because that would take us out of ints. But for the fun of it you could define it as follows and use / as integer division. Note: these operations become straightforward if you remember that i2 is -1.

If you don't, you can just use the formulas.

Note also that your Complex class should have constructors that accept initial values for the real and imaginary part of type int. Your Complex class should define two instance variables a and b that are of type int. No-arg constructors don't make much sense for Complex numbers, but you could make the default case 0 (zero) also.

Also add a toString() instance method in class Complex, to be able to print them.

Here's the program and the test:

class Complex {

    int real, imag;

    Complex (int humpty, int dumpty) {
	real = humpty;
	imag = dumpty; 
    } 

    Complex add(Complex other) {
	int a = real + other.real; 
	int b = imag + other.imag; 
	return new Complex(a, b); 
    } 

    Complex sub(Complex other) {
	int a = real - other.real; 
	int b = imag - other.imag; 
	return new Complex(a, b); 
    } 

    Complex mul(Complex other) {
	int a = real * other.real - imag * other.imag; 
	int b = real * other.imag + imag * other.real; 
	return new Complex(a, b); 
    } 

    public String toString() {
	String hum = real + "";
	String dum;
        if (imag >= 0) 
          if (imag == 1) 
            dum = "+ ";
          else dum = "+ " + imag; 
	else 
          if (imag == -1) 
            dum = "- ";
          else dum = "" + imag; 
	return "(" + hum + " " + dum + "i)" ; 
    }

    boolean equals(Complex other) {
	if (other.real == real && other.imag == imag) 
	    return true; 
	else 
	    return false;
    }

    boolean isReal() {
	if (imag == 0) 
	    return true; 
	else 
	    return false;
    }

    public static void main(String[] args) {
	Complex f = new Complex(1, 2); 
	Complex g = new Complex(3, -1); 

	System.out.println("Test of operations: ");

	System.out.println("  Add: " + f + " + " + g + " = " + f.add(g));
	System.out.println("  Sub: " + f + " - " + g + " = " + f.sub(g)); 
	System.out.println("  Mul: " + f + " * " + g + " = " + f.mul(g)); 

         System.out.println("Test of predicates: "); 
         System.out.print("  1. Does " + f + " equal " + g + "? "); 
         System.out.println(" The answer is: " + f.equals(g)); 

         Complex h = new Complex(8, 0); 
         System.out.print("  2. Is " + h + " a real number? "); 
         System.out.println("The answer is: " + h.isReal()); 

	 h = (f.add(g)).mul(f.sub(g)); // get this? 

         System.out.print("  3. Is " + h + " a real number? "); 
         System.out.println("The answer is: " + h.isReal()); 

    } 

} 
And running it, one obtains:
frilled.cs.indiana.edu%javac Complex.java
frilled.cs.indiana.edu%java Complex
Test of operations: 
  Add: (1 + 2i) + (3 - i) = (4 + i)
  Sub: (1 + 2i) - (3 - i) = (-2 + 3i)
  Mul: (1 + 2i) * (3 - i) = (5 + 5i)
Test of predicates: 
  1. Does (1 + 2i) equal (3 - i)?  The answer is: false
  2. Is (8 + 0i) a real number? The answer is: true
  3. Is (-11 + 10i) a real number? The answer is: false
frilled.cs.indiana.edu%
If you have any questions please let us know.


Last updated: Feb 22, 2001 by Adrian German for A201