不确定我是否正确行事,但这是我在这里工作的计划的延续...... Homework Help PT1
我在这项家庭作业中苦苦挣扎......
**(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?
再次感谢您的帮助。
答案 0 :(得分:0)
要讨论的一些话题:
可变范围
传递给方法的参数仅在整个方法中可见。因此,为您的每个方法命名您的两个操作数comp1
和comp2
完全没问题。
可是:
面向对象
您的方法应该只有一个参数。假设您有一个名为x
的Complex实例。并且您想要添加另一个名为y的实例。然后,根据您的代码,x.add(x,y)
和y.add(x,y)
甚至z.add(x, y)
的任何操作都会产生相同的结果。
所以:放下你的一个参数。您可能想要添加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
由于您的加/减/除/乘运算都返回新的复数,您可能希望使Contex 不可变 - 即:不提供设置器。可以通过构造函数创建复数。您可以通过调用现有的计算机来获取新的复数。但你不能更改一个数字。
所以我的建议是:删除二传手。
输入复数
您可能想要阅读double
并将该字符串与正则表达式匹配,而不是阅读String
。您可以在main中使用它作为实用程序方法,或者甚至作为Complex的构造函数使用它,允许使用String
作为输入。
考虑使用此方法匹配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是您添加到类声明中的接口:
public class Complex implements Cloneable {
此外,您应该实施clone()
方法:
public Object clone() {
return super.clone();
}
<强>的toString()强>
您的作业请求在字符串输出中省略0个虚部。所以你可能想再次检查一下。这应该是一个简单的if()