CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017 

Lab 6: Complex Numbers

In this lab, you will create a class that allows one to use complex numbers. You should start off with s1 driving and s2 navigating.

1. Introductions

Introduce yourself to your lab partner. Are you a baseball fan? Will the Tigers win the World Series?

Using Complex Numbers

In this lab you will familiarize yourself with the java representation of objects by working with a class which supports complex numbers. For this project, you need to complete a class which performs simple operations on complex numbers.

Recall that there is no real number such that x2 = -1, because the square of any real number must be nonnegative. A complex number c can be expressed as a + bi, where a and b are real numbers and i is the square root of -1. If b = 0, then the complex number c is real, in the same way that a real number without any digits to the right of the decimal point is an integer. Arithmetic operations can be performed on complex numbers using this formulation. Let c = a + b i and z = x + y i be complex numbers. Then

c + z = (a + x) + (b + y)i,
and
c z = (a + b i)(x + y i) = a x + a y i + b x i + b y i2 = (ax + b y i2) + (a y i + b x i) = (a x - b y) + (b x + a y)i.
Subtraction and division are defined in a similar way. The complex number system is closed under addition, subtraction, multiplication, and division (except by 0), and taking roots. This is just some of the elegance found in the complex number system.

The Fundamental Theorem of Algebra states that every polynomial of degree n >= 1 with complex coefficients has at least one solution in the complex numbers. This means if we have such a polynomial, all solutions can be considered complex. Gauss finally proved this in 1849. He also introduced the term complex number in 1831.

Download the Complex.java class and the TestComplex.java class. Complete the methods in Complex.java, as shown below. Change the driver/navigator roles after each method.

// Represents a complex number using two doubles, 
// one for the real component and
// one for the imaginary component

public class Complex{
  
  // variables here!
  

// Create printable format
// a+bi if b != 0 OR
// a 	if b==0
public String toString() {
   return String.format("");
   }
  

// Default constructor creates 0+0i
public Complex() {
   }
   
   
// General constructor creates a+bi
public Complex(double a, double b) {
   }
   
   
// Real constructor creates a+0i
   public Complex(double a) {
   }
   
   
// Copy constructor creates a new Complex by copying z
   public Complex(Complex z) {
   }
   
   
// Mutator method to set real component
public void setReal(double a) {
   }

// Mutator method to set imaginary component
public void setImaginary(double b) {
   } 


// Accessor method to set imaginary component
public double getReal() {
  return 0.0;
   }


// Accessor method to get imaginary component
public double getImaginary() {
  return 0.0;
   }

// Accessor method to get modulus sqrt(re^2 + im^2)
public double modulus() {
  return 0.0;
   }

// Accessor method to get argument atan(im/re) -- use atan2
public double argument() {
  return 0.0;
   }

//conjugate
public static Complex conjugate(Complex z) {
   return new Complex();
   }
   
//conjugate
public static Complex negate() {
   return new Complex();
   }


// Add z1+z2 and return result
public static Complex add(Complex z1, Complex z2) {
   return new Complex();
   }

// Subtract z1-z2 and return result
public static Complex subtract(Complex z1, Complex z2) {
   return new Complex();
   }

// Multiply z1*z2 and return result
public static Complex multiply(Complex z1, Complex z2) {
   return new Complex();
   }

// Divide z1/z2 and return result
public static Complex divide(Complex z1, Complex z2) {
   return new Complex();
   }

// Compute e^z = e^(a+bi) = (e^a)(e^ib) = [e^a][(cos(b) + i sin(b))]
public static Complex exp(Complex z) {
   return new Complex();
   }
   

// See http://mathworld.wolfram.com/ComplexExponentiation.html
// Compute z^w = (a + bi)^(c+di) = (A^2 + b^2)^((c+di)/2)e^(i(c+id)arg(a+ib))
public static Complex power(Complex z, Complex w) {
   return new Complex();
   }
   


//conjugate this
public void conjugate() {
   }
   
// Add c to this Complex number
public void add(Complex c) {
   }

// Subtract c from this Complex number
public void subtract(Complex c) {
   }

// Multiply this Complex number by c
public void multiply(Complex c) {
   }

// Divide this Complex number by c
public void divide(Complex c) {
   }


// method to get sqrt() of this
public Complex sqrt() {
   return new Complex();
   }

// method to get sqrt(x) of a double
public static Complex sqrt(double x) {
   return new Complex();
   }

// method to get sqrt(z)
public Complex sqrt(Complex z) {
   return new Complex();
   }

public boolean equals(Complex z){
  return false;
}


}


Test your code with the TestComplex.java class.


public class TestComplex {
public  static void main(String args[]) {
  Complex c = new Complex();
  Complex d = new Complex(1);
  Complex e = new Complex(0, 1);
  Complex f = new Complex(2, 1);
  Complex g = new Complex(2, -1);
  Complex h = new Complex(-6, -8);

// Add other test cases as needed

  System.out.println("This should print 0: " + c);
  System.out.println("This should print 1: " + d);
  System.out.println("This should print i: " + e);
  System.out.println("This should print 2+i: " +f);

  System.out.println("This should print 4: " + Complex.add(f, g));
  System.out.println("This should print 2i: " + Complex.subtract(f, g));

  System.out.println("This should print -1: " + Complex.multiply(e, e));
  System.out.println("This should print -20-10i: " + Complex.multiply(g, h));
  System.out.println("This should print 10: " + h.modulus());
  System.out.println("This should print ?: " + h.argument());
  System.out.println("This should print -6+8i: " + Complex.conjugate(h));
  System.out.println("This should print 6+8i: " + h.negate());

  c.setReal(9);
  c.setImaginary(8);
  System.out.println("This should print 9: " + c.getReal());
  System.out.println("This should print 8: " + c.getImaginary());
  System.out.println("This should print 9+8i: " + c);

  f.add(new Complex(5, -1));
  System.out.println("This should print ___: " +f);

  f.subtract(new Complex(5, -1));
  System.out.println("This should print ___: " +f);

  f.multiply(new Complex(5, -1));
  System.out.println("This should print ___: " +f);

  f.divide(new Complex(5, -1));
  System.out.println("This should print ___: " + f);

  System.out.println("This should print 2^i: " +
    Complex.power(new Complex(2.0,0.0), new Complex(0.0,1.0)));

  System.out.println("This should print sqrt(i): " +
    (new Complex(0.0,1.0)).sqrt());

  System.out.println("This should print i^i: " +
    Complex.power(new Complex(0.0,1.0), new Complex(0.0,1.0)));

  System.out.println("This should print 1: " +
    Complex.exp(new Complex(0.0, 2*Math.PI)));
}
}

When you are finished, email your lab files to your lab partner and instructor.


Copyright © 2017, David A. Reimann. All rights reserved.