Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. public abstract class Zahl {
  2.  
  3.     public abstract Zahl addieren(Zahl z);
  4.    
  5.     public abstract Zahl subtrahieren(Zahl z);
  6.    
  7.     public abstract Zahl multiplizieren(Zahl z);
  8.    
  9.     public abstract Zahl dividieren(Zahl z);
  10.    
  11.     public abstract String toString();
  12.    
  13. }
  14.  
  15. public class ReelleZahl extends Zahl {
  16.  
  17.     double r;
  18.    
  19.     public ReelleZahl (double r){
  20.         this.r = r;
  21.     }
  22.  
  23.     public Zahl addieren(Zahl z) {
  24.         double erg = (this.r + ((ReelleZahl) z).r);
  25.         ReelleZahl r = new ReelleZahl(erg);
  26.         return r;
  27.     }
  28.  
  29.     public Zahl subtrahieren(Zahl z) {
  30.         double erg = (this.r - ((ReelleZahl) z).r);
  31.         ReelleZahl r = new ReelleZahl(erg);
  32.         return r;
  33.     }
  34.  
  35.     public Zahl multiplizieren(Zahl z) {
  36.         double erg = (this.r * ((ReelleZahl) z).r);
  37.         ReelleZahl r = new ReelleZahl(erg);
  38.         return r;
  39.     }
  40.  
  41.     public Zahl dividieren(Zahl z) {
  42.         double erg = (this.r / ((ReelleZahl) z).r);
  43.         ReelleZahl r = new ReelleZahl(erg);
  44.         return r;
  45.     }
  46.  
  47.     public String toString() {
  48.         return "" + this.r;
  49.     }
  50.    
  51. }
  52.  
  53. public class KomplexeZahl extends Zahl {
  54.  
  55.     double a;
  56.     double b;
  57.  
  58.     public KomplexeZahl(double a, double b) {
  59.         this.a = a;
  60.         this.b = b;
  61.     }
  62.  
  63.     public Zahl addieren(Zahl z) {
  64.         double a = (this.a + ((KomplexeZahl) z).a);
  65.         double b = (this.b + ((KomplexeZahl) z).b);
  66.         KomplexeZahl k = new KomplexeZahl(a, b);
  67.         return k;
  68.     }
  69.  
  70.     public Zahl subtrahieren(Zahl z) {
  71.         double a = (this.a - ((KomplexeZahl) z).a);
  72.         double b = (this.b - ((KomplexeZahl) z).b);
  73.         KomplexeZahl k = new KomplexeZahl(a, b);
  74.         return k;
  75.     }
  76.  
  77.     public Zahl multiplizieren(Zahl z) {
  78.         double a = (this.a * ((KomplexeZahl) z).a) - ((this.b * ((KomplexeZahl) z).b));
  79.         double b = (this.a * ((KomplexeZahl) z).b) + ((this.b * ((KomplexeZahl) z).a));
  80.         KomplexeZahl k = new KomplexeZahl(a, b);
  81.         return k;
  82.     }
  83.  
  84.     public Zahl dividieren(Zahl z) {
  85.         double a = ((this.a * ((KomplexeZahl) z).a) + ((this.b * ((KomplexeZahl) z).b)))
  86.                 / (Math.pow(((KomplexeZahl) z).a, 2) + Math.pow(((KomplexeZahl) z).b, 2));
  87.         double b = ((this.b * ((KomplexeZahl) z).a) - ((this.a * ((KomplexeZahl) z).b)))
  88.                 / (Math.pow(((KomplexeZahl) z).a, 2) + Math.pow(((KomplexeZahl) z).b, 2));
  89.         KomplexeZahl k = new KomplexeZahl(a, b);
  90.         return k;
  91.     }
  92.  
  93.     public String toString() {
  94.         return a + "+" + b + "*i";
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement