首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何不更改方法中的对象

如何不更改方法中的对象
EN

Stack Overflow用户
提问于 2019-05-22 18:08:42
回答 1查看 79关注 0票数 0

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

  • 两个实例变量,a和b,表示a+bi中的变量。
  • 两个构造函数,一个带有两个实例变量和一个默认构造函数。
  • 返回复数的toString方法
  • 返回(a*a+b*b)^(1/2)的双模。
  • 一个整数象限,返回复数的象限(1-4),如果数字位于轴之一,返回一个0(所以,如果是a>0,b>0,它在第一个象限中)。
  • 产生并返回共轭的ComplexNumber共轭。(这基本上就是用(-b)而不是b返回复数。
  • ComplexNumber添加(ComplexNumber其他),生成并返回该数字和其他数字的和
  • ComplexNumber减法(ComplexNumber other),它生成并返回这个数字和另一个数字的差。
  • ComplexNumber乘(ComplexNumber其他),它构建并返回这个数字和另一个数字的乘积。

这是我的代码:

代码语言:javascript
复制
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);
       }
}

测试人员是

代码语言:javascript
复制
      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

*/

问题是,我得到

代码语言:javascript
复制
 (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方法、减法方法和乘方法改变了对象,所以该方法将对象更改为任何对象。

你能帮我修一下这些方法吗?这样它就不会改变对象了?

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-22 18:39:01

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

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

下面是一个示例,说明如何使用add方法执行此操作:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56262635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档