Theudas

Aufgabe 2

Mar 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. package Aufgaben;
  2. public class Program{
  3.     public static void main(String[] args) {
  4.         Complex c1 = new Complex();
  5.         Complex c2 = new Complex();
  6.         c1.init(2, 2);
  7.         c2.init(4, 3);
  8.         c1.print(); // 2+2i
  9.         c2.print(); // 4+3i
  10.  
  11.         c1.add(c2);
  12.         c2.mul(c1);
  13.         c2.print();
  14.         c2.sub(c1);
  15.  
  16.         c1.print(); // 6+5i
  17.         c2.print(); // 3+33i
  18.         }
  19. }
  20.  
  21. public class Complex{
  22.   public int real;
  23.   public int imaginary;
  24.  
  25.   public void init(int r, int i){
  26.     this.real = r;
  27.     this.imaginary = i;
  28.   }
  29.  
  30.   public void print(){
  31.     System.out.print(this.real + "+" + this.imaginary + "i \n");
  32.    }
  33.  
  34.   public void add(Complex c){
  35.     this.real += c.real;
  36.     this.imaginary += c.imaginary;
  37.   }
  38.  
  39.   public void mul(Complex c){
  40.     int oldreal = this.real;
  41.     this.real = this.real * c.real - this.imaginary * c.imaginary;
  42.     this.imaginary = oldreal * c.imaginary + this.imaginary * c.real;
  43.   }
  44.  
  45.   public void sub(Complex c){
  46.     this.real -= c.real;
  47.     this.imaginary =this.imaginary- c.imaginary;
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment