Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class ComplexNo{
  2. private int real;
  3. private int img;
  4.  
  5.  
  6. public void getNo(int a,int b){
  7. this.real=a;
  8. this.img=b;
  9. System.out.println("Real number is: "+this.real+"\nImaginary number is: "+this.img+"i");
  10. }
  11. public void getNo(ComplexNo a){
  12. this.real=a.real;
  13. this.img=a.img;
  14. }
  15. public ComplexNo operatorPlus(ComplexNo x){
  16. ComplexNo temp = new ComplexNo();
  17. temp.real=this.real+x.real;
  18. temp.img=this.img+x.img;
  19. return temp;
  20. }
  21.  
  22. public void display(){
  23. if(this.img>0){
  24. System.out.println("The complex no is:"+this.real+"+"+this.img+"i");
  25. }else{
  26. System.out.println("The complex no is:"+this.real+"-"+Math.abs(this.img)+"i");
  27. }
  28. }
  29.  
  30. }
  31. public class TestComplexNo {
  32.  
  33. public static void main(String[] args) {
  34. ComplexNo c1 = new ComplexNo();
  35. ComplexNo c2 = new ComplexNo();
  36. ComplexNo c3 = new ComplexNo();
  37. ComplexNo c4 = new ComplexNo();
  38.  
  39. c1.getNo(12, -3);
  40. c2.getNo(4, 3);
  41. c3.getNo(3,4);
  42. c4.getNo(c1.operatorPlus(c2).operatorPlus(c3));
  43. c4.display();
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement