0% found this document useful (0 votes)
4 views

Import Java

Uploaded by

rashmimaruthi2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Import Java

Uploaded by

rashmimaruthi2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

class complex {
Double real;
Double imag;

public void getdata(Scanner sc) {


System.out.println(" Enter the real part of the complex number :");
this.real = sc.nextDouble();
System.out.println(" Enter the imaginary part of the complex number :");
this.imag = sc.nextDouble();
}

public void display() {


System.out.println("Complex number :" + this.real + "+" + this.imag + "i");
}

public void add(complex c) {


complex c3 = new complex();
c3.real = this.real + c.real;
c3.imag = this.imag + c.imag;
System.out.println("Addition:");
c3.display();
}

public void sub(complex c) {


complex c3 = new complex();
c3.real = this.real - c.real;
c3.imag = this.imag - c.imag;
System.out.println("Subtraction:");
c3.display();
}

public void compare(complex c) {


if (this.real.equals(c.real) && this.imag.equals(c.imag)) {
System.out.println("Complex numbers are equal.");
} else {
System.out.println("Complex numbers are not equal.");
}
}
}
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
{
System.out.println("Complex numbers:");
complex c1 = new complex();
complex c2 = new complex();
c1.getdata(sc);
c1.display();
c2.getdata(sc);
c2.display();
c1.compare(c2);
c1.add(c2);
c1.sub(c2);
}
}
}

You might also like