Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ComplexNo{
- private int real;
- private int img;
- public void getNo(int a,int b){
- this.real=a;
- this.img=b;
- System.out.println("Real number is: "+this.real+"\nImaginary number is: "+this.img+"i");
- }
- public void getNo(ComplexNo a){
- this.real=a.real;
- this.img=a.img;
- }
- public ComplexNo operatorPlus(ComplexNo x){
- ComplexNo temp = new ComplexNo();
- temp.real=this.real+x.real;
- temp.img=this.img+x.img;
- return temp;
- }
- public void display(){
- if(this.img>0){
- System.out.println("The complex no is:"+this.real+"+"+this.img+"i");
- }else{
- System.out.println("The complex no is:"+this.real+"-"+Math.abs(this.img)+"i");
- }
- }
- }
- public class TestComplexNo {
- public static void main(String[] args) {
- ComplexNo c1 = new ComplexNo();
- ComplexNo c2 = new ComplexNo();
- ComplexNo c3 = new ComplexNo();
- ComplexNo c4 = new ComplexNo();
- c1.getNo(12, -3);
- c2.getNo(4, 3);
- c3.getNo(3,4);
- c4.getNo(c1.operatorPlus(c2).operatorPlus(c3));
- c4.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement