CSE 382 Algorithms and Data Structures

Barbara Nostrand, Ph.D.

Electrical Engineering and Computer Science

Laboratory 1 - Exploring Graphics


The goal of this project is to improve your understanding of classes and objects and to introduce graphics. 

ReadSection C.2 of the text for a discussion of simple Graphics methods. 
Controlling Access to Members of a Class. 
Simple ExampleDownload, compile, and execute the rectangle drawing program Rectangle.java
Phasor Class

Here is a working copy of of Phasor.java.  You will need to compile and save it in the same directory as your version of Complex.java

Warning: You must make sure that your Complex.java file employs the same:  method names,  parameter type patterns,  return types,  and access privileges (public/private) as those specified in Phasor.java in order for them to work together.  Otherwise,  you will experience mysterious compiler errors.  Consequently,  you will need to modify the Complex method names and parameter lists in Phasor.java in order for it to work with the version of Complex.java specified below.  Remember that Java is case sensitive

New ClassCreate a Complex class which implements complex numbers.  You need to implement three consturctors and nine basic accessor methods:
  • Complex() - Creates a complex number with value 0;
  • Complex(double realValue) - Creates a real-valued complex number with value realValue;
  • Complex(double realValue, double imagValue) - Creates a complex-valued complex number with value realValue+imagValuei
  • double real() - Returns the real component of internalValue
  • double imag() - Returns the imaginary component of internalValue
  • Complex conj() - Returns the complex conjugate of internalValue
  • double mag() - Returns the magnitude of internalValue
  • Complex add(Complex externalValue) - Returns (internalValue + externalValue)
  • Complex sub(Complex externalValue) - Returns (internalValue - externalValue)
  • Complex mult(Complex externalValue) - Returns (internalValue * externalValue)
  • Complex div(Complex external Value) - Returns (internalValue / externalValue)
  • String toString() - Returns a String representation of internalValue
Note 1. internalValue represents your representation for a complelx number stored inside each object belonging to your Complex class.  It is not necessariliy the name of a variable. 
Note 2. externalValue is the name for a Complex valued parameter passed by the method call. 
Note 3. The complex conjugate of a complex number is computed by inverting the sign of its complex component. 
Note 4. The magnitude of a complex number z is equal to the Euclidean distance from z to the origin.  This value is calculated by using the Pythagorean theorem. 
Note 5. The multiplicative inverse of a complex number z is equal to to the complex conjugate of z divided by the magnitude of z.
Method Your program should do the following things:
  • Acquire an integer n from the user and verify that it is greater than 1. 
  • Construct the complex phase angle w
    import StrictMath;
    double real = cos(2*PI/n);
    double imaginary = sin(2*PI/n);
    double radius = sqrt(pow(real,2) + pow(imaginary,2));
           real = real/radius;
           imaginary = imaginary/radius;
    Complex w = new Complex(real,imaginary);	// w = 2*pi/n
  • Construct the complex phasor called phasor: 
    Complex phasor = new Complex(1,0);		// phasor = 1 + 0*i;
  • Create the graphics window and draw vertical and horizonal axis to divide it into equal quardinants.  Set your scaling factor so that unit distance from the origin will come close to,  but not touch the four sides of your display pane. 
  • Rotate the phasor n times tracing out the path in the graphics window.  The phasor is multiplied by w by using your complex multiply method multiplyYour graphics method called draw draws a line in your complex coordinate system connecting two complex values. 
    for (int i = 0; i < n; i++)
    	draw(phasor, (phasor = phasor.multiply(w)));


You will complete this project with your lab partner. 


You may implement your system as a Java application.  You may use Jgrasp.  Do not use netbeans or produce a Java applet. 

Turn in

You must each turn in a complete software sources and an executable electronic version of your software including test routines.  Your software must be in a single directory/folder as a collection of Java source and class files.  You must not turn in jar files. 

Last modified: 2007 SEP 05