jfcmacro

NumComplejo.java

Mar 6th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class NumComplejo here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class NumComplejo
  9. {
  10.     private double real;
  11.     private double img;
  12.    
  13.     public NumComplejo() {
  14.         this(0.0,0.0);
  15.     }
  16.    
  17.     public NumComplejo(double r) {
  18.         this(r,0.0);
  19.     }
  20.    
  21.     public NumComplejo(double r, double i) {
  22.         real = r;
  23.         img = i;
  24.     }
  25.    
  26.     public double obtReal() {
  27.         return real;
  28.     }
  29.    
  30.     public double obtImg() {
  31.         return img;
  32.     }
  33.    
  34.     public boolean sonIguales(NumComplejo otro) {
  35.         return real == otro.real && img == otro.img;
  36.     }
  37.    
  38.     public NumComplejo sumar(NumComplejo operDer) {
  39.         return new NumComplejo(real + operDer.real, img + operDer.img);
  40.     }
  41.    
  42.     public NumComplejo escalar(double r) {
  43.         return new NumComplejo(real * r, img * r);
  44.     }
  45.    
  46.     public NumComplejo multiplicar(NumComplejo operDer) {
  47.        
  48.         double real = this.real * operDer.real - this.img * operDer.img;
  49.         double img = this.real * operDer.img + this.img * operDer.real;
  50.        
  51.         return new NumComplejo(real, img);
  52.     }
  53.    
  54.     public NumComplejo restar(NumComplejo operDer) {
  55.         return new NumComplejo(real - operDer.real, img - operDer.img);
  56.     }
  57.    
  58.     public NumComplejo dividir(NumComplejo operDer) {
  59.         final double DOS = 2.0;
  60.         double dem = Math.pow(operDer.real, DOS)
  61.                    + Math.pow(operDer.img, DOS);
  62.         double real = (this.real * operDer.real + this.img * operDer.img);
  63.         real /= dem;
  64.         double img = (this.img * operDer.real - this.real * operDer.img);
  65.         img /= dem;
  66.         return new NumComplejo(real, img);
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment