【问题标题】:How not to change an object in a method如何不更改方法中的对象
【发布时间】:2019-05-23 02:53:03
【问题描述】:

我有一个关于复数的实验室。我必须有

  • 两个实例变量a和b,分别代表a+bi中的变量。

  • 两个构造函数,一个带有两个实例变量和一个默认构造函数。

  • 返回复数的toString方法

  • 返回 (a*a+b*b)^(1/2) 的双模。

  • int 象限,返回复数的象限 (1-4),如果数字位于某个轴上,则返回 0(因此,如果 a>0,b>0,则它位于第一象限)。

  • 产生和返回共轭的ComplexNumber 共轭。 (基本上只是用 (-b) 而不是 b 返回复数。

  • ComplexNumber add (ComplexNumber other) 构建并返回此数字与其他数字之和

  • ComplexNumber 减法 (ComplexNumber other) 生成并返回此数字与其他数字的差。

  • ComplexNumber 相乘(ComplexNumber other),生成并返回此数字与另一个数字的乘积。

这是我的代码:

public class ComplexNumber {
       private int a;
       private int b;

       public ComplexNumber(int c, int d){
           a=c;
           b=d;
       }

       public ComplexNumber(){
           a=0;
           b=1;
       }

       public int getA(){
           return a;
       }

       public int getB(){
           return b;
       }

       public void setA( int newA){
           a=newA;
       }

       public void setB( int newB){
           b=newB;
       }

       public String toString(){
           if (a!=0&&b!=0){
                return a+"+("+b+")*i";
           }
           else if (b!=0&&a==0){
                return "("+b+")*i";
           }
           else if(a!=0&&b==0){
                return a+"";
           }
           else 
                return "0";
       }

       public double modulus(){
           return Math.sqrt((a*a+b*b));
       }

       public int quadrant(){
           if(a>0&&b>0){
               return 1;
           }
           else if (a>0&&b<0){
               return 4;
           }
           else if (a<0&&b>0){
               return 2;
           }
           else if (a<0&&b<0){
               return 3;
           }
           else 
               return 0;
       }

       ComplexNumber conjugate(){
           b=-b;
           return new ComplexNumber (a,b);
       }

       ComplexNumber add(ComplexNumber other){
           return new ComplexNumber(this.a+other.a,this.b+other.b);
       }

       ComplexNumber subtract(ComplexNumber other){
           a=Math.abs(this.a-other.a);
           b=Math.abs(this.b-other.b);
           return new ComplexNumber(a,b);
       }

       ComplexNumber multiply(ComplexNumber other){
           a=(this.a)*(other.a)+(this.b)*(other.b);
           b=(this.a)*(other.b)+(this.b)*(other.a);
           return new ComplexNumber(a,b);
       }
}

而测试者是

      public class ComplexNumber_Tester   {
    public static void main (String[] args) {
         //checking toString()
       ComplexNumber a1= new ComplexNumber();
       System.out.println(a1);
       ComplexNumber a2= new ComplexNumber(0,0);
       System.out.println(a2);
       ComplexNumber a3= new ComplexNumber(4,0);
      System.out.println(a3);
       ComplexNumber a4= new ComplexNumber(-4,-7);
       System.out.println(a4);
       ComplexNumber a5= new ComplexNumber(8,-27);
       System.out.println(a5);
       ComplexNumber a6= new ComplexNumber(5,4);
       System.out.println(a6);

  // checking modulus()
  System.out.println();
  System.out.println("abs value of ("+a5+") = "+ a5.modulus());
  System.out.println("abs value of ("+a1+") = "+ a1.modulus());
  System.out.println("abs value of ("+a3+") = "+ a3.modulus());
   // checking conjugate()
  System.out.println();
  System.out.println("conjugate of ("+a5+") = "+ a5.conjugate());
  System.out.println("conjugate of ("+a1+") = "+ a1.conjugate()); 
  System.out.println("conjugate of ("+a3+") = "+ a3.conjugate());
  // checking add()
  System.out.println();
  System.out.println("add ("+a5+")and ("+a4+"):     "+ a5.add(a4)); 
  System.out.println("add ("+a4+")and ("+a5+"):     "+ a4.add(a5)); 
  System.out.println("add ("+a1+")and ("+a5+"):     "+ a5.add(a1)); 
// checking subtract()
  System.out.println();
  System.out.println("subtract ("+a5+")and ("+a4+"):     "+ a5.subtract(a4)); 
  System.out.println("subtract ("+a4+")and ("+a5+"):     "+ a4.subtract(a5)); 
  System.out.println("subtract ("+a1+")and ("+a5+"):     "+ a1.subtract(a5)); 
  // checking multiply()
  System.out.println();
  System.out.println("multiply ("+a5+")and ("+a4+"):     "+ a5.multiply(a4)); 
  System.out.println("multiply ("+a4+")and ("+a5+"):     "+ a4.multiply(a5)); 
  System.out.println("multiply ("+a1+")and ("+a5+"):     "+ a1.multiply(a5));
  System.out.println("multiply ("+a3+")and ("+a6+"):     "+ a3.multiply(a6));
  System.out.println("multiply ("+a5+")and ("+a2+"):     "+ a5.multiply(a2)); 
    // checking quadrant()
       System.out.println();
       System.out.println(a5+"  is in quadrant "+a5.quadrant()); 
       System.out.println(a3+"  is in quadrant "+a3.quadrant()); 
       System.out.println(a4+"  is in quadrant "+a4.quadrant()); 
       System.out.println(a6+"  is in quadrant "+a6.quadrant()); 
       System.out.println(new ComplexNumber(-6, 7)+"  is in quadrant "+ new ComplexNumber(-6, 7).quadrant());    
  }
  }
 /*
    1*i
 0
 4
 -4-7*i
 8-27*i
 5+4*i

  abs value of (8-27*i) = 28.160255680657446
  abs value of (1*i) = 1.0
  abs value of (4) = 4.0

  conjugate of (8-27*i) = 8+27*i
  conjugate of (1*i) = -1*i
  conjugate of (4) = 4

  add (8-27*i)and (-4-7*i):     4-34*i
  add (-4-7*i)and (8-27*i):     4-34*i
  add (1*i)and (8-27*i):     8-26*i

subtract (8-27*i)and (-4-7*i):     12-20*i
subtract (-4-7*i)and (8-27*i):     -12+20*i
subtract (1*i)and (8-27*i):     -8+28*i

multiply (8-27*i)and (-4-7*i):     -221+52*i
multiply (-4-7*i)and (8-27*i):     -221+52*i
multiply (1*i)and (8-27*i):     27+8*i
multiply (4)and (5+4*i):     20+16*i
 multiply (8-27*i)and (0):     0

 8-27*i  is in quadrant 4
   4  is in quadrant 0
   -4-7*i  is in quadrant 3
    5+4*i  is in quadrant 1
    -6+7*i  is in quadrant 2

*/

问题是,我明白了

 (1)*i
  0
  4
 -4+(-7)*i
  8+(-27)*i
  5+(4)*i

abs value of (8+(-27)*i) = 28.160255680657446
abs value of ((1)*i) = 1.0
abs value of (4) = 4.0

conjugate of (8+(-27)*i) = 8+(27)*i
conjugate of ((1)*i) = (-1)*i
 conjugate of (4) = 4

add (8+(27)*i)and (-4+(-7)*i):     4+(20)*i
add (-4+(-7)*i)and (8+(27)*i):     4+(20)*i
add ((-1)*i)and (8+(27)*i):     8+(26)*i

subtract (8+(27)*i)and (-4+(-7)*i):     12+(34)*i
subtract (-4+(-7)*i)and (12+(34)*i):     16+(41)*i
subtract ((-1)*i)and (12+(34)*i):     12+(35)*i

multiply (12+(34)*i)and (16+(41)*i):     1586+(65570)*i
multiply (16+(41)*i)and (1586+(65570)*i):     2713746+(1846731110)*i
multiply (12+(35)*i)and (1586+(65570)*i):     2313982+(1403999890)*i
multiply (4)and (5+(4)*i):     20+(80)*i
multiply (1586+(65570)*i)and (0):     0

 0  is in quadrant 0
 20+(80)*i  is in quadrant 1
 2713746+(1846731110)*i  is in quadrant 1
 5+(4)*i  is in quadrant 1
 -6+(7)*i  is in quadrant 2

对于 add 方法,我应该使用 8-27i 而不是共轭。

我知道发生这种情况是因为 add 方法、subtract 方法和 multiply 方法改变了对象,所以该方法接受对象改变成的任何内容。

你能帮我修复方法,这样它就不会改变对象吗?

提前致谢!

【问题讨论】:

  • 解释中的修复。之所以需要 8+27i 可能是因为共轭法,而不是加法。如何修复共轭方法,以便 add 方法不会采用更改的对象而是采用原始对象?
  • 好的,但看起来您的问题是您在这些方法中更改 ab 而不是声明局部变量。您可以在方法中执行 int newA; int newB; 并将其用于中间计算。
  • 但是如果我返回 return new ComplexNumber (a,NewB);
  • 它仍然改变了对象
  • 改成private final int a; private final int b;,编译器显示问题点

标签: java


【解决方案1】:

您的方法更改对象值的原因是因为它们直接对类变量执行操作。

为避免这种情况,您可以在每个方法中声明要执行的操作的局部变量。

下面是一个示例,说明如何使用 add 方法做到这一点:

public class ComplexNumber {
    private int a;
    private int b;

public ComplexNumber(int x, int y) {
    a = x;
    b = y;
}

public ComplexNumber add(ComplexNumber other) {
    int tempA = a + other.a;
    int tempB = b + other.b;

    return new ComplexNumber(tempA, tempB);
}

附:在我撰写此答案时,您的代码非常难以阅读,这里是阅读有关格式化 java 代码的公认做法的好地方,遵循这些准则有助于使您的代码更易于理解。

https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md#formatting

【讨论】:

  • 另外,最好将 a 和 b 声明为决赛
猜你喜欢
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多