Adding and subtract complex numbers using OOP structure

余生长醉 提交于 2019-12-20 05:49:30

问题


I have here a code that should print the sum and difference of two complex numbers. The instructions given are:
make the methods add, subtract, and print to be void and
test using the constructor's object.

public class Complex {

    /**
     * @param args
     */
    public double real;
    public double imag;
    public String output = "";

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

    public Complex(){
        real = 0;
        imag = 0;
    }

    public double getReal(){
        return real;
    }

    public void setReal(double real){
        this.real = real;
    }

    public double getImag(){
        return imag;
    }

    public void setImag(double imag){
        this.imag = imag;
    }

    public void add(Complex num){
        this.real = real + num.real;
        this.imag = imag + num.imag;
    }

    public void subtract(Complex num){
        this.real = real - num.real;
        this.imag = imag - num.imag;
    }

    public void print(){
        //
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Complex c1 = new Complex(4.0, 8.5);
        Complex c2 = new Complex(8.0, 4.5);

        c1.add(c2);
        c1.subtract(c2);
        c1.print(); //expected answer 12.0 + 13.0i
                                    //-4.0 - 4.0i
    }

}



The expected answers are 12.0 + 13.0i and -4.0 - 4.0i. Please help me with the method print. Thank you.


回答1:


Perhaps this is not what you're looking for, but to make the number be printed isn't enough to make something like this in your print method?

System.out.print("The number is: " +real +"+i" +imag);




回答2:


   public void print(){
     if(this.imag <0){
      System.out.println(this.real+" "+this.imag+"i");
     }
     if(this.imag >0){
      System.out.println(this.real+"+"+this.imag+"i");
     }
    }



回答3:


You incorrectly use print merhod. if you want to see correct result you need to rewrite add method like this:

public void add(Complex num, Complex num2){
    this.real = num.real + num2.real;
    this.imag = num.imag + num2.imag;
}

rewrite subtract method also.

public void subtract(Complex num){
    this.real = real - num.real;
    this.imag = imag - num.imag;
}

Now main method look like this:

public static void main(String[] args) {
        Complex c1 = new Complex(4.0, 8.5);
        Complex c2 = new Complex(8.0, 4.5);
        Complex result = new Complex(8.0, 4.5);
        result.add(c1,c2);

        result.print();

        result.subtract(c1,c2);
        result.print();

print method as I told previously look like:

public void print(){
    System.out.println(real + " " + imag +"i");
}

Explanation:

In your code you have error. You add c2 to c1 and then subtract c2 frim c1 and then print result. Mathematically this looks like : c1= c1+c2-c2;



来源:https://stackoverflow.com/questions/18550771/adding-and-subtract-complex-numbers-using-oop-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!