2011-11-16 2 views
0

이 스레드를 읽은 주셔서 감사합니다. 나는 컴퓨터 과학 수업을위한 프로그램을 끝내려고 노력하고 있는데, 나는 왜 내 대답이 계속 0 + 0i로 나오는지 왜곡되어있다. 내 주요 프로그램을 실행하는 것입니다 여기복합 숫자를 사용하기 위해 만들어진 클래스에서 논리 오류가 발생했습니다.

 public class complex 
    { 
     private int real, imaginary; 
     public complex(int real1, int imaginary1) 
     { 
     int real=real1; 
     int imaginary= imaginary1; 
     } 

     public int getReal() 
     { 
     return real; 
     } 

     public int getImag() 
     { 
     return imaginary; 
     } 

     public complex add(complex complex2) 
     { 
     int real1, real2, imag1, imag2; 
     real1=getReal(); 
     real2=complex2.getReal(); 
     imag1=getImag(); 
     imag2=complex2.getImag(); 

     int sumReal= real1+real2; 
     int sumImaginary = imag1+ imag2; 
     return new complex(sumReal, sumImaginary); 
     } 

     public complex subtract(complex complex2) 
     { 
     int sumReal= getReal() -complex2.getReal(); 
     int sumImaginary = imaginary - complex2.getImag(); 
     return new complex (sumReal, sumImaginary); 
     } 
    // (a1+b1i)(a2+b2) 
     public complex multiply(complex complex2) 
     { 
     int a1, b1, a2, b2, sumReal, sumImaginary; 
     a1=getReal(); 
     b1=getImag(); 
     a2=complex2.getReal(); 
     b2=complex2.getImag(); 
     sumReal=a1*a2-(b1*b2); 
     sumImaginary=b1*a2+a1*b2; 
     return new complex(sumReal, sumImaginary); 
     } 


     public String toString() 
     { 
     String results; 
     results= real + "+" + imaginary + "i"; 
     return results; 
     } 

과 : 프로그램을 실행하면

 import java.util.Scanner; 
    public class complexRun 
    { 
     public static void main (String[] args) 
     { 
     Scanner scan= new Scanner(System.in); 
      int a1, a2, b1, b2; 
      a1=scan.nextInt(); 
      a2=scan.nextInt(); 
      b1=scan.nextInt(); 
      b2=scan.nextInt(); 
      complex complex1= new complex(a1, b1); 
      complex complex2 = new complex (a2, b2); 

      System.out.println(complex1.getReal()); 
      System.out.println(complex1.add(complex2)); 
      System.out.println(complex1.subtract(complex2)); 
      System.out.println(complex1.multiply(complex2)); 
      System.out.println(complex1.getReal()); 
      } 

     } 

, 당신이 대답 것 입력 어떤 것을 알 수 있습니다 여기

내 코드입니다 0 + 0i가된다. 내 논리 오류 내 getReal 및 getImaginary 부분 또는 개체의 초기 생성 거짓말을 믿지 만, 나는 그것을 해결하기 위해해야할지 모르겠다. 어떤 도움을 주셔서 감사 드리며, 오류를 수정하기 위해해야 ​​할 일이 아니라 잘못된 것을 설명 할 수 있다면 정말 좋을 것입니다. 감사합니다.

+0

tldr; 그것은 HW 태그면 그렇게. – Nishant

+0

@Nishant : 그것은 * 태그가 달린 숙제입니다. –

답변

2

이 문제입니다 사용 : 당신은 real라는 인스턴스 변수를 마스킹되는 생성자 내부 real라는 지역 변수를 선언하는

public class complex { 
    private int real, imaginary; 

    public complex(int real1, int imaginary1) { 
     int real=real1; 
     int imaginary= imaginary1; 
    } 
    ... 
} 

합니다. imaginary과 동일합니다. 그러므로 생성자 상기 로컬 변수 및 realimaginary에 복사하고 real1imaginary1의 값을, 상기 인스턴스 변수realimaginary 무시한다. 지역 변수는 즉시 범위를 벗어나 폐기됩니다.

짧은 이야기 : 생성자 본문에 int 선언을 제거하십시오. 1 번째 클래스의 복잡한 기능

+0

정말 고마워요! 담당자를 추가 할 방법이 있습니까? 감사 버튼처럼? – bts37

+0

당신은 대답을하고 대답을 수락 할 수 있습니다. 가장 도움이되는 답변에서 작은 위 화살표를 클릭하고 * 가장 유용한 답변에 체크 표시를하십시오. –

+0

예 ... 투표의 밑에 짧은 발꿈치가 있습니다. 클릭하면 대답을 수락 할 수 있습니다. – Arne

1

귀하의 add, subtractmultiply 방법은 메소드 호출의 대상이되는 complex의 인스턴스를 변이하지 않는, 오히려 그들은 그 값으로 새로운 복소수를 반환합니다. 당신이 필요합니다 그래서 (예를 들어) 대답 :

complex complex1 = new complex(5, 7); 
complex complex2 = new complex(6, 8); 
complex complex3 = complex1.add(complex2); 
System.out.println(complex3.getReal()); // returns 11 
System.out.println(complex3.getImaginary()); // returns 15 

당신은 그들 (함으로써 기능 void을) 대신의 값을 업데이트하는 각 기능의 끝에서 return 문을 제거하여 자신의 인스턴스를 변이 할 수 있습니다 realimaginary.


예로서 add 방법을 사용하여 : 복소 생성자 내부

public void add(complex complex2) { 
    int real1, real2, imag1, imag2; 
    real1 = getReal(); 
    real2 = complex2.getReal(); 
    imag1 = getImag(); 
    imag2 = complex2.getImag(); 

    real = real1 + real2; 
    imaginary = imag1 + imag2; 
    } 
+0

여기는 문제가 아니며 어떤 경우에도 문제가 아닙니다.불변의 객체를 가지는 것은 때때로 좋은 일입니다. –

+0

나는 불변 성의 목적과 이점을 이해한다. 나는 이것이 그대로 사용하는 방법과 그것을 변경 가능하게 만드는 방법을 모두 설명한 이유 중 하나이다. 그러나 마지막에'complex1.getReal()'을 호출 한 결과, OP는 complex1.getReal()의 값이 산술 연산 후에 변경 될 것으로 기대하고있었습니다. OP가 찾던 것이 아니기 때문에이 답변을 적용 할 수 없습니다. 그렇다면 세부 정보가 여기에 있습니다. –

+0

충분하다. 그러나이 경우에는 (im) 변경이 문제가되지 않는다. 그것은 별개로 좋은 대답입니다. –

0

는 실수 및 허수 변수를 선언 말아. 대신 this.real = real1 및 this.imaginary = imaginary1

0

변경 ...
유 쓰기 int real=real1;int imaginary= imaginary1;는 그 문제

했다 그래서 ... complex(int real1, int imaginary1)의 로컬 사촌 함수 외부 액세스 할 수 없습니다 새 변수를 만들
public complex(int real1, int imaginary1) 
    { 
    this.real=real1; // the problem was there 
    this.imaginary= imaginary1; // the problem was there 
    } 
관련 문제