Java 作业帮助Pt2(复杂的数学课)
不确定我是否做对了,但这是我在这里工作的程序的延续 我正在为这个家庭作业苦苦挣扎Java 作业帮助Pt2(复杂的数学课),java,complex-numbers,Java,Complex Numbers,不确定我是否做对了,但这是我在这里工作的程序的延续 我正在为这个家庭作业苦苦挣扎 **(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
**(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来接收这个号码吗?比如想象第二部分和现实第二部分
再次感谢您的帮助。一些需要详细讨论的主题: 可变范围 传递到方法中的参数仅在该方法中可见。因此,为每个方法和所有方法命名两个操作数
comp1
和comp2
是非常好的
但是:
面向对象
您的方法应该只有一个参数。假设您有一个名为x
的复杂实例。您想添加另一个名为y的实例。然后给定您的代码,x.add(x,y)
和y.add(x,y)
甚至z.add(x,y)
的任何操作都会产生相同的结果
所以:放下你的一个参数。您可能需要添加空检查
public Complex add(Complex toAdd) {
return new Complex(this.realPart + toAdd.realPart,
this.imaginaryPart + toAdd.imagineryPart);
}
现在你可以写作了
Complex z = x.add(y);
能手和二传手
由于您的加法/减法/除法/乘法操作都返回一个新的复数,因此您可能希望使Contex不可变,即:不提供setter。可以通过构造函数创建复数。您可以通过调用对现有复数的计算来获得新的复数。但是你不能改变一个数字
因此,我的建议是:删除setter
复数输入
与其读取double
s,不如考虑读取String
并将该字符串与正则表达式匹配。您可以将其用作main中的实用方法,甚至用作Complex的constructor,允许使用字符串作为输入
考虑以下匹配字符串的方法:
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是添加到类声明中的接口:
public class Complex implements Cloneable {
此外,还应实现一个clone()
方法:
public Object clone() {
return super.clone();
}
toString()
赋值要求在字符串输出中省略0虚部。所以你可能想再检查一遍。这应该是一个简单的if()
可变范围
传递到方法中的参数仅在该方法中可见。因此,为每个方法和所有方法命名两个操作数comp1
和comp2
是非常好的
但是:
面向对象
您的方法应该只有一个参数。假设您有一个名为x
的复杂实例。您想添加另一个名为y的实例。然后给定您的代码,x.add(x,y)
和y.add(x,y)
甚至z.add(x,y)
的任何操作都会产生相同的结果
所以:放下你的一个参数。您可能需要添加空检查
public Complex add(Complex toAdd) {
return new Complex(this.realPart + toAdd.realPart,
this.imaginaryPart + toAdd.imagineryPart);
}
现在你可以写作了
Complex z = x.add(y);
能手和二传手
由于您的加法/减法/除法/乘法操作都返回一个新的复数,因此您可能希望使Contex不可变,即:不提供setter。可以通过构造函数创建复数。您可以通过调用对现有复数的计算来获得新的复数。但是你不能改变一个数字
因此,我的建议是:删除setter
复数输入
与其读取double
s,不如考虑读取String
并将该字符串与正则表达式匹配。您可以将其用作main中的实用方法,甚至用作Complex的constructor,允许使用字符串作为输入
考虑以下匹配字符串的方法:
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是添加到类声明中的接口:
public class Complex implements Cloneable {
此外,还应实现一个clone()
方法:
public Object clone() {
return super.clone();
}
toString()
赋值要求在字符串输出中省略0虚部。所以你可能想再检查一遍。这应该是一个简单的if()