SHOW:
|
|
- or go back to the newest paste.
| 1 | package Aufgaben; | |
| 2 | public class Program{
| |
| 3 | public static void main(String[] args) {
| |
| 4 | - | Complex c1 = new Complex(); |
| 4 | + | Complex c1 = new Complex(2, 2); |
| 5 | - | Complex c2 = new Complex(); |
| 5 | + | Complex c2 = new Complex(4, 3); |
| 6 | - | c1.init(2, 2); |
| 6 | + | |
| 7 | - | c2.init(4, 3); |
| 7 | + | |
| 8 | c2.print(); // 4+3i | |
| 9 | ||
| 10 | c1.add(c2); | |
| 11 | c2.mul(c1); | |
| 12 | c2.print(); | |
| 13 | c2.sub(c1); | |
| 14 | ||
| 15 | c1.print(); // 6+5i | |
| 16 | c2.print(); // 3+33i | |
| 17 | } | |
| 18 | } | |
| 19 | ||
| 20 | public class Complex{
| |
| 21 | public int real; | |
| 22 | public int imaginary; | |
| 23 | ||
| 24 | public Complex(int r, int i){
| |
| 25 | - | public void init(int r, int i){
|
| 25 | + | |
| 26 | this.imaginary = i; | |
| 27 | } | |
| 28 | ||
| 29 | public void print(){
| |
| 30 | System.out.print(this.real + "+" + this.imaginary + "i \n"); | |
| 31 | } | |
| 32 | ||
| 33 | public void add(Complex c){
| |
| 34 | this.real += c.real; | |
| 35 | this.imaginary += c.imaginary; | |
| 36 | } | |
| 37 | ||
| 38 | public void mul(Complex c){
| |
| 39 | int oldreal = this.real; | |
| 40 | this.real = this.real * c.real - this.imaginary * c.imaginary; | |
| 41 | this.imaginary = oldreal * c.imaginary + this.imaginary * c.real; | |
| 42 | } | |
| 43 | ||
| 44 | public void sub(Complex c){
| |
| 45 | this.real -= c.real; | |
| 46 | this.imaginary =this.imaginary- c.imaginary; | |
| 47 | } | |
| 48 | } |