Skip to content

Commit

Permalink
added-prac-01-java
Browse files Browse the repository at this point in the history
  • Loading branch information
rajjitlai committed Oct 10, 2024
1 parent ded90e2 commit 08306c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Binary file added Java/Practical/practical-01/Complex.class
Binary file not shown.
38 changes: 38 additions & 0 deletions Java/Practical/practical-01/Complex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Complex {
double real;
double imaginary;

public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

public Complex add(Complex other) {
return new Complex(real + other.real, imaginary + other.imaginary);
}

public Complex multiply(Complex other) {
double realPart = (real * other.real) - (imaginary * other.imaginary);
double imaginaryPart = (real * other.imaginary) + (imaginary * other.real);
return new Complex(realPart, imaginaryPart);
}

@Override
public String toString() {
if (imaginary < 0) {
return real + " - " + Math.abs(imaginary) + "i";
}
return real + " + " + imaginary + "i";
}

public static void main(String[] args) {
Complex c1 = new Complex(3, 2);
Complex c2 = new Complex(1, 7);

Complex sum = c1.add(c2);
System.out.println("Sum: " + sum);

Complex product = c1.multiply(c2);
System.out.println("Product: " + product);
}
}

0 comments on commit 08306c8

Please sign in to comment.