Advertisement
AnindyaBiswas

complex

May 19th, 2022
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3. public class complex{
  4.     public static void main(String Args[])
  5.     {
  6.         Scanner sc = new Scanner(System.in);
  7.         complex_add obj = new complex_add();
  8.         System.out.print("Enter real part of 1st number : ");
  9.         int r1 = sc.nextInt();
  10.         System.out.print("Enter real part of 2nd number : ");
  11.         int r2 = sc.nextInt();
  12.         System.out.print("Enter img part of 1st number : ");
  13.         int i1 = sc.nextInt();
  14.         System.out.print("Enter img part of 2nd number : ");
  15.         int i2 = sc.nextInt();
  16.         obj.set_i1(i1);
  17.         obj.set_i2(i2);
  18.         obj.set_r1(r1);
  19.         obj.set_r2(r2);
  20.         obj.add();
  21.         System.out.println(obj);
  22.         System.out.println(obj.getClass());
  23.      
  24.     }
  25. }
  26.  
  27. class complex_add {
  28.     private int r1, r2, i1, i2;
  29.  
  30.     complex_add()
  31.     {
  32.         System.out.println("Constructor called");
  33.         r1 = 0;
  34.         r2 = 0;
  35.         i1 = 0;
  36.         i2 = 0;
  37.     }
  38.    
  39.     void set_r1(int v)
  40.     {
  41.         r1 = v;
  42.     }
  43.    
  44.     void set_r2(int v)
  45.     {
  46.         r2 = v;
  47.     }
  48.    
  49.     void set_i1(int v)
  50.     {
  51.         i1 = v;
  52.     }
  53.    
  54.     void set_i2(int v)
  55.     {
  56.         i2 = v;
  57.     }
  58.    
  59.     void add()
  60.     {
  61.         System.out.println("\nThe sum is  = " + (r1+r2) + "+" + (i1+i2) + "i");
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement