Theudas

Aufgabe 1 ideone

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