This class represents complex numbers, and defines methods for performing arithmetic on complex numbers : Complex Number « Data Type « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » Complex NumberScreenshots 
This class represents complex numbers, and defines methods for performing arithmetic on complex numbers

 

/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

/**
 * This class represents complex numbers, and defines methods for performing
 * arithmetic on complex numbers.
 */
public class ComplexNumber {
  // These are the instance variables. Each ComplexNumber object holds
  // two double values, known as x and y. They are private, so they are
  // not accessible from outside this class. Instead, they are available
  // through the real() and imaginary() methods below.
  private double x, y;

  /** This is the constructor. It initializes the x and y variables */
  public ComplexNumber(double real, double imaginary) {
    this.x = real;
    this.y = imaginary;
  }

  /**
   * 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".
   */
  public double real() {
    return x;
  }

  /** An accessor method. Returns the imaginary part of the complex number */
  public double imaginary() {
    return y;
  }

  /** Compute the magnitude of a complex number */
  public double magnitude() {
    return Math.sqrt(x * x + y * y);
  }

  /**
   * 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
   */
  public String toString() {
    return "{" + x + "," + y + "}";
  }

  /**
   * 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);
   */
  public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
    return new ComplexNumber(a.x + b.x, a.y + b.y);
  }

  /**
   * 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);
   */
  public ComplexNumber add(ComplexNumber a) {
    return new ComplexNumber(this.x + a.x, this.y + a.y);
  }

  /** A static class method to multiply complex numbers */
  public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {
    return new ComplexNumber(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
  }

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

 
Related examples in the same category
1. A class to represent Complex Numbers
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.