Purpose:
In this lab, you will attempt to work with the Complex
class that we discussed in class.
Hopefully, this well begin to get you accustomed to Java's syntax,
compiler, and interpreter.
Here is the complete code for a simple implementation of Complex numbers. Note that even a simple class may have a large number of methods, including a variety on constructors, accessors, modifiers, and standard methods.
/** * A simple implementation of complex numbers (those of the form * x + y*i) that supports a small set of operations. ** Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved. * * @author Samuel A. Rebelsky * @version 1.1 of January 1998 */ public class Complex implements Cloneable { // +------------+-------------------------------------------------------- // | Attributes | // +------------+ /** * The real part of a complex number which is represented in * traditional "x + y*i" format. */ protected double real_part; /** * The imaginary part of a complex number which is represented * in traditional "x + y*i" format. */ protected double imaginary_part; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Initialize to 0.0 + 0.0i */ public Complex() { this(0.0, 0.0); } // Complex() /** * Initialize to x + y*i */ public Complex(double x,double y) { real_part = x; imaginary_part = y; } // Complex(double,double) /** * Initialize to x + 0.0i */ public Complex(double x) { this(x,0.0); } // Complex(double) // +-----------+--------------------------------------------------------- // | Accessors | // +-----------+ /** * Get the imaginary part of this complex number. */ public double getImaginary() { return imaginary_part; } // getImaginary /** * Get the radius of the vector corresponding to this complex number. */ public double getRadius() { return Math.sqrt(real_part*real_part + imaginary_part*imaginary_part); } // getRadius() /** * Get the real part of this complex number. */ public double getReal() { return real_part; } // getReal() // +-----------+--------------------------------------------------------- // | Modifiers | // +-----------+ /** * Set/change the value of this complex number to x + y*i. */ public void setValue(double x, double y) { real_part = x; imaginary_part = y; } // setValue // +------------------+-------------------------------------------------- // | Standard Methods | // +------------------+ /** * Make a copy of this complex number. */ public Object clone() { return new Complex(real_part, imaginary_part); } // clone /** * Determine if another complex number equals this complex number. */ public boolean equals(Complex other) { return ( (other.real_part == real_part) && (other.imaginary_part == imaginary_part) ); } // equals(Complex) /** * Determine if another object equals this complex number. */ public boolean equals(Object other) { return ( (other instanceof Complex) && (equals((Complex) other)) ); } // equals(Object) /** * Compute a hash code for this number. */ public int hashCode() { // The Double version of the real part Double real_double = new Double(real_part); // The Double version of the imaginary part Double imag_double = new Double(imaginary_part); // Combine them in an interesting fashion (2/3 of the real part // and 1/3 of the imaginary part) return ( (real_double.hashCode() * 2 / 3) + (imag_double.hashCode() / 3) ); } //hashCode /** * Build a string representation of the current complex number. */ public String toString() { return (real_part + "+" + imaginary_part + "i"); } // toString() } // Complex
Begin by making a copy of the
code for the Complex class.
You'll need to save this as Complex.java.
Compile it using the Java compiler. Fix an errors you observe (hopefully, there will be none).
Note that if you try to run that program, Java will complain
because that class does not contain a main method.
Hence, you'll need to build one. It's not clear what the
main for a Complex should do. We don't
normally "run" complex numbers.
Hence, you'll want to build a main whose primary purpose is
to test the Complex class. I'd recommend putting
this main in a separate ComplexTester class.
Here's a sample that you might expand upon. Work with it sufficiently
much that you understand the various methods that Complex
provides. You will certainly want to test comparison.
import rebelsky.io.SimpleInput;
import rebelsky.io.SimpleOutput;
/**
* An interactive test program for my simple implementation of
* complex numbers.
*
* @author Samuel A. Rebelsky
* @author YOUR NAME HERE
* @version 1.0 of January 1998
*/
public class ComplexTester
{
/**
* Read some information and play with complex numbers.
*/
public static void main(String[] args)
{
// An object used for reading input
SimpleInput in = new SimpleInput();
// An object used for printing output
SimpleOutput out = new SimpleOutput();
// The sample complex number we're playing with.
Complex num = new Complex();
// The real part of a new complex number
double rpart;
// The imaginary part of a new complex number
double ipart;
// Tell the user about the default number.
out.println("The default complex number is " + num);
out.println("The hash code for the complex number is " + num.hashCode());
// Read the parts of a new complex number
out.println("I'll allow you to enter a new complex number, part by part.");
out.print("Real part: ");
rpart = in.readDouble();
out.print("Imaginary part: ");
ipart = in.readDouble();
// Update the value
num.setValue(rpart, ipart);
out.println("The complex number is now " + num);
out.println("The hash code is now " + num.hashCode());
// Exit nicely
System.exit(0);
} //main
} // ComplexTexter
See if you can come up with additional constructors and routines that
might be useful for the Complex class. Here are some
possibilities to get you started.
void setReal(double x) -- set the real part of a complex number,
leaving the imaginary part unchanged.
double getAngle() -- get the angle for the vector that
corresponds to the current number.
setValue(double angle, double radius) -- set the value of
the current complex number.
Complex multiply(Complex c) -- return the new complex
number created by multiplying the current complex number by c.
Note that Java will complain if you write one of these (not any one, but a particular one). Any idea why?
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Mon Jan 26 07:56:04 1998.
This page generated on Mon Jan 26 07:59:18 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu