2014-11-10 3 views
-2

어떻게하면 복소수로 기본 수학 연산을 수행하는지 정의하는 코드를 작성해야했습니다. 이러한 작업을 수행하는 방법과 코드를 실행하기위한 테스트 클래스를 정의하는 클래스를 만들었습니다. 여기 컴파일러가 잘못된 대답을 출력 함 - Java

import java.util.Scanner; 

public class ComplexNumber{ 

    float real; 
    float imaginary; 
    private float a; 
    private float b; 

public ComplexNumber(float a, float b){ 
    real = a; 
    imaginary = b; 
} 

public ComplexNumber add(ComplexNumber otherNumber){ 
    ComplexNumber newComplex; 
    float newA = a + otherNumber.getA(); 
    float newB = b + otherNumber.getB(); 
    newComplex = new ComplexNumber(newA, newB); 
    return newComplex; 
} 

public ComplexNumber minus(ComplexNumber otherNumber){ 
    ComplexNumber newComplex; 
    float newA = a - otherNumber.getA(); 
    float newB = b - otherNumber.getB(); 
    newComplex = new ComplexNumber(newA, newB); 
    return newComplex; 
} 

public ComplexNumber times(ComplexNumber otherNumber){ 
    ComplexNumber newComplex; 
    float newA = (a * otherNumber.getA()) - (b * otherNumber.getB()); 
    float newB = (b * otherNumber.getB()) - (a * otherNumber.getA()); 
    newComplex = new ComplexNumber(newA, newB); 
    return newComplex; 
} 

public ComplexNumber divide(ComplexNumber otherNumber){ 
    ComplexNumber newComplex; 
    float newA = (a * otherNumber.getA()) + (b*otherNumber.getB()); 
    float newB = (otherNumber.getA() * otherNumber.getA()) + (otherNumber.getB() * otherNumber.getB()); 
    float newC = (b * otherNumber.getA()) + (a * otherNumber.getB()); 
    float newD = (otherNumber.getA() * otherNumber.getA()) + (otherNumber.getB() * otherNumber.getB()); 
    float firstFraction = (newA/newB); 
    float secondFraction = (newC/newD); 
    newComplex = new ComplexNumber(firstFraction, secondFraction); 
    return newComplex;  
} 

public float getA(){ 
    return a; 
} 

public float getB(){ 
    return b; 
} 

public String toString(){ 
    return a + " + " + b + "i"; 
} 
} 

내가 가진 테스트 클래스입니다 : 여기

이 작업을 정의하는 코드,

내 코드를 실행할 때마다
import java.util.Scanner; 

class ComplexCalculator{ 
public static void main(String[] args){ 
    Scanner myScanner = new Scanner(System.in); 
    float tempA, tempB, tempC, tempD; 
    ComplexNumber first, second, result; 

    System.out.println("Choose an operation: \n1. Addition \n2. Subtraction \n3. Multiplication \n4. Division \n5. Quit"); 
    int selection = myScanner.nextInt(); 

    if (selection == 1) { 
     System.out.println("Enter A: "); 
     tempA = myScanner.nextFloat(); 
     System.out.println("Enter B: "); 
     tempB = myScanner.nextFloat(); 
     System.out.println("Enter C: "); 
     tempC = myScanner.nextFloat(); 
     System.out.println("Enter D: "); 
     tempD = myScanner.nextFloat(); 

     first = new ComplexNumber(tempA, tempB); 
     second = new ComplexNumber(tempC, tempD); 
     result = first.add(second); 
     System.out.println(result.toString()); 
    } 

    if (selection == 2) { 
     System.out.println("Enter A: "); 
     tempA = myScanner.nextFloat(); 
     System.out.println("Enter B: "); 
     tempB = myScanner.nextFloat(); 
     System.out.println("Enter C: "); 
     tempC = myScanner.nextFloat(); 
     System.out.println("Enter D: "); 
     tempD = myScanner.nextFloat(); 

     first = new ComplexNumber(tempA, tempB); 
     second = new ComplexNumber(tempC, tempD); 
     result = first.minus(second); 
     System.out.println(result.toString()); 
    } 

    if (selection == 3) { 
     System.out.println("Enter A: "); 
     tempA = myScanner.nextFloat(); 
     System.out.println("Enter B: "); 
     tempB = myScanner.nextFloat(); 
     System.out.println("Enter C: "); 
     tempC = myScanner.nextFloat(); 
     System.out.println("Enter D: "); 
     tempD = myScanner.nextFloat(); 

     first = new ComplexNumber(tempA, tempB); 
     second = new ComplexNumber(tempC, tempD); 
     result = first.times(second); 
     System.out.println(result.toString()); 
    } 

    if (selection == 4) { 
     System.out.println("Enter A: "); 
     tempA = myScanner.nextFloat(); 
     System.out.println("Enter B: "); 
     tempB = myScanner.nextFloat(); 
     System.out.println("Enter C: "); 
     tempC = myScanner.nextFloat(); 
     System.out.println("Enter D: "); 
     tempD = myScanner.nextFloat(); 

     first = new ComplexNumber(tempA, tempB); 
     second = new ComplexNumber(tempC, tempD); 
     result = first.divide(second); 
     System.out.println(result.toString()); 
    } 

    if (selection == 5){ 
     System.out.println("Thank you!"); 
     return; 
    } 

} 
} 

, 나는 네 개의 숫자를 넣어하지만, 항상 0.0 + 0.0i을 인쇄합니다. 당신이

public String toString(){ 
    return a + " + " + b + "i"; 
} 

로 정의 두 멤버

private float a; 
private float b; 

하고 toString()을 지정하지만 a 또는 b 중 하나가 설정되어있는 코드에서 아무데도 없다대로

+1

이 질문은 더 이상 재생 될 수 없거나 간단한 인쇄 오류로 인해 발생한 것입니다. 비슷한 질문이 여기에 주제 일지 모르지만, 이것은 미래 독자를 돕지 않을 방법으로 해결되었습니다. 이것은 종종 게시하기 전에 문제를 재현하는 데 필요한 가장 짧은 프로그램을 확인하고 면밀히 조사함으로써 피할 수 있습니다. –

+2

'float a;의 목적은 무엇입니까? float b;'이미 float을 가지고있을 때; 플로트 상상; – Pshemo

+0

그럼 디버깅을 위해 무엇을 했습니까? tempA, tempB, tempC, tempD의 값을 출력 했습니까? –

답변

1

문제가 발생합니다.

a 및 b를 realimaginary으로 바꾸어보십시오.

관련 문제