家庭作业帮助(复杂班) [英] Homework Help (The Complex Class)

查看:133
本文介绍了家庭作业帮助(复杂班)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

延续与此计划相关的问题

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

First I'd like to apologize if I'm placing this in the wrong area. This is my first post and I haven't quite figured out how everything works yet.

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

I'm struggling a lot with this homework assignment...

**(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.**

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

Here is what I have so far. Two classes...

// 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而是将它们彼此分开命名?目标是在最后同时打印每种方法的结果。具有相同名称的这些是否会混乱?

Here is my updated code after Jan's help. I've created 3 more methods (subtract, multiply and divide). Should I not be using comp1 and comp2 in every method and instead name them separately from each other? The goal is to print the results of each method at the end at the same time. Will these having the same names mess with that?

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

I'd also like to know when I should implement the cloneable interface.

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

Lastly, according to the text a complex number actually looks like two numbers separated by a space. (i.e. 3.5 5.0 rather than just 3.5). If I add two more scanner inputs for the second halves of both of complex numbers, I will have to change my code. Will I have to create new getters and setters to receive this number? Such as imaginaryPart2 and realPart2?

再次感谢您的帮助。

推荐答案

编译错误的提示:

    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 not Complex

Your member variables real1, 2, imaginary1,2 need to be of type double not Complex

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

Now it compiles - but it will not work. You want to add to Complex numbers after all. So rename your method signature:

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);
}

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

Now go ahead an implement multiplication likewise...

这篇关于家庭作业帮助(复杂班)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆