Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package learningjava;
- public class ComplexNumber {
- public final static ComplexNumber I = new ComplexNumber(0., 1.);
- public final static ComplexNumber ZERO = new ComplexNumber(0.);
- public final static ComplexNumber ONE = new ComplexNumber(1.);
- private double re;
- private double im;
- public ComplexNumber(double re, double im) {
- super();
- this.re = re;
- this.im = im;
- }
- public ComplexNumber() {
- this(0., 0.);
- }
- public ComplexNumber(ComplexNumber z) {
- this(z.re, z.im);
- }
- public ComplexNumber(double re) {
- this(re, 0.);
- }
- public double getRe() {
- return this.re;
- }
- public void setRe(double re) {
- this.re = re;
- }
- public double getIm() {
- return this.im;
- }
- public void setIm(double im) {
- this.im = im;
- }
- public String toString() {
- if(this.im == 0)
- return "" + this.re;
- return "(" + this.re + ";" + this.im + ")";
- }
- public double getModulus() {
- return Math.sqrt(this.re * this.re + this.im * this.im);
- }
- public void addTo(ComplexNumber z) {
- this.re += z.re;
- this.im = this.im + z.im;
- }
- public ComplexNumber getConjugate() {
- ComplexNumber z = new ComplexNumber(this.re, -this.im);
- return z;
- }
- public static ComplexNumber add(ComplexNumber z1, ComplexNumber z2) {
- ComplexNumber z3 = new ComplexNumber(z1.re + z2.re, z1.im + z2.im);
- return z3;
- }
- public ComplexNumber multiplyByC(double c) {
- return new ComplexNumber(this.re * c, this.im * c);
- }
- public static ComplexNumber multiply(ComplexNumber z1, ComplexNumber z2) {
- return new ComplexNumber((z1.re*z2.re) - (z1.im * z2.im), (z1.re*z2.im) + (z1.im*z2.re));
- }
- public static void main(String[] args) {
- ComplexNumber z = new ComplexNumber(1);
- System.out.println(z);
- System.out.println(z.getModulus());
- ComplexNumber z2 = new ComplexNumber(0, 1);
- z.addTo(z2);
- System.out.println(z);
- ComplexNumber z1 = z.getConjugate();
- System.out.println(z1);
- ComplexNumber z4 = ComplexNumber.add(z1, z2);
- System.out.println(z4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment