62 lines
1.5 KiB
Java
62 lines
1.5 KiB
Java
import java.util.Scanner;
|
|
|
|
class Complex {
|
|
|
|
double real;
|
|
double imaginary;
|
|
|
|
public Complex(double real, double imaginary) {
|
|
this.real = real;
|
|
this.imaginary = imaginary;
|
|
}
|
|
|
|
public Complex add(int realPart) {
|
|
return new Complex(this.real + realPart, this.imaginary);
|
|
}
|
|
|
|
public Complex add(Complex other) {
|
|
return new Complex(
|
|
this.real + other.real,
|
|
this.imaginary + other.imaginary
|
|
);
|
|
}
|
|
|
|
public String toString() {
|
|
return this.real + " + " + this.imaginary + "i";
|
|
}
|
|
}
|
|
|
|
public class ComplexAdder {
|
|
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
int a, b, c, d, e;
|
|
|
|
System.out.print(
|
|
"Enter the values of a and b, for the first Complex Number of the form a + ib: "
|
|
);
|
|
a = sc.nextInt();
|
|
b = sc.nextInt();
|
|
|
|
System.out.print(
|
|
"Enter the values of c and d, for the second Complex Number of the form c + id: "
|
|
);
|
|
c = sc.nextInt();
|
|
d = sc.nextInt();
|
|
|
|
System.out.print(
|
|
"Enter the integer you want to add to the first complex number:"
|
|
);
|
|
e = sc.nextInt();
|
|
|
|
Complex c1 = new Complex(a, b);
|
|
Complex c2 = new Complex(c, d);
|
|
|
|
Complex sum1 = c1.add(e);
|
|
Complex sum2 = c1.add(c2);
|
|
|
|
System.out.println("Sum of complex number and integer: " + sum1);
|
|
System.out.println("Sum of two complex numbers: " + sum2);
|
|
}
|
|
}
|