Complex class project

From Huben's Wiki
Jump to: navigation, search

Complex-Numbers.jpg

This project will create a class called Complex that represents complex numbers. It will have 4 constructors, a constant field, a main() for testing, +, -, x and / operators and a few other methods.

To hand in this code:

  • at the top, put your name and give credit to collaborators and helpers.
  • put the output of the program as a comment at the bottom of the program
  • print it out and hand it in

Copy this code into Dr. Java. Then follow the numbered instructions.

For those of you who have forgotten complex math, assume two complex numbers a and b.

a + b = (areal + breal) + (aimag + bimag)

a * b = (areal * breal - aimag * bimag) + (areal * bimag + aimag * breal)

b. conjugate = breal - bimag

a / b = (a * b.conjugate) / (b * b.conjugate) // note that b * b.conjugate is a real number, not complex.

// Write a class Complex similar to your class Fraction.
// this class will have two double fields, real and imag.
// class Complex should be IMMUTABLE: you cannot change a complex
// number after you create it.  You can only make new complex numbers.
//
// Replace this line with the class declaration.
{
// Contructors:
// no-arguments creates a 0.0+0.0i complex number
// 1 argument creates a complex representation of a real number.
// 2 arguments creates a complex number with real and imaginary parts.
// copy constructor that takes a Complex
//
// Write a toString method.
//
// Methods such as plus() need to create and return a new Complex.
//
// Write a static final constant I that is 0.0+1.0i.
//
// To do this assignment:
//
// (1) Write the class header (above.)
// (2) Create the fields.
// (3) Make the constructors.
// (4) Make the main() by writing the header and uncommenting parts
//     one line at a time.
// (5) Write each method needed as you uncomment more of main.
// (6) Compare your results to the comments after each line.
// (7) If there is something you can't do, leave it commented out.
//
// Replace this line with the declaration of the main method.
  {
// Complex c1 = new Complex();
// implement toString before proceding further
// System.out.println(c1);             // 0.0+0.0i
// Complex c2 = new Complex(3);
// System.out.println(c2);             // 3.0+0.0i
// Complex c3 = new Complex(-2, -5.5);
// System.out.println(c3);             // -2.0-5.5i
// Complex c4 = new Complex(2, 3);
// System.out.println(c4);             // 2.0+3.0i
// Complex c5 = new Complex(c4);
// System.out.println(c5);             // 2.0+3.0i
// System.out.println(c3.getReal());   // -2.0
// System.out.println(c3.getImag());   // -5.5
// no set methods, because immutable!
// System.out.println(c3.plus(c2));    // 1.0-5.5i
// System.out.println(c3.minus(c2));   // -5.0-5.5i
// System.out.println(c3.times(2.0));  // -4.0-11.0i
// System.out.println(c3.times(c4));   // 12.5-17.0i
// System.out.println(c3.conjugate()); // -2.0+5.5i
// System.out.println(c3.divide(c4));  // compare with your calculator, optional exercise
// Make the following work like Math.PI.
// System.out.println(Complex.I);      // 0.0+1.0i
// System.out.println(Complex.I.times(3.3)); // 0.0+3.3i
  }
}


Personal tools
translate