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

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

问题描述

不确定我是否正确行事,但这是我在这里工作的程序的延续...... 家庭作业帮助PT1

Not sure if I'm doing this right, but this is a continuation of the program I was working on here...Homework Help PT1

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

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?

再次感谢您的帮助。

推荐答案

要讨论的主题:

可变范围

传递给方法的参数是仅在整个方法中可见。因此,为每个和所有方法命名两个操作数 comp1 comp2 完全没问题。

Parameters passed into a method are visible only throughout that method. So naming your two operands comp1 and comp2 for each and all of your methods is perfectly fine.

但是:

面向对象

您的方法应该只有一个参数。假设您有一个名为 x 的Complex实例。并且您想要添加另一个名为y的实例。然后给出你的代码, x.add(x,y) y.add(x,y)甚至 z.add(x,y)会产生相同的结果。

Your methods should only have one parameter. Say your have one instance of Complex named x. And you want to add to that another instance named y. Then given your code, any operation of x.add(x,y) and y.add(x,y) and even z.add(x, y) would yield the same results.

所以:掉一个你的参数。您可能希望添加nullchecks。

So: Drop one of your paramters. You might want to add nullchecks.

public Complex add(Complex toAdd) {
   return new Complex(this.realPart + toAdd.realPart,
        this.imaginaryPart + toAdd.imagineryPart); 
}

现在你可以写了

Complex z = x.add(y); 

Getters and Setters

当你的加/减/除/乘运算都返回 new 复数时,你可能想让Contex 不可变 - 即:不提供setter。可以通过构造函数创建复数。您可以通过调用现有的复数来获得新的复数。但是你不能更改一个数字。

As your add / subtract / divide / multiply operations all return a new Complex number, you might want to make Contex immutable - that is: Provide no setters. Complex number can be created through the constructors. You can get new Complex numbers by invoking calculations on existing ones. But you cannot change a number.

所以我的建议是:删除setter。

So my advice: Remove the setters.

输入复数

而不是阅读双重 s,您可能想要考虑读取 String 并将该字符串与正则表达式匹配。您可以在main中使用它作为实用程序方法,或者甚至作为Complex的构造函数使用它,允许使用 String 作为输入。

Instead of reading doubles, you might want to think about reading a String and match that string with a regular expression. You could use that as a utility method in your main or even as a consttructor for Complex, allowing to use a String as input.

考虑使用此方法匹配字符串:

Consider this method for matching String:

    Pattern complexFinder = Pattern.compile("(-?\\d+(\\.\\d*)?)?\\s*([-+]\\s*\\d+(\\.\\d*)?i)?");
    Matcher m = complexFinder.matcher(complexString);
    if (m.find()) {
        double realPart = 0;
        double imaginaryPart = 0;
        if (m.group(1) != null) {
            realPart = Double.parseDouble(m.group(1).replaceAll("\\s", ""));
        }
        if (m.group(3) != null) {
            imaginaryPart = Double.parseDouble(m.group(3).replaceAll("\\s", "").replace("i", ""));
        }
        Complex c = new Complex(realPart, imaginaryPart);
    }

可克隆

Cloneable是您添加到类声明的接口:

Cloneable is an interface you add to your class declaration:

public class Complex implements Cloneable {

此外,您应该实现 clone()方法:

Additionally you should implement a clone() method:

public Object clone() {
   return super.clone();
}     

toString()

您的作业请求在字符串输出中省略0个虚部。所以你可能想再次检查一下。这应该是一个简单的 if()

Your assignment requests that an 0 imaginary part be left out in String output. So you might want to check that again. This should be a simple if()

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

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