FileDocCategorySizeDatePackage
ComplexNumber.javaAPI DocExample2906Mon Sep 22 13:30:30 BST 1997None

ComplexNumber

public class ComplexNumber extends Object
This class represents complex numbers, and defines methods for performing arithmetic on complex numbers.

Fields Summary
private double
x
private double
y
Constructors Summary
public ComplexNumber(double real, double imaginary)
This is the constructor. It initializes the x and y variables

    this.x = real;
    this.y = imaginary;
  
Methods Summary
public static ComplexNumberadd(ComplexNumber a, ComplexNumber b)
This is a static class method. It takes two complex numbers, adds them, and returns the result as a third number. Because it is static, there is no "current instance" or "this" object. Use it like this: ComplexNumber c = ComplexNumber.add(a, b);

    return new ComplexNumber(a.x + b.x, a.y + b.y);
  
public ComplexNumberadd(ComplexNumber a)
This is a non-static instance method by the same name. It adds the specified complex number to the current complex number. Use it like this: ComplexNumber c = a.add(b);

    return new ComplexNumber(this.x + a.x, this.y+a.y);
  
public doubleimaginary()
An accessor method. Returns the imaginary part of the complex number

 return y; 
public doublemagnitude()
Compute the magnitude of a complex number

 return Math.sqrt(x*x + y*y); 
public static ComplexNumbermultiply(ComplexNumber a, ComplexNumber b)
A static class method to multiply complex numbers

    return new ComplexNumber(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
  
public ComplexNumbermultiply(ComplexNumber a)
An instance method to multiply complex numbers

    return new ComplexNumber(x*a.x - y*a.y, x*a.y + y*a.x);
  
public doublereal()
An accessor method. Returns the real part of the complex number. Note that there is no setReal() method to set the real part. This means that the ComplexNumber class is "immutable".

 return x; 
public java.lang.StringtoString()
This method converts a ComplexNumber to a string. This is a method of Object that we override so that complex numbers can be meaningfully converted to strings, and so they can conveniently be printed out with System.out.println() and related methods

 return "{" + x + "," + y + "}";