It is necessary to create a complex number: real (a1) and imaginary (b1) (the class is a parent and its fields). Calculate the module of a complex number (method 1) and find the complex number that is the inverse of the given one (method 2)

I need help, I can’t figure it out anymore. Help to add 2 methods

public class Complex { private final double a1; // the real part private final double b1; // the imaginary part // create a new object with the given real and imaginary parts public Complex(double real, double imag) { a1 = real; b1 = imag; } public Complex modul(Complex b, Complex a) { ΠΌΠ΅Ρ‚ΠΎΠ΄ нахоТдСния модуля комплСксного числа double real = ? // double imag = ? return new Complex(real, imag); } public Complex returnchisl(Complex b, Complex a) { ΠΌΠ΅Ρ‚ΠΎΠ΄ нахоТдСния числа, ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠ΅ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ } } 
  • one
    First read the theory on complex numbers. What you wrote here is complete nonsense. commons.apache.org/proper/commons-math/userguide/complex.html complex numbers are implemented in a lot of libraries, including java - ArchDemon
  • the modulus of a complex number is a scalar, which is calculated according to the Pythagorean theorem. - vp_arth

1 answer 1

Define the class of a complex number:

 public class Complex { private double mRe; private double mIm; public Complex(double re, double im) { mRe = re; mIm = im; } public double getRe() { return mRe; } public double getIm() { return mIm; } } 

Everything is simple here: a class with two fields corresponding to the real and imaginary parts of a complex number; constructor and two getters.

The modulus of a complex number z=x+i*y is determined by the expression |z| = sqrt(x^2+y^2) |z| = sqrt(x^2+y^2) . Add to the above class a method that returns the module of this complex number:

 public class Complex { private double mRe; private double mIm; public Complex(double re, double im) { mRe = re; mIm = im; } public double getRe() { return mRe; } public double getIm() { return mIm; } public double abs() { return Math.sqrt(mRe*mRe + mIm*mIm); } } 

For a complex number z = x+i*y reverse will be: 1/z = x/(x^2+y^2) - i*y/(x^2+y^2) . We implement the appropriate method:

 public class Complex { private double mRe; private double mIm; public Complex(double re, double im) { mRe = re; mIm = im; } public double getRe() { return mRe; } public double getIm() { return mIm; } public double abs() { return Math.sqrt(mRe*mRe + mIm*mIm); } public Complex getReciprocal() { double denominator = mRe*mRe + mIm*mIm; if (denominator != 0) { return new Complex(mRe/denominator, -mIm/denominator); } else { throw new IllegalStateException("z = 0"); } } @Override public String toString() { return "Re = " + mRe + ", Im = " + mIm; } } 

Here, the toString() method has also been redefined to get the textual representation of the complex number.

  • Thank you so much, especially for every explanation :) - Giperion