家庭作业帮助(复杂班级)

时间:2015-12-12 07:08:13

标签: java

Continuation of Questions Related To This Program

如果我把它放在错误的区域,我首先要道歉。这是我的第一篇文章,我还没有弄明白一切是如何运作的。

我在这项家庭作业中苦苦挣扎......

**(Math: The Complex class) A complex number is a number in the form a + bi,
where a and b are real numbers and i is 2-1. The numbers a and b are known
as the real part and imaginary part of the complex number, respectively. You can
perform addition, subtraction, multiplication, and division for complex numbers
using the following formulas:
a + bi + c + di = (a + c) + (b + d)i
a + bi - (c + di) = (a - c) + (b - d)i
(a + bi)*(c + di) = (ac - bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
You can also obtain the absolute value for a complex number using the following
formula:
 a + bi  = 2a2 + b2
Design a class named Complex for representing complex numbers and the
methods add, subtract, multiply, divide, and abs for performing complexnumber
operations, and override toString method for returning a string representation
for a complex number. The toString method returns (a + bi) as a
string. If b is 0, it simply returns a. Your Complex class should also implement the
Cloneable interface.
Provide three constructors Complex(a, b), Complex(a), and Complex().
Complex() creates a Complex object for number 0 and Complex(a) creates
a Complex object with 0 for b. Also provide the getRealPart() and
getImaginaryPart() methods for returning the real and imaginary part of the
complex number, respectively.
Write a test program that prompts the user to enter two complex numbers and
displays the result of their addition, subtraction, multiplication, division, and absolute
value.**

这是我到目前为止所拥有的。两个班......

// ComplexTest.java

import java.util.Scanner;

public class ComplexTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the first complex number: ");
        double realPart = input.nextDouble();

        System.out.println("Enter the second complex number: ");
        double imaginaryPart = input.nextDouble();

        Complex cn1 = new Complex(realPart, imaginaryPart);
        Complex cn2 = new Complex(realPart);
        Complex cn3 = new Complex();

        if (realPart == 0) {
            System.out.println(cn3.toString());
        }
        if (imaginaryPart == 0) {
            System.out.println(cn2.toString());
        }
        if(realPart != 0 && imaginaryPart != 0) {
            System.out.println(cn1.toString());
        }
    }
}

// Complex.java

import java.util.Scanner;

public class Complex {

    // cloneable interface
    public interface Cloneable { }

    // Instance Real + Getters and Setters (Accessors and Mutators)
    private double realPart;

    public double getReal() {
        return realPart;
    }

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

    // Instance Real + Getters and Setters (Accessors and Mutators)

    private double imaginaryPart;

    public double getImaginary() {
        return imaginaryPart;
    }

    public void setImaginary(double imaginary) {
        this.imaginaryPart = imaginary;
    }

    // Constructor Method CN1
    public Complex(double a, double b) {
        realPart = a;
        imaginaryPart = b;
    }

    // Constructor Method CN2
    public Complex(double a) {
        realPart = a;
        imaginaryPart = 0;
    }

    // Constructor Method CN3
    public Complex() { }

    // Add Complex Numbers
    public Complex add(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getImaginary();
        double imaginary2 = comp2.getImaginary();

        return new Complex(real1 + real2, imaginary1 + imaginary2);
    }

    // Subtract Complex Numbers
    public Complex subtract(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 - real2, imaginary1 - imaginary2);
    }

    // Multiply Complex Numbers
    public Complex multiply(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 * real2, imaginary1 * imaginary2);
    }

    // Divide Complex Numbers
    public Complex divide(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 / real2, imaginary1 / imaginary2);
    }

    // toString to Change Display
    public String toString() {
        String result;
        result = realPart + " + " + imaginaryPart + "i";
        return result;
    }
}

这是我在Jan的帮助下更新的代码。我创造了另外3种方法(减法,乘法和除法)。我是不是应该在每种方法中使用comp1和comp2而是将它们彼此分开命名?目标是在最后同时打印每种方法的结果。具有相同名称的这些是否会混乱?

我也想知道何时应该实现可克隆接口。

最后,根据文本,复数实际上看起来像是用空格分隔的两个数字。 (即3.5 5.0而不是3.5)。如果我为两个复数的后半部分再添加两个扫描仪输入,我将不得不更改我的代码。我是否必须创建新的getter和setter来接收这个号码?如imamblearyPart2和realPart2?

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

编译错误的一些提示:

    Complex real1 = realPart;
    Complex real2 = (Complex) realPart.clone();
    Complex imaginary1 = imaginaryPart;
    Complex imaginary2 = (Complex) imaginaryPart.clone();

    double realTotal = real1 + real2;
    double imaginaryTotal = imaginary1 + imaginary2;

您的成员变量real1,2,imaginary1,2需要是double类型而不是Complex

现在它编译 - 但它不起作用。您想要添加复数。所以重命名你的方法签名:

public Complex add(Complex comp1, Complex comp2) {
    double real1 = comp1.getReal();
    double real2 = comp2.getReal();
    double imaginary1 = comp1.getImaginary();
    double imaginary2 = comp2.getImaginary();

    return new Complex(real1+real2, imaginary1 +imaginary2);
}

现在继续进行工具倍增......