Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Zahl {
- public abstract Zahl addieren(Zahl z);
- public abstract Zahl subtrahieren(Zahl z);
- public abstract Zahl multiplizieren(Zahl z);
- public abstract Zahl dividieren(Zahl z);
- public abstract String toString();
- }
- public class ReelleZahl extends Zahl {
- double r;
- public ReelleZahl (double r){
- this.r = r;
- }
- public Zahl addieren(Zahl z) {
- double erg = (this.r + ((ReelleZahl) z).r);
- ReelleZahl r = new ReelleZahl(erg);
- return r;
- }
- public Zahl subtrahieren(Zahl z) {
- double erg = (this.r - ((ReelleZahl) z).r);
- ReelleZahl r = new ReelleZahl(erg);
- return r;
- }
- public Zahl multiplizieren(Zahl z) {
- double erg = (this.r * ((ReelleZahl) z).r);
- ReelleZahl r = new ReelleZahl(erg);
- return r;
- }
- public Zahl dividieren(Zahl z) {
- double erg = (this.r / ((ReelleZahl) z).r);
- ReelleZahl r = new ReelleZahl(erg);
- return r;
- }
- public String toString() {
- return "" + this.r;
- }
- }
- public class KomplexeZahl extends Zahl {
- double a;
- double b;
- public KomplexeZahl(double a, double b) {
- this.a = a;
- this.b = b;
- }
- public Zahl addieren(Zahl z) {
- double a = (this.a + ((KomplexeZahl) z).a);
- double b = (this.b + ((KomplexeZahl) z).b);
- KomplexeZahl k = new KomplexeZahl(a, b);
- return k;
- }
- public Zahl subtrahieren(Zahl z) {
- double a = (this.a - ((KomplexeZahl) z).a);
- double b = (this.b - ((KomplexeZahl) z).b);
- KomplexeZahl k = new KomplexeZahl(a, b);
- return k;
- }
- public Zahl multiplizieren(Zahl z) {
- double a = (this.a * ((KomplexeZahl) z).a) - ((this.b * ((KomplexeZahl) z).b));
- double b = (this.a * ((KomplexeZahl) z).b) + ((this.b * ((KomplexeZahl) z).a));
- KomplexeZahl k = new KomplexeZahl(a, b);
- return k;
- }
- public Zahl dividieren(Zahl z) {
- double a = ((this.a * ((KomplexeZahl) z).a) + ((this.b * ((KomplexeZahl) z).b)))
- / (Math.pow(((KomplexeZahl) z).a, 2) + Math.pow(((KomplexeZahl) z).b, 2));
- double b = ((this.b * ((KomplexeZahl) z).a) - ((this.a * ((KomplexeZahl) z).b)))
- / (Math.pow(((KomplexeZahl) z).a, 2) + Math.pow(((KomplexeZahl) z).b, 2));
- KomplexeZahl k = new KomplexeZahl(a, b);
- return k;
- }
- public String toString() {
- return a + "+" + b + "*i";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement