Upload files to "OOP/Java/Lab/Week5"
This commit is contained in:
parent
ba0ee79bc1
commit
f7d1aa3358
4 changed files with 160 additions and 11 deletions
61
OOP/Java/Lab/Week5/ComplexAdder.java
Normal file
61
OOP/Java/Lab/Week5/ComplexAdder.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue