// Lab 4: Complex.java
// Definition of class Complex
public class Complex {
private double real;
private double imaginary;
// Initialize both parts to 0
/* Write header for a no-argument constructor. */
{
/* Write code here that calls the Complex constructor that takes 2
arguments and initializes both parts to 0 */
}
// Initialize real part to r and imaginary part to i
/* Write header for constructor that takes two arguments梤eal part r and
imaginary part i. */
{
/* Write line of code that sets real part to r. */
/* Write line of code that sets imaginary part to i. */
}
// Add two Complex numbers
public Complex add( Complex right )
{
/* Write code here that returns a Complex number in which the real part is
the sum of the real part of this Complex object and the real part of the
Complex object passed to the method; and the imaginary part is the sum
of the imaginary part of this Complex object and the imaginary part of
the Complex object passed to the method. */
}
// Subtract two Complex numbers
public Complex subtract( Complex right )
{
/* Write code here that returns a Complex number in which the real part is
the difference between the real part of this Complex object and the real
part of the Complex object passed to the method; and the imaginary part
is the difference between the imaginary part of this Complex object and
the imaginary part of the Complex object passed to the method. */
}
// Return String representation of a Complex number
public String toComplexString()
{
return "(" + real + ", " + imaginary + ")";
}
} // end class Complex
// Lab 4: ComplexTest.java
// Test the Complex number class
import javax.swing.*;
public class ComplexTest {
public static void main( String args[] )
{
Complex a, b;
a = new Complex( 9.9, 7.7 );
b = new Complex( 1.4, 3.1 );
String result = "a = " + a.toComplexString();
result += "\nb = " + b.toComplexString();
result += "\na + b = " + a.add( b ).toComplexString();
result += "\na - b = " + a.subtract( b ).toComplexString();
JOptionPane.showMessageDialog( null, result, "Complex Test",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}